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

时间:2016-09-22 18:13:56

标签: .net vb.net ui-automation

我在尝试通过 Windows UI Automation 识别通知区域窗口内的按钮控件时遇到问题(类名: ToolbarWindow32 ):

enter image description here

我通过 Windows SDK 中部署的 Windows UI自动化工具验证了这些"图标"是类型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.

对此有何解决方案?

1 个答案:

答案 0 :(得分:3)

  

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

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

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

在下面的示例中,我会查看Shell_TrayWnd,它将为我们提供我们所需要的功能。然后我会查看我们所追求的ToolBar,然后循环浏览FindAll Button的{​​{1}} ...

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

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

enter image description here