我正在尝试通过 Windows UI Automation 识别位于 通知区域 窗口(类名:ToolbarWindow32)内部的按钮控件,并遇到了问题:
我通过部署在 Windows SDK 中的 Windows UI Automation 工具验证了这些“图标”是 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.
有解决方案吗?