質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Q&A

解決済

1回答

14569閲覧

現在開いているEdgeのURLとタイトルを取得したいです。

cancat

総合スコア313

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

0グッド

0クリップ

投稿2017/08/17 13:36

編集2017/08/18 04:49

こんにちは。
Windows10でC#のアプリケーションを開発しています。
Visual Studio 2017 Communityを使っています。

###前提・実現したいこと
現在開いているEdgeのURLとタイトルを取得したいです。

###試したこと
以前に質問して、Edgeに関する参考サイトを教えていただきました。
https://www.ka-net.org/blog/?p=6148
ここを参考に、というかまるっとそのままですが、コンソールコマンドのプロジェクトを作り、実行してみましたが、いきなりAutomationElement edgeでedgeを取得することができず、nullになっています。
参考サイトは、2015年8月ごろの日付なので、それほど古いとも環境が異なっているとも思えないのです。
アドバイスいただければ幸いです。

[追記]
なるほど。CreatorsUpdateで変わっているのですね。情報ありがとうございます。
TreeScope.Childrenだけなのがおかしいとのことですが、具体的にはどうしたらよいのでしょう?

UIAutomationClient, UIAutomationTypesをcomで参照追加しました。

###発生している問題・エラーメッセージ
AutomationElement edgeでedgeを取得することができず、nullになっています。

###該当のソースコード

C#

1using System; 2using System.Runtime.InteropServices; 3using System.Text; 4using System.Windows.Automation; 5 6namespace UIAutomationEdge 7{ 8 class Program 9 { 10 public delegate bool Win32Callback(IntPtr hwnd, IntPtr lParam); 11 [DllImport("user32.Dll")] 12 [return: MarshalAs(UnmanagedType.Bool)] 13 public static extern bool EnumChildWindows(IntPtr parentHandle, Win32Callback callback, IntPtr lParam); 14 15 [DllImport("user32.dll", CharSet = CharSet.Auto)] 16 static public extern IntPtr GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); 17 18 [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)] 19 public static extern IntPtr GetParent(IntPtr hWnd); 20 public static void Main(string[] args) 21 { 22 AutomationElement root = AutomationElement.RootElement; 23 AutomationElement edge = root.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "TitleBar")); 24 if (edge != null) 25 { 26 edge = edge.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "Windows.UI.Core.CoreWindow")); 27 if (edge != null) 28 { 29 Win32Callback childProc = new Win32Callback(EnumWindow); 30 EnumChildWindows((IntPtr)edge.Current.NativeWindowHandle, childProc, (IntPtr)0); 31 Console.ReadKey(true); 32 } 33 } 34 } 35 36 private static bool EnumWindow(IntPtr handle, IntPtr pointer) 37 { 38 StringBuilder buf = new StringBuilder(255); 39 IntPtr ret = GetClassName(handle, buf, buf.Capacity); 40 if (ret != IntPtr.Zero) 41 { 42 string className = buf.ToString(); 43 if (className == "Windows.UI.Core.CoreComponentInputSource") 44 { 45 IntPtr hTitle = GetParent(handle); 46 AutomationElement title = AutomationElement.FromHandle(hTitle); 47 AutomationElement url = AutomationElement.FromHandle(handle); 48 Console.WriteLine(title.Current.Name + ", " + url.Current.Name); 49 } 50 } 51 return true; 52 } 53 } 54 } 55}

###補足情報(言語/FW/ツール等のバージョンなど)
Microsoft Visual Studio Community 2017
Version 15.0.26228.9 D15RTWSVC
Microsoft .NET Framework
Version 4.6.01586

です。
よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hmmm

2017/08/17 16:27

条件がTreeScope.Childrenだけなのがおかしいです。それとCreators UpdateでWindowの構成が変わっていたと思います。
guest

回答1

0

ベストアンサー

.Net FrameworkのSystem.Windows.Automationだとクラス名が"Internet Explorer_Server"のエレメントを探すことがなぜかできないので、c++で例を記載しました。
C#で実装したい場合は、質問に記載のリンク先に記載されているようにtlbimp でinterop dllを作成しないと動かないかもしれません。

c++

1#include <Windows.h> 2#include <UIAutomationClient.h> 3#include <atlbase.h> 4#include <string> 5 6using namespace ATL; 7 8int main() 9{ 10 //エラー処理は省略 11 ::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); 12 13 CComPtr<IUIAutomation> automation; 14 automation.CoCreateInstance(CLSID_CUIAutomation); 15 16 CComPtr<IUIAutomationElement> root; 17 automation->GetRootElement(&root); 18 19 CComPtr<IUIAutomationTreeWalker> walker; 20 automation->get_RawViewWalker(&walker); 21 22 //デスクトップ配下からEdgeを探す。探し方はNameからEdgeを含むものでとりあえず探す 23 //正確に行いたい場合はほかのプロパティ等を調べて絞りこむ。 24 CComPtr<IUIAutomationElement> edge; 25 walker->GetFirstChildElement(root,&edge); 26 while (edge) { 27 CComBSTR bname; 28 edge->get_CurrentName(&bname); 29 if (bname.Length() > 0) { 30 std::wstring name(bname); 31 if (name.find(L"Microsoft Edge") != std::wstring::npos) { 32 break; 33 } 34 } 35 CComPtr<IUIAutomationElement> next; 36 walker->GetNextSiblingElement(edge, &next); 37 edge.Release(); 38 edge = next; 39 } 40 if (!edge) { 41 return 0; 42 } 43 44 45 //Edge配下のタイトルが取得できそうなエレメントを取得 46 CComPtr<IUIAutomationElement> m_tabContentDCompVisualElement; 47 { 48 CComPtr<IUIAutomationCondition> cond; 49 CComVariant automation_id(L"m_tabContentDCompVisualElement"); 50 51 automation->CreatePropertyCondition(UIA_AutomationIdPropertyId, automation_id, &cond); 52 HRESULT hr= edge->FindFirst(TreeScope::TreeScope_Subtree, cond, &m_tabContentDCompVisualElement); 53 if (FAILED(hr)) { 54 return 0; 55 } 56 } 57 58 if (!m_tabContentDCompVisualElement) { 59 return 0; 60 } 61 62 //タイトル 63 CComBSTR title; 64 m_tabContentDCompVisualElement->get_CurrentName(&title); 65 ::OutputDebugString((LPWSTR)title); 66 ::OutputDebugString(L"\r\n"); 67 68 //Edge配下のURLが取得できそうなエレメントを取得 69 CComPtr<IUIAutomationElement> ie_server; 70 { 71 CComPtr<IUIAutomationCondition> cond; 72 CComVariant classname(L"Internet Explorer_Server"); 73 74 automation->CreatePropertyCondition(UIA_ClassNamePropertyId, classname, &cond); 75 HRESULT hr = edge->FindFirst(TreeScope::TreeScope_Subtree, cond, &ie_server); 76 if (FAILED(hr)) { 77 return 0; 78 } 79 } 80 81 //URL 82 CComBSTR url; 83 ie_server->get_CurrentName(&url); 84 ::OutputDebugString((LPWSTR)url); 85 ::OutputDebugString(L"\r\n"); 86 87 ::CoUninitialize(); 88 return 0; 89} 90 91

C#版を追加しました。
参照の追加からCOMを選んでUIAUtomationClientをチェックしてください。
COM

C#

1using System; 2using UIAutomationClient; 3 4namespace ConsoleApp1 5{ 6 class Program 7 { 8 [STAThread] 9 static void Main(string[] args) 10 { 11 string CLSID_CUIAutomation = "ff48dba4-60ef-4201-aa87-54103eef594e"; 12 Type type = Type.GetTypeFromCLSID(Guid.Parse(CLSID_CUIAutomation)); 13 IUIAutomation automation = Activator.CreateInstance(type) as IUIAutomation; 14 15 IUIAutomationElement root = automation.GetRootElement(); 16 IUIAutomationTreeWalker walker = automation.RawViewWalker; 17 18 IUIAutomationElement edge = null; 19 edge = walker.GetFirstChildElement(root); 20 while (edge != null) 21 { 22 string name = edge.CurrentName; 23 if(!string.IsNullOrEmpty(name) && name .EndsWith(" ‎- Microsoft Edge")) 24 { 25 break; 26 } 27 IUIAutomationElement next= walker.GetNextSiblingElement(edge); 28 edge = next; 29 } 30 if (edge == null) 31 { 32 return; 33 } 34 35 //Edge配下のタイトルが取得できそうなエレメントを取得 36 IUIAutomationElement m_tabContentDCompVisualElement = null; 37 { 38 const int UIA_AutomationIdPropertyId = 30011; 39 var cond = automation.CreatePropertyCondition(UIA_AutomationIdPropertyId, 40 "m_tabContentDCompVisualElement"); 41 m_tabContentDCompVisualElement = edge.FindFirst(TreeScope.TreeScope_Subtree, cond); 42 } 43 44 if (m_tabContentDCompVisualElement == null) 45 { 46 return; 47 } 48 49 //タイトル 50 string title = m_tabContentDCompVisualElement.CurrentName; 51 52 //Edge配下のURLが取得できそうなエレメントを取得 53 IUIAutomationElement ie_server; 54 { 55 const int UIA_ClassNamePropertyId = 30012; 56 var cond = automation.CreatePropertyCondition(UIA_ClassNamePropertyId, "Internet Explorer_Server"); 57 ie_server = edge.FindFirst(TreeScope.TreeScope_Subtree, cond); 58 59 } 60 61 if (ie_server == null) 62 { 63 return; 64 } 65 66 //URL 67 string url = ie_server.CurrentName; 68 69 Console.WriteLine(title); 70 Console.WriteLine(url); 71 Console.ReadKey(); 72 } 73 74 } 75} 76

投稿2017/08/20 12:21

編集2017/08/22 13:07
hmmm

総合スコア818

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

cancat

2017/08/20 14:22

ありがとうございます。 当方、残念ながらC++は読めず、せっかくのご回答ですが、参考にできずにおります。
cancat

2017/08/23 14:17

ありがとうございました。実装して検証しました。 うまくいきました。 C#のコードをいただいて感謝します。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問