Vb.net 如何为UI自动化树指定树的根节点

Vb.net 如何为UI自动化树指定树的根节点,vb.net,user-interface,automation,Vb.net,User Interface,Automation,我看到了一些使用VB.NET解析UI自动化树的示例代码。代码如下: '' <summary> ''' Walks the UI Automation tree and adds the control type of each element it finds ''' in the control view to a TreeView. ''' </summary> ''' <param name="rootElement">The root of

我看到了一些使用VB.NET解析UI自动化树的示例代码。代码如下:

'' <summary> 
''' Walks the UI Automation tree and adds the control type of each element it finds  
''' in the control view 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 WalkControlElements(ByVal rootElement As AutomationElement, ByVal treeNode As TreeNode)
    ' Conditions for the basic views of the subtree (content, control, and raw)  
    ' are available as fields of TreeWalker, and one of these is used in the  
    ' following code. 

    Dim elementNode As AutomationElement = TreeWalker.ControlViewWalker.GetFirstChild(rootElement)

    While (elementNode IsNot Nothing)
        Dim childTreeNode As TreeNode = treeNode.Nodes.Add(elementNode.Current.ControlType.LocalizedControlType)
        WalkControlElements(elementNode, childTreeNode)
        elementNode = TreeWalker.ControlViewWalker.GetNextSibling(elementNode)
    End While 

End Sub 'WalkControlElements
“”
''遍历UI自动化树并添加它找到的每个元素的控件类型
控件视图中的“”指向树视图。
'''  
''此迭代上搜索的根。
''此迭代的树视图中的节点。
'''  
''这是一个递归函数,从
''UI自动化元素在第一次调用时作为rootElement传入。例如,这可能是,
''一个应用程序窗口。
''注意:不要传入AutomationElement.RootElement。正在尝试映射的整个子树
''桌面可能需要很长时间,甚至会导致堆栈溢出。
'''  
私有子WalkControlElements(ByVal rootElement作为AutomationElement,ByVal treeNode作为treeNode)
'子树基本视图的条件(内容、控件和原始)
'作为TreeWalker的字段提供,其中一个用于
"下面的代码。
Dim elementNode作为AutomationElement=TreeWalker.ControlViewWalker.GetFirstChild(根元素)
While(elementNode不是空的)
Dim childTreeNode As TreeNode=TreeNode.Nodes.Add(elementNode.Current.ControlType.LocalizedControlType)
WalkControlElements(elementNode、childTreeNode)
elementNode=TreeWalker.ControlViewWalker.GetNextSibling(elementNode)
结束时
“结束子元素”WalkControlElements

如果我想使用它,我必须传入rootElement。我想知道这个的语法是什么?使用UISpy,我可以看到类名是“WindowsForm10.Window.8.app.0.378734a”

AutomationElement.RootElement
将为您提供AutomationElement树的根目录,即桌面。如果要获取不同的元素,可以创建一个
条件
对象,然后调用
AutomationElement.RootElement.FindFirst
AutomationElement.RootElement.FindAll
,具体取决于您在此处要执行的操作