Windows UI 自动化无法识别按钮控件

2024-01-02

我在尝试通过以下方式识别时遇到问题Windows 用户界面自动化里面的按钮控件通知区窗口(类名:工具栏Window32):

我通过验证Windows 用户界面自动化部署在的工具视窗软件开发工具包这些“图标”是类型控件ControlType.Button,但是当我尝试运行下面的代码时,我收到空引用异常,因为我使用的搜索条件没有得到任何控制。

我做错了什么,或者我发现了某种限制Windows 用户界面自动化 ?

这是代码,我将其与 WinAPI 调用混合在一起,只是为了方便可能更喜欢使用该方法的帮助用户完成任务。

Dim tskBarClassName As String = "Shell_TrayWnd"
Dim tskBarHwnd As IntPtr = NativeMethods.FindWindow(tskBarClassName, Nothing)

Dim systrayBarClassName As String = "TrayNotifyWnd"
Dim systrayBarHwnd As IntPtr = NativeMethods.FindWindowEx(tskBarHwnd, IntPtr.Zero, systrayBarClassName, Nothing)

Dim ntfyBarClassName As String = "ToolbarWindow32"
Dim ntfyBarHwnd As IntPtr = NativeMethods.FindWindowEx(systrayBarHwnd, IntPtr.Zero, ntfyBarClassName, Nothing)

Dim window As AutomationElement = AutomationElement.FromHandle(ntfyBarHwnd)
Dim condition As New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button)
Dim button As AutomationElement = window.FindFirst(TreeScope.Descendants, condition)

MsgBox(button.Current.Name) ' Here throws the null-reference exception.

有什么解决办法吗?


我通过 Windows SDK 中部署的 Windows UI 自动化工具验证这些“图标”是 ControlType.Button 类型的控件

你是对的somewhat。从技术上讲,他们是不在 ToolbarWindow32 中,而是在 Shell_TrayWnd 中。我检查了该区域,发现这些按钮实际上位于一个ToolBar,所以你需要寻找ControlType.ToolBar。接下来你需要FindAll它将返回满足以下条件的所有 AutomationElementsPropertyCondition...

注意:第一个循环是获取用户升级通知区域。下一个有趣的循环是获取正在运行的应用程序按钮...(代码适用于 WIN7、WIN8 和 WIN10)

在下面的示例中,我追求Shell_TrayWnd这将为我们提供我们需要的东西。然后我浏览并找到什么ToolBar我们在后面,然后循环并FindAll控制类型Button...

Dim arrText As New List(Of String)
        Dim tskBarClassName As String = "Shell_TrayWnd"
        Dim tskBarHwnd As IntPtr = FindWindow(tskBarClassName, Nothing)
        Dim window As AutomationElement = AutomationElement.FromHandle(tskBarHwnd)
        Dim condition As New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ToolBar)
        Dim elementCollection As AutomationElementCollection = window.FindAll(TreeScope.Descendants, condition)

        'for fun get all we can...
        For Each aE As AutomationElement In elementCollection
            If aE.Current.Name.Equals("User Promoted Notification Area") Then
                For Each ui As AutomationElement In aE.FindAll(TreeScope.Descendants, New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button))
                    arrText.Add("Notification Area - " & Replace(ui.Current.HelpText, Chr(10), " "c)) 'removed line break as when shown it would show some on a new line in messagebox
                Next
            ElseIf aE.Current.Name.Equals("Running applications") Then
                For Each ui As AutomationElement In aE.FindAll(TreeScope.Descendants, New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button))
                    arrText.Add("Toolbar Area - " & Replace(ui.Current.Name, Chr(10), " "c)) 'removed line break as when shown it would show some on a new line in messagebox
                Next
            End If

        Next

If arrText.Count > 0 Then
            MessageBox.Show(String.Join(Environment.NewLine, arrText.ToArray))
        End If

如果您有任何疑问,请告诉我。下图(出于安全原因我注释掉了一些内容)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Windows UI 自动化无法识别按钮控件 的相关文章

随机推荐