Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- IUIAutomationElement rootElement = uiAutomation.GetRootElement();
- int propertyName = 30005; // UIA_NamePropertyId
- int propertyAutomationId = 30011; // UIA_AutomationIdPropertyId
- int propertyClassName = 30012; // UIA_ClassNamePropertyId
- int propertyNativeWindowHandle = 30020; // UIA_NativeWindowHandlePropertyId
- // Get the main Edge element, which is a direct child of the UIA root element.
- // For this test, assume that the Edge element is the only element with an
- // AutomationId of "TitleBar".
- string edgeAutomationId = "TitleBar";
- IUIAutomationCondition condition =
- uiAutomation.CreatePropertyCondition(
- propertyAutomationId, edgeAutomationId);
- // Have the window handle cached when we find the main Edge element.
- IUIAutomationCacheRequest cacheRequestNativeWindowHandle = uiAutomation.CreateCacheRequest();
- cacheRequestNativeWindowHandle.AddProperty(propertyNativeWindowHandle);
- IUIAutomationElement edgeElement =
- rootElement.FindFirstBuildCache(
- TreeScope.TreeScope_Children,
- condition,
- cacheRequestNativeWindowHandle);
- if (edgeElement != null)
- {
- IntPtr edgeWindowHandle = edgeElement.CachedNativeWindowHandle;
- // Next find the element whose name is the url of the loaded page. And have
- // the name of the element related to the url cached when we find the element.
- IUIAutomationCacheRequest cacheRequest =
- uiAutomation.CreateCacheRequest();
- cacheRequest.AddProperty(propertyName);
- // For this test, assume that the element with the url is the first descendant element
- // with a ClassName of "Internet Explorer_Server".
- string urlElementClassName = "Internet Explorer_Server";
- IUIAutomationCondition conditionUrl =
- uiAutomation.CreatePropertyCondition(
- propertyClassName,
- urlElementClassName);
- IUIAutomationElement urlElement =
- edgeElement.FindFirstBuildCache(
- TreeScope.TreeScope_Descendants,
- conditionUrl,
- cacheRequest);
- string url = urlElement.CachedName;
- // Next find the title of the loaded page. First find the list of
- // tabs shown at the top of Edge.
- string tabsListAutomationId = "TabsList";
- IUIAutomationCondition conditionTabsList =
- uiAutomation.CreatePropertyCondition(
- propertyAutomationId, tabsListAutomationId);
- IUIAutomationElement tabsListElement =
- edgeElement.FindFirst(
- TreeScope.TreeScope_Descendants,
- conditionTabsList);
- // Find which of those tabs is selected. (It should be possible to
- // cache the Selection pattern with the above call, and that would
- // avoid one cross-process call here.)
- int selectionPatternId = 10001; // UIA_SelectionPatternId
- IUIAutomationSelectionPattern selectionPattern =
- tabsListElement.GetCurrentPattern(selectionPatternId);
- // For this test, assume there's always one selected item in the list.
- IUIAutomationElementArray elementArray = selectionPattern.GetCurrentSelection();
- string title = elementArray.GetElement(0).CurrentName;
- // Now show the title, url and window handle.
- MessageBox.Show(
- "Page title: " + title +
- "rnURL: " + url +
- "rnhwnd: " + edgeWindowHandle);
- }
- class Program
- {
- [DllImport("user32.dll")]
- private static extern IntPtr GetForegroundWindow();
- static bool TryGetMSEdgeUrlAndTitle(IntPtr edgeWindow, out string url, out string title)
- {
- const int UIA_NamePropertyId = 30005;
- const int UIA_ClassNamePropertyId = 30012;
- const int UIA_NativeWindowHandlePropertyId = 30020;
- url = "";
- title = "";
- IUIAutomation uiA = new CUIAutomation();
- IUIAutomationElement rootElement = uiA.GetRootElement();
- IUIAutomationCacheRequest cacheRequest = uiA.CreateCacheRequest();
- cacheRequest.AddProperty(UIA_NamePropertyId);
- IUIAutomationCondition windowCondition = uiA.CreatePropertyCondition(UIA_NativeWindowHandlePropertyId, GetForegroundWindow());
- IUIAutomationElement windowElement = rootElement.FindFirstBuildCache(TreeScope.TreeScope_Descendants, windowCondition, cacheRequest);
- if (windowElement == null)
- return false;
- IUIAutomationCondition edgeCondition = uiA.CreatePropertyCondition(UIA_NamePropertyId, "Microsoft Edge");
- IUIAutomationElement edgeElement = windowElement.FindFirstBuildCache(TreeScope.TreeScope_Subtree, edgeCondition, cacheRequest);
- if (edgeElement == null)
- return false;
- IUIAutomationCondition tabCondition = uiA.CreatePropertyCondition(UIA_ClassNamePropertyId, "TabWindowClass");
- IUIAutomationElement tabElement = edgeElement.FindFirstBuildCache(TreeScope.TreeScope_Descendants, tabCondition, cacheRequest);
- if (tabElement == null)
- return false;
- IUIAutomationCondition ieCondition = uiA.CreatePropertyCondition(UIA_ClassNamePropertyId, "Internet Explorer_Server");
- IUIAutomationElement ieElement = tabElement.FindFirstBuildCache(TreeScope.TreeScope_Descendants, ieCondition, cacheRequest);
- if (ieElement == null)
- return false;
- url = ieElement.CachedName;
- title = tabElement.CachedName;
- return true;
- }
- static void Main(string[] args)
- {
- string oldUrl = "";
- string oldTitle = "";
- while (true)
- {
- string url = "";
- string title = "";
- if (TryGetMSEdgeUrlAndTitle(GetForegroundWindow(), out url, out title))
- {
- if ((url != oldUrl) || (title != oldTitle))
- {
- Console.WriteLine(String.Format("Page title: {0} rnURL: {1}", title, url));
- oldUrl = url;
- oldTitle = title;
- }
- }
- Thread.Sleep(250);
- }
- }
- }
- IUIAutomationElement urlElement =
- edgeElement.FindFirstBuildCache(
- TreeScope.TreeScope_Descendants,
- conditionUrl,
- cacheRequest);
- if(urlElement == null)//true
- '-----------------------------------------------------------------------------
- 'Allow code to get Microsoft Edge URL & Title
- ' Add .Net references for UIAutomationClient & UIAutomationTypes
- Imports System.Windows.Automation
- '-----------------------------------------------------------------------------
- Public Function ActiveMicrosoftEdgeTitleAndURL(ByRef HadError As Boolean,
- ByVal InhibitMsgBox As Boolean) As String()
- Dim i1 As Integer
- Dim tmp1 As String = "", tmp2() As String, METitle As String, MEURL As String
- Dim strME As String = "Microsoft Edge"
- 'ActiveMicrosoftEdgeTitleAndURL(Index) = Page Title or "No Title" + Chr(255) + Page URL
- 'If no Page URL then any Page Title is ignored.
- ' If the form is minimized to the taskbar the url is typically not available.
- HadError = False : ReDim tmp2(-1) : i1 = -1
- Try
- Dim conditions As Condition = Condition.TrueCondition
- Dim BaseElement As AutomationElement = AutomationElement.RootElement
- Dim elementCollection As AutomationElementCollection = BaseElement.FindAll(TreeScope.Children, conditions)
- Dim AE As AutomationElement
- For Each AE In elementCollection
- If AE IsNot Nothing Then
- tmp1 = AE.GetCurrentPropertyValue(AutomationElement.NameProperty).ToString
- If StrComp(Strings.Right(tmp1, strME.Length), strME, vbTextCompare) = 0 Then
- MEURL = "" : METitle = ""
- '-----------------------------------------------------------------------------------------------------------
- Dim AE1 As AutomationElement = _
- AE.FindFirst(TreeScope.Subtree, New PropertyCondition(AutomationElement.AutomationIdProperty, "TitleBar"))
- METitle = AutomationElementText(AE1)
- METitle = Trim(METitle)
- '-----------------------------------------------------------------------------------------------------------
- AE1 = AE.FindFirst(TreeScope.Subtree, New PropertyCondition(AutomationElement.AutomationIdProperty, "addressEditBox"))
- MEURL = AutomationElementText(AE1)
- MEURL = Trim(MEURL)
- '-----------------------------------------------------------------------------------------------------------
- If MEURL <> "" Then
- If METitle = "" Then METitle = "No Title"
- i1 = i1 + 1 : Array.Resize(tmp2, i1 + 1)
- tmp2(i1) = METitle + Chr(255) + MEURL
- End If
- End If
- End If
- Next
- Catch ex As Exception
- HadError = True
- MsgBox("Function AutomationElementData system error." + vbCr + vbCr + ex.ToString, vbExclamation)
- End Try
- Return tmp2
- End Function
- Private Function AutomationElementText(ByRef AE As AutomationElement) As String
- Dim MyPattern As AutomationPattern = ValuePattern.Pattern
- Dim MyPattern1 As AutomationPattern = TextPattern.Pattern
- Dim objPattern As Object = Nothing
- Dim txt As String = ""
- 'Any error just return a null string. !r
- If AE.TryGetCurrentPattern(MyPattern, objPattern) Then
- Dim AEValuePattern As ValuePattern = AE.GetCurrentPattern(MyPattern)
- txt = AEValuePattern.Current.Value
- Else
- If AE.TryGetCurrentPattern(MyPattern1, objPattern) Then
- Dim AETextPattern As TextPattern = AE.GetCurrentPattern(MyPattern1)
- txt = AETextPattern.DocumentRange.GetText(-1)
- End If
- End If
- Return txt
- End Function
- Try
- Dim conditions As Condition = Condition.TrueCondition
- Dim BaseElement As AutomationElement = AutomationElement.RootElement
- Dim elementCollection As AutomationElementCollection = BaseElement.FindAll(TreeScope.Children, conditions)
- Dim AE As AutomationElement
- For Each AE In elementCollection
- If AE IsNot Nothing Then
- tmp1 = AE.GetCurrentPropertyValue(AutomationElement.NameProperty).ToString
- If StrComp(Strings.Right(tmp1, strME.Length), strME, vbTextCompare) = 0 Then
- Dim elmUrlBar0 As AutomationElement
- elmUrlBar0 = AE.FindFirst(TreeScope.Subtree, New PropertyCondition(AutomationElement.AutomationIdProperty, "addressEditBox"))
- If elmUrlBar0 IsNot Nothing Then
- Dim patterns0 As AutomationPattern() = elmUrlBar0.GetSupportedPatterns()
- If patterns0.Length > 0 Then
- Dim val As ValuePattern = DirectCast(elmUrlBar0.GetCurrentPattern(patterns0(0)), ValuePattern)
- If Not elmUrlBar0.GetCurrentPropertyValue(AutomationElement.HasKeyboardFocusProperty) Then
- Dim pvgc As String = LCase(val.Current.Value).Trim
- If pvgc.ToString <> "" Then
- urls = pvgc.ToString
- MsgBox(urls)
- End If
- End If
- End If
- End If
- End If
- End If
- Next
- Catch ex As Exception
- MsgBox("Function AutomationElementData system error." + vbCr + vbCr + ex.ToString, vbExclamation)
- End Try
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement