frameworke3.0 及以上
using System.Windows.Automation;
UIAutomationClient.dll
UIAutomationClientsideProviders.dll C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\UIAutomationClientsideProviders.dll
UIAutomationProvider.dll
UIAutomationTypes.dll
下面的示例演示如何使用 FindAll 查找窗口中的所有已启用的按钮。
/// <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 自动化线程处理问题。 |
本主题包括下列各节。
- 根元素
- 条件
- 搜索范围
- 查找已知元素
- 查找子树中的元素
- 浏览子树
- 检索元素的其他方法
- 相关主题
根元素
所有 AutomationElement 对象搜索必须具有一个起点。此起点可以是任何元素,包括桌面、应用程序窗口或控件。
所有元素所起源于的根元素是从静态的 AutomationElement..::.RootElement 属性中获得的。
警告: |
---|
通常,您应当尝试只获取 RootElement 的直接子项。对子代的搜索可能循环访问数百个甚至数千个元素,从而可能导致堆栈溢出。如果尝试在较低级别获取特定元素,您应当从应用程序窗口或位于较低级别的容器中开始搜索。 |
条件
对于用于检索 UI 自动化元素的大多数方法,您必须指定 Condition,这是一组用于定义要检索的元素的条件。
最简单的条件是 TrueCondition,这是一个指定要返回搜索范围内的所有元素的预定义对象。FalseCondition 是 TrueCondition 的对立条件,其作用不大,因为它将阻止找到任何元素。
下面是三个其他的预定义条件,这些条件既可以单独使用,也可以与其他条件一起使用:ContentViewCondition、ControlViewCondition 和 RawViewCondition。单独使用的 RawViewCondition 等效于 TrueCondition,因为它不根据元素的 IsControlElement 或 IsContentElement 属性来筛选元素。
其他条件是根据一个或多个 PropertyC