In C# or else VB.Net, how I could use Microsoft UI Automation to retrieve the text of any control that contains text?.
I've been researching in the MSDN Docs, but I don't get it.
Obtain Text Attributes Using UI Automation
Then, for example, with the code below I'm trying to retrieve the text of the Window titlebar by giving the hwnd of that window, but I don't know exactlly how to follow the titlebar to find the child control (label?) that really contains the text.
Imports System.Windows.Automation
Imports System.Windows.Automation.Text
.
Dim hwnd As IntPtr = Process.GetProcessesByName("notepad").First.MainWindowHandle
Dim targetApp As AutomationElement = AutomationElement.FromHandle(hwnd)
' The control type we're looking for; in this case 'TitleBar'
Dim cond1 As New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TitleBar)
Dim targetTextElement As AutomationElement =
targetApp.FindFirst(TreeScope.Descendants, cond1)
Debug.WriteLine(targetTextElement Is Nothing)
In the example above I'm trying with the titlebar, but just I would like to do it with any other control that contains text ...like a titlebar.
PS: I'm aware of P/Invoking GetWindowText
API.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…