|
using System; |
|
using System.Threading.Tasks; |
|
using UIAutomationClient; |
|
namespace MPBRCL |
|
{ |
|
class Program |
|
{ |
|
// EdgeのアドレスバーのURL取得 |
|
static async Task Main(string[] args) |
|
{ |
|
IUIAutomation uiAutomation = new CUIAutomation8(); |
|
IUIAutomationElement rootElement = uiAutomation.GetRootElement(); |
|
|
|
IUIAutomationElement elmEdge = null; |
|
|
|
// MatchSubstring を使うには Win10 v1809 以降が必要です。 |
|
// 古い環境向けでは、NameProperty 条件を外した FindFirstAll にて列挙判定します。 |
|
elmEdge = rootElement.FindFirst(TreeScope.TreeScope_Children, |
|
uiAutomation.CreateAndCondition( |
|
uiAutomation.CreatePropertyCondition( |
|
UIA_PropertyIds.UIA_ControlTypePropertyId, |
|
UIA_ControlTypeIds.UIA_WindowControlTypeId |
|
), |
|
uiAutomation.CreatePropertyConditionEx( |
|
UIA_PropertyIds.UIA_NamePropertyId, |
|
"Microsoft\u200B Edge", |
|
PropertyConditionFlags.PropertyConditionFlags_IgnoreCase | |
|
PropertyConditionFlags.PropertyConditionFlags_MatchSubstring |
|
) |
|
) |
|
); |
|
// 最初に見つけた Edge のみが対象 |
|
if (elmEdge != null) |
|
{ |
|
Console.WriteLine("name : " + elmEdge.CurrentName); |
|
|
|
// Name の代わりに ClassName で検索する場合は "OmniboxViewViews" |
|
IUIAutomationElement elmAddress = |
|
elmEdge.FindFirst(TreeScope.TreeScope_Subtree, |
|
uiAutomation.CreateAndCondition( |
|
uiAutomation.CreatePropertyCondition( |
|
UIA_PropertyIds.UIA_ControlTypePropertyId, |
|
UIA_ControlTypeIds.UIA_EditControlTypeId |
|
), |
|
uiAutomation.CreatePropertyCondition( |
|
UIA_PropertyIds.UIA_NamePropertyId, |
|
"アドレスと検索バー" |
|
) |
|
) |
|
); |
|
var getAddress = (elmAddress == null) |
|
? new Func<string>(() => string.Empty) |
|
: () => elmAddress.GetCurrentPropertyValue( |
|
UIA_PropertyIds.UIA_ValueValuePropertyId) as string ?? ""; |
|
|
|
// "タブ バー" |
|
IUIAutomationElement elmTabBar = |
|
elmEdge.FindFirst(TreeScope.TreeScope_Subtree, |
|
uiAutomation.CreateAndCondition( |
|
uiAutomation.CreatePropertyCondition( |
|
UIA_PropertyIds.UIA_ControlTypePropertyId, |
|
UIA_ControlTypeIds.UIA_TabControlTypeId |
|
), |
|
uiAutomation.CreatePropertyCondition( |
|
UIA_PropertyIds.UIA_ClassNamePropertyId, |
|
"TabStripRegionView" |
|
) |
|
) |
|
); |
|
|
|
// タブ群のペイン |
|
IUIAutomationElementArray elmTabs = |
|
elmTabBar?.FindFirst(TreeScope.TreeScope_Subtree, |
|
uiAutomation.CreateAndCondition( |
|
uiAutomation.CreatePropertyCondition( |
|
UIA_PropertyIds.UIA_ControlTypePropertyId, |
|
UIA_ControlTypeIds.UIA_PaneControlTypeId |
|
), |
|
uiAutomation.CreatePropertyCondition( |
|
UIA_PropertyIds.UIA_ClassNamePropertyId, |
|
"TabContainerImpl" |
|
) |
|
) |
|
)?.FindAll(TreeScope.TreeScope_Children, |
|
// このサンプルでは、グループ化されたタブを無視しています |
|
// グループも拾う場合は、Or 条件で UIA_ButtonControlTypeId を加えてください |
|
uiAutomation.CreatePropertyCondition( |
|
UIA_PropertyIds.UIA_ControlTypePropertyId, |
|
UIA_ControlTypeIds.UIA_TabItemControlTypeId |
|
) |
|
); |
|
|
|
Console.WriteLine($"Tabs.Count:{elmTabs?.Length}"); |
|
|
|
// ここでは右側のタブから順に処理しています |
|
for (int i = (elmTabs?.Length ?? 0) - 1; i >= 0; i--) |
|
{ |
|
IUIAutomationElement tabItem = elmTabs.GetElement(i); |
|
Console.WriteLine($"Tab{i + 1}.Name:{tabItem.CurrentName}"); |
|
Console.WriteLine($@"Tab{i + 1}.FullDescription:{ |
|
tabItem.GetCurrentPropertyValue( |
|
UIA_PropertyIds.UIA_FullDescriptionPropertyId |
|
) |
|
}"); |
|
|
|
IUIAutomationInvokePattern tabInvoker = (IUIAutomationInvokePattern) |
|
tabItem.GetCurrentPattern(UIA_PatternIds.UIA_InvokePatternId); |
|
IUIAutomationInvokePattern closeInvoker = (IUIAutomationInvokePattern) |
|
tabItem.FindFirst(TreeScope.TreeScope_Children, |
|
uiAutomation.CreateAndCondition( |
|
uiAutomation.CreatePropertyCondition( |
|
UIA_PropertyIds.UIA_ControlTypePropertyId, |
|
UIA_ControlTypeIds.UIA_ButtonControlTypeId |
|
), |
|
uiAutomation.CreatePropertyCondition( |
|
UIA_PropertyIds.UIA_NamePropertyId, |
|
"タブを閉じる" |
|
) |
|
) |
|
)?.GetCurrentPattern(UIA_PatternIds.UIA_InvokePatternId); |
|
tabInvoker?.Invoke(); |
|
if (tabInvoker != null) |
|
{ |
|
await Task.Delay(270); |
|
Console.WriteLine($"Tab{i + 1}.Address:{getAddress()}"); |
|
|
|
// ピン留めされていると "タブを閉じる" ボタンがありません |
|
// ピン留めされていても閉じたいなら、コンテキストメニューを呼び出すか、 |
|
// もしくは Ctrl+W を送出して閉じるようにします |
|
if (closeInvoker != null) |
|
{ |
|
Console.Write("このタブを閉じますか?(Y/N):"); |
|
if ("Y" == Console.ReadLine().ToUpperInvariant()) |
|
{ |
|
closeInvoker.Invoke(); |
|
} |
|
} |
|
} |
|
} |
|
} |
|
} |
|
} |
|
} |