在C#或VB.Net 中,我如何使用Microsoft UI 自动化来检索包含文本的任何控件的文本?。
我一直在 MSDN 文档中进行研究,但我不明白。
然后,例如,使用下面的代码,我试图通过提供该窗口的 hwnd 来检索窗口标题栏的文本,但我不知道如何按照标题栏找到子控件(标签?)这确实包含文本。
Imports System.Windows.Automation
Imports System.Windows.Automation.Text
Run Code Online (Sandbox Code Playgroud)
.
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)
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,我正在尝试使用标题栏,但只是我想使用任何其他包含文本的控件来执行此操作……例如标题栏。
PS:我知道 P/Invoking GetWindowTextAPI。