Skip to content

Instantly share code, notes, and snippets.

@Benshi
Last active March 25, 2024 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Benshi/e6cca7a15bac7eef12d1e888e55988ca to your computer and use it in GitHub Desktop.
Save Benshi/e6cca7a15bac7eef12d1e888e55988ca to your computer and use it in GitHub Desktop.
[C#]Edge で開かれているタブを閉じる

Visual Studio 2019 + C# で、Edge で開かれているタブを閉じるサンプル

元ネタ

  • No101896: Edgeで開かれているタブを指定の条件で閉じる
    at 2023/05/11(Thu) 17:10:21
    by 新米SE

メモ

  • (.NET Managed ではなく) COM 版の UIAutomationClient を用いたサンプルです。
  • error CS1752 が発生する場合は、ソリューション エクスプローラーで [参照]-[UIAutomationClient] を選択し、プロパティ一覧の中から 相互運用型の埋め込み を False にしてコンパイルしてください。
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();
}
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment