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

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

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

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

VB.NET

Microsoft Visual Basic .NETのことで、Microsoft Visual Basic(VB6)の後継。 .NET環境向けのプログラムを開発することができます。 現在のVB.NETでは、.NET Frameworkを利用して開発を行うことが可能です。

Q&A

解決済

1回答

11092閲覧

起動中のMicrosoft EdgeからタイトルとURLを取得したい(C# or VB.NET)

Ante-Mode

総合スコア6

C#

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

VB.NET

Microsoft Visual Basic .NETのことで、Microsoft Visual Basic(VB6)の後継。 .NET環境向けのプログラムを開発することができます。 現在のVB.NETでは、.NET Frameworkを利用して開発を行うことが可能です。

0グッド

0クリップ

投稿2018/04/06 02:41

編集2018/04/06 02:43

前提・実現したいこと

お世話になります。

本ソースはこちらを参考にWindowsフォームで検証中です。
https://teratail.com/questions/88664

発生している問題・エラーメッセージ

①タブの情報が取得できない ②Edgeが最小化されている場合そもそも取得できない

該当のソースコード

C#

1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Data; 5using System.Drawing; 6using System.Linq; 7using System.Text; 8using System.Threading.Tasks; 9using System.Windows.Forms; 10using UIAutomationClient; 11 12namespace GetEdgeUrl 13{ 14 public partial class frmMain : Form 15 { 16 public frmMain() 17 { 18 InitializeComponent(); 19 } 20 21 private void btnTest_Click(object sender, EventArgs e) 22 { 23 MessageBox.Show("Hello World!!", btnTest.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); 24 } 25 26 private void btnExecute_Click(object sender, EventArgs e) 27 { 28 GetTrialEdgeUrl02(); 29 30 } 31 32 private void GetTrialEdgeUrl02() 33 { 34 string CLSID_CUIAutomation = "ff48dba4-60ef-4201-aa87-54103eef594e"; 35 Type type = Type.GetTypeFromCLSID(Guid.Parse(CLSID_CUIAutomation)); 36 IUIAutomation automation = Activator.CreateInstance(type) as IUIAutomation; 37 38 IUIAutomationElement root = automation.GetRootElement(); 39 IUIAutomationTreeWalker walker = automation.RawViewWalker; 40 41 IUIAutomationElement edge = null; 42 //IUIAutomationElementArray edgeAry = null; 43 44 edge = walker.GetFirstChildElement(root); 45 while (edge != null) 46 { 47 string name = edge.CurrentName; 48 if (!string.IsNullOrEmpty(name) && name.EndsWith(" ‎- Microsoft Edge")) 49 { 50 //break; 51 //Edge配下のタイトルが取得できそうなエレメントを取得 52 IUIAutomationElement m_tabContentDCompVisualElement = null; 53 { 54 const int UIA_AutomationIdPropertyId = 30011; 55 var cond = automation.CreatePropertyCondition(UIA_AutomationIdPropertyId, 56 "m_tabContentDCompVisualElement"); 57 m_tabContentDCompVisualElement = edge.FindFirst(TreeScope.TreeScope_Subtree, cond); 58 59 } 60 61 if (m_tabContentDCompVisualElement == null) 62 { 63 //MessageBox.Show("Edgeが起動していません。", btnExecute.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); 64 return; 65 } 66 67 //タイトル 68 string title = m_tabContentDCompVisualElement.CurrentName; 69 70 //Edge配下のURLが取得できそうなエレメントを取得 71 IUIAutomationElement ie_server; 72 { 73 const int UIA_ClassNamePropertyId = 30012; 74 var cond = automation.CreatePropertyCondition(UIA_ClassNamePropertyId, "Internet Explorer_Server"); 75 ie_server = edge.FindFirst(TreeScope.TreeScope_Subtree, cond); 76 77 } 78 79 if (ie_server == null) 80 { 81 return; 82 } 83 84 //URL 85 string url = ie_server.CurrentName; 86 87 StringBuilder MsgLine = new StringBuilder(); 88 MsgLine.Append("Title:"); 89 MsgLine.AppendLine(title); 90 MsgLine.Append("Url:"); 91 MsgLine.AppendLine(url); 92 93 MessageBox.Show(MsgLine.ToString(), btnExecute.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); 94 95 } 96 IUIAutomationElement next = walker.GetNextSiblingElement(edge); 97 edge = next; 98 } 99 if (edge == null) 100 { 101 MessageBox.Show("Edgeが起動していません。", btnExecute.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); 102 return; 103 } 104 105 106 107 //Console.WriteLine(title); 108 //Console.WriteLine(url); 109 //Console.ReadKey(); 110 } 111 } 112}

試したこと

IUIAutomationElementArrayを使えばいいのかなと思ったのですが、
うまくいかず…

補足情報(FW/ツールのバージョンなど)

Microsoft Visual Studio Professional 2017
Version 15.6.5
Microsoft .NET Framework
Version 4.7.02556
Microsoft Edge 41.16299.248.0

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

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

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

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

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

guest

回答1

0

自己解決

下記で一旦解決しました。
※UIAutomationClientの利用を辞めました。

C#

1using System; 2using System.Text; 3using System.Windows.Forms; 4using System.Runtime.InteropServices; 5 6namespace GetEdgeUrl02 7{ 8 public partial class frmMain : Form 9 { 10 [Flags] 11 private enum SendMessageTimeoutFlags : uint 12 { 13 SMTO_NORMAL = 0x0000, 14 SMTO_BLOCK = 0x0001, 15 SMTO_ABORTIFHUNG = 0x0002, 16 SMTO_NOTIMEOUTIFNOTHUNG = 0x0008, 17 SMTO_ERRORONEXIT = 0x0020 18 } 19 private delegate bool Win32Callback(IntPtr hWnd, ref IntPtr lParam); 20 21 [DllImport("user32.dll")] 22 [return: MarshalAs(UnmanagedType.Bool)] 23 private static extern bool EnumWindows(Win32Callback lpEnumFunc, ref IntPtr lParam); 24 [DllImport("user32.dll", SetLastError = true)] 25 private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); 26 [DllImport("user32.dll", CharSet = CharSet.Auto)] 27 private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); 28 [DllImport("oleacc.dll", PreserveSig = false)] 29 [return: MarshalAs(UnmanagedType.Interface)] 30 private static extern object ObjectFromLresult(UIntPtr lResult, [MarshalAs(UnmanagedType.LPStruct)] Guid refiid, IntPtr wParam); 31 [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] 32 private static extern uint RegisterWindowMessage(string lpString); 33 [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] 34 private static extern IntPtr SendMessageTimeout(IntPtr windowHandle, uint Msg, IntPtr wParam, IntPtr lParam, SendMessageTimeoutFlags flags, uint timeout, out UIntPtr result); 35 // ■取得確認用フラグ 36 private bool NoneFlg; 37 38 public frmMain() 39 { 40 InitializeComponent(); 41 } 42 43 private void btnExecute_Click(object sender, EventArgs e) 44 { 45 this.NoneFlg = false; 46 IntPtr hWin = IntPtr.Zero; 47 Win32Callback proc = new Win32Callback(EnumWindowsProc); 48 EnumWindows(proc, ref hWin); 49 // ■取得しなかった場合 50 if (this.NoneFlg == false) { 51 MessageBox.Show("Edgeが起動していません。", this.btnExecute.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); 52 } 53 54 } 55 private bool EnumWindowsProc(IntPtr hWnd, ref IntPtr lParam) 56 { 57 IntPtr hChild = IntPtr.Zero; 58 StringBuilder buf = new StringBuilder(1024); 59 GetClassName(hWnd, buf, buf.Capacity); 60 if (buf.ToString() == "TabWindowClass") 61 { 62 //get 'Internet Explorer_Server' window 63 hChild = FindWindowEx(hWnd, IntPtr.Zero, "Internet Explorer_Server", ""); 64 if (hChild != IntPtr.Zero) 65 { 66 dynamic doc = null; 67 doc = GetHTMLDocumentFromWindow(hChild); 68 if (doc != null) 69 { 70 // ■メッセージボックスで返却 71 //Console.WriteLine(doc.Title + ", " + doc.url); //get document title & url 72 StringBuilder sb = new StringBuilder(); 73 sb.AppendLine("Title:" + doc.Title); 74 sb.AppendLine("Url:" + doc.url); 75 MessageBox.Show(sb.ToString(), "Ante-Mode", MessageBoxButtons.OK, MessageBoxIcon.Information); 76 this.NoneFlg = true; 77 } 78 } 79 } 80 return true; 81 } 82 83 //get HTMLDocument object 84 private static object GetHTMLDocumentFromWindow(IntPtr hWnd) 85 { 86 UIntPtr lRes; 87 object doc = null; 88 Guid IID_IHTMLDocument2 = new Guid("332C4425-26CB-11D0-B483-00C04FD90119"); 89 uint nMsg = RegisterWindowMessage("WM_HTML_GETOBJECT"); 90 if (nMsg != 0) 91 { 92 SendMessageTimeout(hWnd, nMsg, IntPtr.Zero, IntPtr.Zero, SendMessageTimeoutFlags.SMTO_ABORTIFHUNG, 1000, out lRes); 93 if (lRes != UIntPtr.Zero) 94 { 95 doc = ObjectFromLresult(lRes, IID_IHTMLDocument2, IntPtr.Zero); 96 } 97 } 98 return doc; 99 } 100 101 } 102} 103

投稿2018/04/06 09:01

編集2018/04/06 09:04
Ante-Mode

総合スコア6

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問