frameworke3.0 及以上html
using System.Windows.Automation;app
UIAutomationClient.dllide
UIAutomationClientsideProviders.dll C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\UIAutomationClientsideProviders.dll测试
UIAutomationProvider.dllui
UIAutomationTypes.dllthis
下面的示例演示如何使用 FindAll 查找窗口中的全部已启用的按钮。spa
/// <summary> /// Finds all enabled buttons in the specified window element. /// </summary> /// <param name="elementWindowElement">An application or dialog window.</param> /// <returns>A collection of elements that meet the conditions.</returns> AutomationElementCollection FindByMultipleConditions(AutomationElement elementWindowElement) { if (elementWindowElement == null) { throw new ArgumentException(); } Condition conditions = new AndCondition( new PropertyCondition(AutomationElement.IsEnabledProperty, true), new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button) ); // Find all children that match the specified conditions. AutomationElementCollection elementCollection = elementWindowElement.FindAll(TreeScope.Children, conditions); return elementCollection; }
本主题描述获取 用户界面 (UI) 元素的 AutomationElement 对象的各类方法。线程
![]() |
---|
若是客户端应用程序能够尝试在其本身的用户界面中查找元素,则必须在一个单独的线程上进行全部 UI 自动化调用。有关更多信息,请参见 UI 自动化线程处理问题。code |
本主题包括下列各节。orm
全部 AutomationElement 对象搜索必须具备一个起点。此起点能够是任何元素,包括桌面、应用程序窗口或控件。
全部元素所起源于的根元素是从静态的 AutomationElement..::.RootElement 属性中得到的。
![]() |
---|
一般,您应当尝试只获取 RootElement 的直接子项。对子代的搜索可能循环访问数百个甚至数千个元素,从而可能致使堆栈溢出。若是尝试在较低级别获取特定元素,您应当从应用程序窗口或位于较低级别的容器中开始搜索。 |
对于用于检索 UI 自动化元素的大多数方法,您必须指定 Condition,这是一组用于定义要检索的元素的条件。
最简单的条件是 TrueCondition,这是一个指定要返回搜索范围内的全部元素的预约义对象。FalseCondition 是 TrueCondition 的对立条件,其做用不大,由于它将阻止找到任何元素。
下面是三个其余的预约义条件,这些条件既能够单独使用,也能够与其余条件一块儿使用:ContentViewCondition、ControlViewCondition 和 RawViewCondition。单独使用的 RawViewCondition 等效于 TrueCondition,由于它不根据元素的 IsControlElement 或 IsContentElement 属性来筛选元素。
其余条件是根据一个或多个 PropertyCondition 对象(每一个对象都指定一个属性值)创建的。例如,PropertyCondition 能够指定元素处于启用状态或元素支持某种控件模式。
经过构建 AndCondition、OrCondition 和 NotCondition 类型的对象,能够用布尔逻辑将条件结合起来。
用 FindFirst 或 FindAll 执行的搜索必须具备一个范围和一个起点。
范围定义了要搜索的起点周围的空间。这能够包括元素自己、其同级项、父项、祖先、直接子项以及子代。
搜索范围由按位组合的 TreeScope 枚举值来定义。
若要查找由已知元素的 Name、AutomationId、某些其余属性或属性组合标识的已知元素,最简单的方法是使用 FindFirst 方法。若是查找的元素是一个应用程序窗口,则搜索的起点能够是 RootElement。
这种查找 UI 自动化 元素的方法在自动化测试方案中最有用。
若要查找与已知元素有关且知足特定条件的全部元素,您可使用 FindAll。例如,您可使用这种方法从列表或菜单中检索列表项或菜单项,或找出对话框中的全部控件。
若是您事先不了解可与客户端一块儿使用的应用程序,则可使用 TreeWalker 类创建一个包含感兴趣的全部元素的子树。您的应用程序可能会执行此操做以响应 focus-changed 事件;也就是说,在应用程序或控件接收输入焦点时,UI 自动化客户端将检查子项,可能还会检查有焦点的元素的全部子代。
使用 TreeWalker 的另外一种方式是识别元素的祖先。例如,经过向上浏览树,您能够识别控件的父窗口。
经过建立类的对象(用 Condition 定义感兴趣的元素),或者使用如下被定义为 TreeWalker 的字段的预约义对象之一,您可使用 TreeWalker。
ContentViewWalker |
只查找 IsContentElement 属性为 true 的元素。 |
ControlViewWalker |
只查找 IsControlElement 属性为 true 的元素。 |
RawViewWalker |
查找全部元素。 |
在得到了 TreeWalker 以后,能够直接使用它。只需调用 Get 方法即可在子树的元素中导航。
Normalize 方法可用于从视图外的另外一元素导航到子树中的某个元素。例如,假设您使用 ContentViewWalker 建立了一个子树视图。而后,您的应用程序收到通知,得知滚动条已经接收了输入焦点。由于滚动条不是内容元素,因此子树视图中未呈现滚动条。可是,您能够将表明滚动条的 AutomationElement 传递到 Normalize 并检索内容视图中最接近的祖先。
除了应用搜索和导航以外,您还能够经过如下方法来检索 AutomationElement。
当您的应用程序接收 UI 自动化事件时,传递到事件处理程序的源对象为 AutomationElement。例如,若是您订阅了 focus-changed 事件,则传递到 AutomationFocusChangedEventHandler 的源是收到焦点的元素。
有关更多信息,请参见订阅 UI 自动化事件。
若是您具备屏幕坐标(例如,光标位置),则可使用静态的 FromPoint 方法来检索 AutomationElement。
若要从 HWND 中检索 AutomationElement,请使用静态的 FromHandle 方法。
能够从静态的 FocusedElement 属性中检索表示焦点控件的 AutomationElement。
表示一个 Condition,其计算结果老是为 true。
命名空间: System.Windows.Automation
程序集: UIAutomationClient(在 UIAutomationClient.dll 中)
Visual Basic(声明) |
---|
Public Shared ReadOnly TrueCondition As Condition |
Visual Basic(用法) |
---|
Dim value As Condition value = Condition.TrueCondition |
C# |
---|
public static readonly Condition TrueCondition |
Visual C++ |
---|
public: static initonly Condition^ TrueCondition |
J# |
---|
public static final Condition TrueCondition |
JScript |
---|
public static final var TrueCondition : Condition |
在下面的示例中,使用 TrueCondition 检索在指定范围内的所有 UI 自动化元素。
C# | ![]() |
---|---|
/// <summary> /// Examples of using predefined conditions to find elements. /// </summary> /// <param name="elementMainWindow">The element for the target window.</param> public void StaticConditionExamples(AutomationElement elementMainWindow) { if (elementMainWindow == null) { throw new ArgumentException(); } // Use TrueCondition to retrieve all elements. AutomationElementCollection elementCollectionAll = elementMainWindow.FindAll( TreeScope.Subtree, Condition.TrueCondition); Console.WriteLine("\nAll control types:"); foreach (AutomationElement autoElement in elementCollectionAll) { Console.WriteLine(autoElement.Current.Name); } // Use ContentViewCondition to retrieve all content elements. AutomationElementCollection elementCollectionContent = elementMainWindow.FindAll( TreeScope.Subtree, Automation.ContentViewCondition); Console.WriteLine("\nAll content elements:"); foreach (AutomationElement autoElement in elementCollectionContent) { Console.WriteLine(autoElement.Current.Name); } // Use ControlViewCondition to retrieve all control elements. AutomationElementCollection elementCollectionControl = elementMainWindow.FindAll( TreeScope.Subtree, Automation.ControlViewCondition); Console.WriteLine("\nAll control elements:"); foreach (AutomationElement autoElement in elementCollectionControl) { Console.WriteLine(autoElement.Current.Name); } } |
Visual Basic | ![]() |
---|---|
''' <summary> ''' Examples of using predefined conditions to find elements. ''' </summary> ''' <param name="elementMainWindow">The element for the target window.</param> Public Sub StaticConditionExamples(ByVal elementMainWindow As AutomationElement) If elementMainWindow Is Nothing Then Throw New ArgumentException() End If ' Use TrueCondition to retrieve all elements. Dim elementCollectionAll As AutomationElementCollection = elementMainWindow.FindAll(TreeScope.Subtree, Condition.TrueCondition) Console.WriteLine(vbLf + "All control types:") Dim autoElement As AutomationElement For Each autoElement In elementCollectionAll Console.WriteLine(autoElement.Current.Name) Next autoElement ' Use ContentViewCondition to retrieve all content elements. Dim elementCollectionContent As AutomationElementCollection = elementMainWindow.FindAll(TreeScope.Subtree, Automation.ContentViewCondition) Console.WriteLine(vbLf + "All content elements:") For Each autoElement In elementCollectionContent Console.WriteLine(autoElement.Current.Name) Next autoElement ' Use ControlViewCondition to retrieve all control elements. Dim elementCollectionControl As AutomationElementCollection = elementMainWindow.FindAll(TreeScope.Subtree, Automation.ControlViewCondition) Console.WriteLine(vbLf & "All control elements:") For Each autoElement In elementCollectionControl Console.WriteLine(autoElement.Current.Name) Next autoElement End Sub 'StaticConditionExamples |
Windows Vista
.NET Framework 和 .NET Compact Framework 并非对每一个平台的全部版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。