مشاركة عبر


التنقل بين واجه المستخدم التنفيذ التلقائي عناصر مع TreeWalker

ملاحظةملاحظة

Th هو الوثائق هو المقصودة لإطار عمل.NET المطورين الذين يرغبون في استخدام المدارة UI Automationالفئات المعرفة في System.Windows.Automationمساحة الاسم.للحصول على أحدث المعلومات حول UI Automation، راجع API التنفيذ التلقائي Windows: أتمتة واجه المستخدم.

هذا إلى صورة تحتوي على التعليمة البرمجية في المثال يظهر كيف إلى التنقل بين Microsoft UI Automationالعناصر باستخدام TreeWalkerفئة.

مثال

يستخدم المثال التالي GetParentإلى تقود Microsoft UI Automationشجرة حتى يتم العثور على العنصر الجذر، أو deskإلىp. العنصر أسفل الذي هو الإطار الأصل للعنصر المحدد.

    ''' <summary>
    ''' Retrieves the top-level window that contains the specified UI Automation element.
    ''' </summary>
    ''' <param name="element">The contained element.</param>
    ''' <returns>The containing top-level window element.</returns>
    Private Function GetTopLevelWindow(ByVal element As AutomationElement) As AutomationElement 
        Dim walker As TreeWalker = TreeWalker.ControlViewWalker
        Dim elementParent As AutomationElement
        Dim node As AutomationElement = element
        If node = elementRoot Then
            Return node
        End If
        Do
            elementParent = walker.GetParent(node)
            If elementParent = AutomationElement.RootElement Then
                Exit Do
            End If
            node = elementParent
        Loop While True
        Return node

    End Function 'GetTopLevelWindow
End Class 'Reader 
/// <summary>
/// Retrieves the top-level window that contains the specified UI Automation element.
/// </summary>
/// <param name="element">The contained element.</param>
/// <returns>The containing top-level window element.</returns>
private AutomationElement GetTopLevelWindow(AutomationElement element)
{
    TreeWalker walker = TreeWalker.ControlViewWalker;
    AutomationElement elementParent;
    AutomationElement node = element;
    if (node == elementRoot) return node;
    do
    {
        elementParent = walker.GetParent(node);
        if (elementParent == AutomationElement.RootElement) break;
        node = elementParent;
    }
    while (true);
    return node;
}

يستخدم المثال التالي GetFirstChildو GetNextSiblingلإنشاء TreeViewالذي يظهر الشجرة فرعية بأكمله من Microsoft UI Automationالعناصر الموجودة في عنصر تحكم تمكين طريقة العرض والتي.

    ''' <summary>
    ''' Walks the UI Automation tree and adds the control type of each enabled control 
    ''' element it finds to a TreeView.
    ''' </summary>
    ''' <param name="rootElement">The root of the search on this iteration.</param>
    ''' <param name="treeNode">The node in the TreeView for this iteration.</param>
    ''' <remarks>
    ''' This is a recursive function that maps out the structure of the subtree beginning at the
    ''' UI Automation element 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 WalkEnabledElements(ByVal rootElement As AutomationElement, ByVal treeNode As TreeNode)
        Dim condition1 As New PropertyCondition(AutomationElement.IsControlElementProperty, True)
        Dim condition2 As New PropertyCondition(AutomationElement.IsEnabledProperty, True)
        Dim walker As New TreeWalker(New AndCondition(condition1, condition2))
        Dim elementNode As AutomationElement = walker.GetFirstChild(rootElement)
        While (elementNode IsNot Nothing)
            Dim childTreeNode As TreeNode = treeNode.Nodes.Add(elementNode.Current.ControlType.LocalizedControlType)
            WalkEnabledElements(elementNode, childTreeNode)
            elementNode = walker.GetNextSibling(elementNode)
        End While

    End Sub 'WalkEnabledElements

/// <summary>
/// Walks the UI Automation tree and adds the control type of each enabled control 
/// element it finds to a TreeView.
/// </summary>
/// <param name="rootElement">The root of the search on this iteration.</param>
/// <param name="treeNode">The node in the TreeView for this iteration.</param>
/// <remarks>
/// This is a recursive function that maps out the structure of the subtree beginning at the
/// UI Automation element 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 void WalkEnabledElements(AutomationElement rootElement, TreeNode treeNode)
{
    Condition condition1 = new PropertyCondition(AutomationElement.IsControlElementProperty, true);
    Condition condition2 = new PropertyCondition(AutomationElement.IsEnabledProperty, true);
    TreeWalker walker = new TreeWalker(new AndCondition(condition1, condition2));
    AutomationElement elementNode = walker.GetFirstChild(rootElement);
    while (elementNode != null)
    {
        TreeNode childTreeNode = treeNode.Nodes.Add(elementNode.Current.ControlType.LocalizedControlType);
        WalkEnabledElements(elementNode, childTreeNode);
        elementNode = walker.GetNextSibling(elementNode);
    }
}

راجع أيضًا:

المبادئ

الحصول على واجه المستخدم التنفيذ التلقائي عناصر