Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.36 KB | None | 0 0
  1. IUIAutomationElement rootElement = uiAutomation.GetRootElement();
  2.  
  3. int propertyName = 30005; // UIA_NamePropertyId
  4. int propertyAutomationId = 30011; // UIA_AutomationIdPropertyId
  5. int propertyClassName = 30012; // UIA_ClassNamePropertyId
  6. int propertyNativeWindowHandle = 30020; // UIA_NativeWindowHandlePropertyId
  7.  
  8. // Get the main Edge element, which is a direct child of the UIA root element.
  9. // For this test, assume that the Edge element is the only element with an
  10. // AutomationId of "TitleBar".
  11. string edgeAutomationId = "TitleBar";
  12.  
  13. IUIAutomationCondition condition =
  14. uiAutomation.CreatePropertyCondition(
  15. propertyAutomationId, edgeAutomationId);
  16.  
  17. // Have the window handle cached when we find the main Edge element.
  18. IUIAutomationCacheRequest cacheRequestNativeWindowHandle = uiAutomation.CreateCacheRequest();
  19. cacheRequestNativeWindowHandle.AddProperty(propertyNativeWindowHandle);
  20.  
  21. IUIAutomationElement edgeElement =
  22. rootElement.FindFirstBuildCache(
  23. TreeScope.TreeScope_Children,
  24. condition,
  25. cacheRequestNativeWindowHandle);
  26.  
  27. if (edgeElement != null)
  28. {
  29. IntPtr edgeWindowHandle = edgeElement.CachedNativeWindowHandle;
  30.  
  31. // Next find the element whose name is the url of the loaded page. And have
  32. // the name of the element related to the url cached when we find the element.
  33. IUIAutomationCacheRequest cacheRequest =
  34. uiAutomation.CreateCacheRequest();
  35. cacheRequest.AddProperty(propertyName);
  36.  
  37. // For this test, assume that the element with the url is the first descendant element
  38. // with a ClassName of "Internet Explorer_Server".
  39. string urlElementClassName = "Internet Explorer_Server";
  40.  
  41. IUIAutomationCondition conditionUrl =
  42. uiAutomation.CreatePropertyCondition(
  43. propertyClassName,
  44. urlElementClassName);
  45.  
  46. IUIAutomationElement urlElement =
  47. edgeElement.FindFirstBuildCache(
  48. TreeScope.TreeScope_Descendants,
  49. conditionUrl,
  50. cacheRequest);
  51.  
  52. string url = urlElement.CachedName;
  53.  
  54. // Next find the title of the loaded page. First find the list of
  55. // tabs shown at the top of Edge.
  56. string tabsListAutomationId = "TabsList";
  57.  
  58. IUIAutomationCondition conditionTabsList =
  59. uiAutomation.CreatePropertyCondition(
  60. propertyAutomationId, tabsListAutomationId);
  61.  
  62. IUIAutomationElement tabsListElement =
  63. edgeElement.FindFirst(
  64. TreeScope.TreeScope_Descendants,
  65. conditionTabsList);
  66.  
  67. // Find which of those tabs is selected. (It should be possible to
  68. // cache the Selection pattern with the above call, and that would
  69. // avoid one cross-process call here.)
  70. int selectionPatternId = 10001; // UIA_SelectionPatternId
  71. IUIAutomationSelectionPattern selectionPattern =
  72. tabsListElement.GetCurrentPattern(selectionPatternId);
  73.  
  74. // For this test, assume there's always one selected item in the list.
  75. IUIAutomationElementArray elementArray = selectionPattern.GetCurrentSelection();
  76. string title = elementArray.GetElement(0).CurrentName;
  77.  
  78. // Now show the title, url and window handle.
  79. MessageBox.Show(
  80. "Page title: " + title +
  81. "rnURL: " + url +
  82. "rnhwnd: " + edgeWindowHandle);
  83. }
  84.  
  85. class Program
  86. {
  87. [DllImport("user32.dll")]
  88. private static extern IntPtr GetForegroundWindow();
  89.  
  90. static bool TryGetMSEdgeUrlAndTitle(IntPtr edgeWindow, out string url, out string title)
  91. {
  92. const int UIA_NamePropertyId = 30005;
  93. const int UIA_ClassNamePropertyId = 30012;
  94. const int UIA_NativeWindowHandlePropertyId = 30020;
  95.  
  96. url = "";
  97. title = "";
  98.  
  99. IUIAutomation uiA = new CUIAutomation();
  100. IUIAutomationElement rootElement = uiA.GetRootElement();
  101.  
  102. IUIAutomationCacheRequest cacheRequest = uiA.CreateCacheRequest();
  103. cacheRequest.AddProperty(UIA_NamePropertyId);
  104.  
  105. IUIAutomationCondition windowCondition = uiA.CreatePropertyCondition(UIA_NativeWindowHandlePropertyId, GetForegroundWindow());
  106. IUIAutomationElement windowElement = rootElement.FindFirstBuildCache(TreeScope.TreeScope_Descendants, windowCondition, cacheRequest);
  107. if (windowElement == null)
  108. return false;
  109.  
  110. IUIAutomationCondition edgeCondition = uiA.CreatePropertyCondition(UIA_NamePropertyId, "Microsoft Edge");
  111. IUIAutomationElement edgeElement = windowElement.FindFirstBuildCache(TreeScope.TreeScope_Subtree, edgeCondition, cacheRequest);
  112. if (edgeElement == null)
  113. return false;
  114.  
  115. IUIAutomationCondition tabCondition = uiA.CreatePropertyCondition(UIA_ClassNamePropertyId, "TabWindowClass");
  116. IUIAutomationElement tabElement = edgeElement.FindFirstBuildCache(TreeScope.TreeScope_Descendants, tabCondition, cacheRequest);
  117. if (tabElement == null)
  118. return false;
  119.  
  120. IUIAutomationCondition ieCondition = uiA.CreatePropertyCondition(UIA_ClassNamePropertyId, "Internet Explorer_Server");
  121. IUIAutomationElement ieElement = tabElement.FindFirstBuildCache(TreeScope.TreeScope_Descendants, ieCondition, cacheRequest);
  122. if (ieElement == null)
  123. return false;
  124.  
  125. url = ieElement.CachedName;
  126. title = tabElement.CachedName;
  127.  
  128. return true;
  129. }
  130.  
  131. static void Main(string[] args)
  132. {
  133. string oldUrl = "";
  134. string oldTitle = "";
  135.  
  136. while (true)
  137. {
  138. string url = "";
  139. string title = "";
  140.  
  141. if (TryGetMSEdgeUrlAndTitle(GetForegroundWindow(), out url, out title))
  142. {
  143. if ((url != oldUrl) || (title != oldTitle))
  144. {
  145. Console.WriteLine(String.Format("Page title: {0} rnURL: {1}", title, url));
  146.  
  147. oldUrl = url;
  148. oldTitle = title;
  149. }
  150. }
  151.  
  152. Thread.Sleep(250);
  153. }
  154. }
  155. }
  156.  
  157. IUIAutomationElement urlElement =
  158. edgeElement.FindFirstBuildCache(
  159. TreeScope.TreeScope_Descendants,
  160. conditionUrl,
  161. cacheRequest);
  162.  
  163. if(urlElement == null)//true
  164.  
  165. '-----------------------------------------------------------------------------
  166. 'Allow code to get Microsoft Edge URL & Title
  167. ' Add .Net references for UIAutomationClient & UIAutomationTypes
  168. Imports System.Windows.Automation
  169. '-----------------------------------------------------------------------------
  170.  
  171. Public Function ActiveMicrosoftEdgeTitleAndURL(ByRef HadError As Boolean,
  172. ByVal InhibitMsgBox As Boolean) As String()
  173.  
  174. Dim i1 As Integer
  175. Dim tmp1 As String = "", tmp2() As String, METitle As String, MEURL As String
  176. Dim strME As String = "Microsoft Edge"
  177.  
  178. 'ActiveMicrosoftEdgeTitleAndURL(Index) = Page Title or "No Title" + Chr(255) + Page URL
  179.  
  180. 'If no Page URL then any Page Title is ignored.
  181. ' If the form is minimized to the taskbar the url is typically not available.
  182.  
  183. HadError = False : ReDim tmp2(-1) : i1 = -1
  184.  
  185. Try
  186. Dim conditions As Condition = Condition.TrueCondition
  187. Dim BaseElement As AutomationElement = AutomationElement.RootElement
  188. Dim elementCollection As AutomationElementCollection = BaseElement.FindAll(TreeScope.Children, conditions)
  189. Dim AE As AutomationElement
  190. For Each AE In elementCollection
  191. If AE IsNot Nothing Then
  192. tmp1 = AE.GetCurrentPropertyValue(AutomationElement.NameProperty).ToString
  193. If StrComp(Strings.Right(tmp1, strME.Length), strME, vbTextCompare) = 0 Then
  194. MEURL = "" : METitle = ""
  195. '-----------------------------------------------------------------------------------------------------------
  196. Dim AE1 As AutomationElement = _
  197. AE.FindFirst(TreeScope.Subtree, New PropertyCondition(AutomationElement.AutomationIdProperty, "TitleBar"))
  198. METitle = AutomationElementText(AE1)
  199. METitle = Trim(METitle)
  200. '-----------------------------------------------------------------------------------------------------------
  201. AE1 = AE.FindFirst(TreeScope.Subtree, New PropertyCondition(AutomationElement.AutomationIdProperty, "addressEditBox"))
  202. MEURL = AutomationElementText(AE1)
  203. MEURL = Trim(MEURL)
  204. '-----------------------------------------------------------------------------------------------------------
  205. If MEURL <> "" Then
  206. If METitle = "" Then METitle = "No Title"
  207. i1 = i1 + 1 : Array.Resize(tmp2, i1 + 1)
  208. tmp2(i1) = METitle + Chr(255) + MEURL
  209. End If
  210. End If
  211. End If
  212. Next
  213. Catch ex As Exception
  214. HadError = True
  215. MsgBox("Function AutomationElementData system error." + vbCr + vbCr + ex.ToString, vbExclamation)
  216. End Try
  217.  
  218. Return tmp2
  219.  
  220. End Function
  221.  
  222. Private Function AutomationElementText(ByRef AE As AutomationElement) As String
  223.  
  224. Dim MyPattern As AutomationPattern = ValuePattern.Pattern
  225. Dim MyPattern1 As AutomationPattern = TextPattern.Pattern
  226. Dim objPattern As Object = Nothing
  227. Dim txt As String = ""
  228.  
  229. 'Any error just return a null string. !r
  230.  
  231. If AE.TryGetCurrentPattern(MyPattern, objPattern) Then
  232. Dim AEValuePattern As ValuePattern = AE.GetCurrentPattern(MyPattern)
  233. txt = AEValuePattern.Current.Value
  234. Else
  235. If AE.TryGetCurrentPattern(MyPattern1, objPattern) Then
  236. Dim AETextPattern As TextPattern = AE.GetCurrentPattern(MyPattern1)
  237. txt = AETextPattern.DocumentRange.GetText(-1)
  238. End If
  239. End If
  240.  
  241. Return txt
  242.  
  243. End Function
  244.  
  245. Try
  246. Dim conditions As Condition = Condition.TrueCondition
  247. Dim BaseElement As AutomationElement = AutomationElement.RootElement
  248. Dim elementCollection As AutomationElementCollection = BaseElement.FindAll(TreeScope.Children, conditions)
  249. Dim AE As AutomationElement
  250.  
  251. For Each AE In elementCollection
  252. If AE IsNot Nothing Then
  253. tmp1 = AE.GetCurrentPropertyValue(AutomationElement.NameProperty).ToString
  254. If StrComp(Strings.Right(tmp1, strME.Length), strME, vbTextCompare) = 0 Then
  255. Dim elmUrlBar0 As AutomationElement
  256. elmUrlBar0 = AE.FindFirst(TreeScope.Subtree, New PropertyCondition(AutomationElement.AutomationIdProperty, "addressEditBox"))
  257. If elmUrlBar0 IsNot Nothing Then
  258. Dim patterns0 As AutomationPattern() = elmUrlBar0.GetSupportedPatterns()
  259. If patterns0.Length > 0 Then
  260. Dim val As ValuePattern = DirectCast(elmUrlBar0.GetCurrentPattern(patterns0(0)), ValuePattern)
  261. If Not elmUrlBar0.GetCurrentPropertyValue(AutomationElement.HasKeyboardFocusProperty) Then
  262. Dim pvgc As String = LCase(val.Current.Value).Trim
  263. If pvgc.ToString <> "" Then
  264. urls = pvgc.ToString
  265. MsgBox(urls)
  266. End If
  267. End If
  268. End If
  269. End If
  270. End If
  271. End If
  272. Next
  273.  
  274. Catch ex As Exception
  275. MsgBox("Function AutomationElementData system error." + vbCr + vbCr + ex.ToString, vbExclamation)
  276. End Try
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement