Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Invoke a Control Using UI Automation

Note Note

This documentation is intended for .NET Framework developers who want to use the managed UI Automation classes defined in the System.Windows.Automation namespace. For the latest information about UI Automation, see Windows Automation API: UI Automation.

This topic demonstrates how to perform the following tasks:

  • Find a control that matches specific property conditions by walking the control view of the UI Automation tree for the target application.

  • Create an AutomationElement for each control.

  • Obtain an InvokePattern object from any UI Automation element found that supports the InvokePattern control pattern.

  • Use Invoke to invoke the control from a client event handler.

This example uses the TryGetCurrentPattern method of the AutomationElement class to generate an InvokePattern object and invoke a control by using the Invoke method.

        ''' <summary> 
        ''' Walks the UI Automation tree of the target and reports the control  
        ''' type of each element it finds in the control view to the client. 
        ''' </summary> 
        ''' <param name="targetTreeViewElement"> 
        ''' The root of the search on this iteration. 
        ''' </param> 
        ''' <param name="treeviewIndex"> 
        ''' The TreeView index for this iteration. 
        ''' </param> 
        ''' <remarks> 
        ''' This is a recursive function that maps out the structure of the  
        ''' subtree beginning at the AutomationElement passed in as  
        ''' rootElement on the first call. This could be, for example, 
        ''' an application window. 
        ''' CAUTION: Do not pass in AutomationElement.RootElement. Attempting  
        ''' to map out the entire subtree of the desktop could take a very  
        ''' long time and even lead to a stack overflow. 
        ''' </remarks> 
        Private Sub FindTreeViewDescendants( _
        ByVal targetTreeViewElement As AutomationElement, _
        ByVal treeviewIndex As Integer)
            If (IsNothing(targetTreeViewElement)) Then 
            End If 

            Dim elementNode As AutomationElement = _

            While Not (elementNode Is Nothing)
                Dim elementInfo As New Label()
                elementInfo.Margin = New Thickness(0)

                ' Compile information about the control.
                elementInfoCompile = New StringBuilder()
                Dim controlName As String 
                If (elementNode.Current.Name = "") Then
                    controlName = "Unnamed control" 
                    controlName = elementNode.Current.Name
                End If 
                Dim autoIdName As String 
                If (elementNode.Current.AutomationId = "") Then
                    autoIdName = "No AutomationID" 
                    autoIdName = elementNode.Current.AutomationId
                End If

                elementInfoCompile.Append(controlName).Append(" (") _
                .Append(elementNode.Current.ControlType.LocalizedControlType) _
                .Append(" - ").Append(autoIdName).Append(")")

                ' Test for the control patterns of interest for this sample. 
                Dim objPattern As Object = Nothing 
                Dim expcolPattern As ExpandCollapsePattern
                If True = elementNode.TryGetCurrentPattern(ExpandCollapsePattern.Pattern, objPattern) Then
                    expcolPattern = DirectCast(objPattern, ExpandCollapsePattern)
                    If expcolPattern.Current.ExpandCollapseState <> ExpandCollapseState.LeafNode Then 
                        Dim expcolButton As New Button()
                        expcolButton.Margin = New Thickness(0, 0, 0, 5)
                        expcolButton.Height = 20
                        expcolButton.Width = 100
                        expcolButton.Content = "ExpandCollapse"
                        expcolButton.Tag = expcolPattern
                        AddHandler expcolButton.Click, AddressOf ExpandCollapse_Click
                    End If 
                End If 
                Dim togPattern As TogglePattern
                If True = elementNode.TryGetCurrentPattern(TogglePattern.Pattern, objPattern) Then
                    togPattern = DirectCast(objPattern, TogglePattern)
                    Dim togButton As New Button()
                    togButton.Margin = New Thickness(0, 0, 0, 5)
                    togButton.Height = 20
                    togButton.Width = 100
                    togButton.Content = "Toggle"
                    togButton.Tag = togPattern
                    AddHandler togButton.Click, AddressOf Toggle_Click
                End If 
                Dim invPattern As InvokePattern
                If True = elementNode.TryGetCurrentPattern(InvokePattern.Pattern, objPattern) Then
                    invPattern = DirectCast(objPattern, InvokePattern)
                    Dim invButton As New Button()
                    invButton.Margin = New Thickness(0)
                    invButton.Height = 20
                    invButton.Width = 100
                    invButton.Content = "Invoke"
                    invButton.Tag = invPattern
                    AddHandler invButton.Click, AddressOf Invoke_Click
                End If 
                ' Display compiled information about the control.
                elementInfo.Content = elementInfoCompile
                Dim sep As New Separator()

                ' Iterate to next element. 
                ' elementNode - Current element. 
                ' treeviewIndex - Index of parent TreeView.
                FindTreeViewDescendants(elementNode, treeviewIndex)
                elementNode = TreeWalker.ControlViewWalker.GetNextSibling(elementNode)
            End While 

        End Sub 'FindTreeViewDescendants


        ''' <summary> 
        ''' Handles the Invoke click event on the client control.  
        ''' The client click handler calls Invoke() on the equivalent target control. 
        ''' </summary> 
        ''' <param name="sender">The object that raised the event.</param> 
        ''' <param name="e">Event arguments.</param> 
        ''' <remarks> 
        ''' The Tag property of the FrameworkElement, the client button in this  
        ''' case, is used to store the InvokePattern object previously obtained  
        ''' from the associated target control. 
        ''' </remarks> 
        Private Sub Invoke_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            Dim clientButton As Button = DirectCast(sender, Button)
            Dim targetInvokePattern As InvokePattern = _
            DirectCast(clientButton.Tag, InvokePattern)
            If (IsNothing(targetInvokePattern)) Then 
            End If
            statusText.Text = "Button invoked." 
        End Sub 'Invoke_Click
© 2015 Microsoft