繁体   English   中英

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

[英]Windows UI Automation doesn't recognize button controls

我在尝试通过Windows UI Automation识别Notification Area窗口(classname: ToolbarWindow32 )中的按钮控件时遇到问题:

在此输入图像描述

我通过Windows SDK中部署的Windows UI自动化工具验证了这些“图标”是ControlType.Button类型的ControlType.Button ,但是当我尝试运行下面的代码时,我得到一个空引用异常,因为我使用的搜索条件没有得到任何控制。

我做错了,或者我在Windows UI Automation中发现了某些限制?

这是代码,我将它与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类型的控件

有点正确。 从技术上讲,它们不在ToolbarWindow32中,而是在Shell_TrayWnd中 我检查了该区域并发现,这些按钮实际上位于ToolBar ,因此您需要查找ControlType.ToolBar 接下来你需要FindAll ,它将返回满足PropertyCondition所有AutomationElements ...

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

在下面的示例中,我将介绍Shell_TrayWnd ,它将为我们提供所需的功能。 然后我通过找到我们正在使用的ToolBar ,然后循环浏览Button FindAll ControlTypes ...

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

如果您有任何疑问,请告诉我。 下面的图片(出于安全原因我注释了一些事情)

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM