UI 자동화를 사용하여 컨트롤 호출

참고 항목

이 설명서는 System.Windows.Automation 네임스페이스에 정의된 관리되는 UI 자동화 클래스를 사용하려는 .NET Framework 개발자를 위한 것입니다. UI 자동화에 대한 최신 정보는 Windows 자동화 API: UI 자동화를 참조하세요.

이 항목에서는 다음 작업을 수행하는 방법을 보여 줍니다.

  • 대상 애플리케이션에 대해 UI 자동화 트리의 컨트롤 뷰를 검색하여 특정 속성 조건과 일치하는 컨트롤을 찾습니다.

  • 각 컨트롤에 대해 AutomationElement 를 만듭니다.

  • InvokePattern 컨트롤 패턴을 지원하는 검색된 모든 UI 자동화 요소에서 InvokePattern 개체를 가져옵니다.

  • Invoke 를 사용하여 클라이언트 이벤트 처리기에서 컨트롤을 호출합니다.

예시

이 예제에서는 TryGetCurrentPattern 클래스의 AutomationElement 메서드를 사용하여 InvokePattern 개체를 생성하고 Invoke 메서드를 사용하여 컨트롤을 호출합니다.

///--------------------------------------------------------------------
/// <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="elementIndex">
/// The TreeView index for this iteration.
/// </param>
/// <remarks>
/// This is a recursive function that maps out the structure of the
/// subtree of the target beginning at the AutomationElement passed in
/// as the 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 void FindTreeViewDescendants(
    AutomationElement targetTreeViewElement, int treeviewIndex)
{
    if (targetTreeViewElement == null)
        return;

    AutomationElement elementNode =
        TreeWalker.ControlViewWalker.GetFirstChild(targetTreeViewElement);

    while (elementNode != null)
    {
        Label elementInfo = new Label();
        elementInfo.Margin = new Thickness(0);
        clientTreeViews[treeviewIndex].Children.Add(elementInfo);

        // Compile information about the control.
        elementInfoCompile = new StringBuilder();
        string controlName =
            (elementNode.Current.Name == "") ?
            "Unnamed control" : elementNode.Current.Name;
        string autoIdName =
            (elementNode.Current.AutomationId == "") ?
            "No AutomationID" : elementNode.Current.AutomationId;

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

        // Test for the control patterns of interest for this sample.
        object objPattern;
        ExpandCollapsePattern expcolPattern;
        if (true == elementNode.TryGetCurrentPattern(ExpandCollapsePattern.Pattern, out objPattern))
        {
            expcolPattern = objPattern as ExpandCollapsePattern;
            if (expcolPattern.Current.ExpandCollapseState != ExpandCollapseState.LeafNode)
            {
                Button expcolButton = new Button();
                expcolButton.Margin = new Thickness(0, 0, 0, 5);
                expcolButton.Height = 20;
                expcolButton.Width = 100;
                expcolButton.Content = "ExpandCollapse";
                expcolButton.Tag = expcolPattern;
                expcolButton.Click +=
                    new RoutedEventHandler(ExpandCollapse_Click);
                clientTreeViews[treeviewIndex].Children.Add(expcolButton);
            }
        }
        TogglePattern togPattern;
        if (true == elementNode.TryGetCurrentPattern(TogglePattern.Pattern, out objPattern))
        {
            togPattern = objPattern as TogglePattern;
            Button togButton = new Button();
            togButton.Margin = new Thickness(0, 0, 0, 5);
            togButton.Height = 20;
            togButton.Width = 100;
            togButton.Content = "Toggle";
            togButton.Tag = togPattern;
            togButton.Click += new RoutedEventHandler(Toggle_Click);
            clientTreeViews[treeviewIndex].Children.Add(togButton);
        }
        InvokePattern invPattern;
        if (true == elementNode.TryGetCurrentPattern(InvokePattern.Pattern, out objPattern))
        {
            invPattern = objPattern as InvokePattern;
            Button invButton = new Button();
            invButton.Margin = new Thickness(0);
            invButton.Height = 20;
            invButton.Width = 100;
            invButton.Content = "Invoke";
            invButton.Tag = invPattern;
            invButton.Click += new RoutedEventHandler(Invoke_Click);
            clientTreeViews[treeviewIndex].Children.Add(invButton);
        }
        // Display compiled information about the control.
        elementInfo.Content = elementInfoCompile;
        Separator sep = new Separator();
        clientTreeViews[treeviewIndex].Children.Add(sep);

        // Iterate to next element.
        // elementNode - Current element.
        // treeviewIndex - Index of parent TreeView.
        FindTreeViewDescendants(elementNode, treeviewIndex);
        elementNode =
            TreeWalker.ControlViewWalker.GetNextSibling(elementNode);
    }
}
'''--------------------------------------------------------------------
''' <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
        Return
    End If

    Dim elementNode As AutomationElement = _
    TreeWalker.ControlViewWalker.GetFirstChild(targetTreeViewElement)

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

        ' Compile information about the control.
        elementInfoCompile = New StringBuilder()
        Dim controlName As String
        If (elementNode.Current.Name = "") Then
            controlName = "Unnamed control"
        Else
            controlName = elementNode.Current.Name
        End If
        Dim autoIdName As String
        If (elementNode.Current.AutomationId = "") Then
            autoIdName = "No AutomationID"
        Else
            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
                clientTreeViews(treeviewIndex).Children.Add(expcolButton)
            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
            clientTreeViews(treeviewIndex).Children.Add(togButton)
        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
            clientTreeViews(treeviewIndex).Children.Add(invButton)
        End If
        ' Display compiled information about the control.
        elementInfo.Content = elementInfoCompile
        Dim sep As New Separator()
        clientTreeViews(treeviewIndex).Children.Add(sep)

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

End Sub

///--------------------------------------------------------------------
/// <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>
///--------------------------------------------------------------------
void Invoke_Click(object sender, RoutedEventArgs e)
{
    Button clientButton = sender as Button;
    InvokePattern targetInvokePattern = clientButton.Tag as InvokePattern;
    if (targetInvokePattern == null)
        return;
    targetInvokePattern.Invoke();
    statusText.Text = "Button invoked.";
}
'''--------------------------------------------------------------------
''' <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
        Return
    End If
    targetInvokePattern.Invoke()
    statusText.Text = "Button invoked."
End Sub

참고 항목