I'm having problems trying to identify via Windows UI Automation the button controls that are inside the Notification Area window (classname: ToolbarWindow32):
I verified via the Windows UI Automation tools deployed in the Windows SDK that those "icons" are controls of type ControlType.Button
, however when I try to run the code below I get a null-reference exception because the search condition I use doesn't get any control.
I'm doing something wrong, or maybe I found some kind of limitation in Windows UI Automation ?
This is the code, I mixed it with WinAPI calls just to facilitate the task for the helper users who maybe preffers to use that methodology.
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.
Any solution for this?
You are correct somewhat. Technically they are not in ToolbarWindow32, rather Shell_TrayWnd. I inspected the area and found out, those buttons are actually in a
ToolBar
, so you need to look for theControlType.ToolBar
. Next you need toFindAll
which will return all AutomationElements that would satisfy thePropertyCondition
...Note: the first loop is to get the User Promoted Notification Area. The next loop for fun is getting the Running Application buttons... (CODE IS WORKING ON WIN7, WIN8 and WIN10)
Here in my example below, I go after the
Shell_TrayWnd
which will get us what we would need. Then I go through and find whatToolBar
we are after, then loop through andFindAll
ControlTypes ofButton
...If you have any questions, please let me know. Image below (some things I commented out for secure reasons)