Windows UI Automation doesn't recognize button controls

前端 未结 1 1168
滥情空心
滥情空心 2021-02-20 06:20

I\'m having problems trying to identify via Windows UI Automation the button controls that are inside the Notification Area window (classname: ToolbarW

1条回答
  •  自闭症患者
    2021-02-20 06:36

    I verified via the Windows UI Automation tools deployed in the Windows SDK that those "icons" are controls of type ControlType.Button

    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 the ControlType.ToolBar. Next you need to FindAll which will return all AutomationElements that would satisfy the PropertyCondition...

    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 what ToolBar we are after, then loop through and FindAll ControlTypes of 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
    

    If you have any questions, please let me know. Image below (some things I commented out for secure reasons)

    0 讨论(0)
提交回复
热议问题