windows之UIAutomation简介

来源:互联网 发布:淘宝旺旺权重值查询 编辑:程序博客网 时间:2024/06/06 09:04

1、简介

UIAutomation微软提供的UI自动化库,主要用AutomationElement类来表示UI 自动化目录树中的一个UI自动化元素,.NET Windows的窗体应用程序和WPF应用程序

2、体系

这里写图片描述

  • 在服务端由UIAutomationProvider.dll和UIAutomationTypes.dll提供
  • 在客户端由UIAutomationClient.dll和UIAutomationTypes.dll提供
  • UIAutomationCore.dll为UI自动化的核心部分,负责Server端和Client端的交互
  • UIAUtomationClientSideProvides.dll为客户端程序提供自动化支持

3、UI自动化流程

项目中一定要添加引用:
这里写图片描述
使用UISpy查看控件属性值
这里写图片描述

Process p = Process.Start(@"C:\Windows\system32\calc.exe");//启动应用程序Thread.Sleep(2000);AutomationElement desktop = AutomationElement.RootElement;//获取RootElementAutomationElement calcframe = desktop.FindFirst(TreeScope.Descendants | TreeScope.Children,    new PropertyCondition(AutomationElement.NameProperty, "计算器"));//获取应用程序AutomationElement sevenbtn = calcframe.FindFirst(TreeScope.Descendants | TreeScope.Children,    new PropertyCondition(AutomationElement.NameProperty, "7"));//获取控件InvokePattern ivkp = (InvokePattern)sevenbtn.GetCurrentPattern(InvokePattern.Pattern);ivkp.Invoke(); //触发控件事件

执行效果:
这里写图片描述

注:PropertyCondition类是用来对相关属性进行条件匹配,在控件树中查找控件时,可以通过最佳匹配来找到相应的控件

4、UIAutomation重要属性

AutomationIdProperty:通过AutomationId来查找AutomationElement

NameProperty:通过控件的Name属性来查找AutomationElement

ControlType:通过控件的类型来查找AutomationElement

AutomationId: 唯一地标识自动化元素,将其与同级相区分

Name: WPF 按钮的Content 属性、Win32 按钮的Caption 属性以及 HTML 图像的ALT 属性都映射到 UI 自动化视图中的同一个属性Name

5、TreeScope

Element:指定搜索包括元素本身。

Children:指定搜索包括元素的直接子级。

Descendants:指定搜索包括元素的子代(包括子级)。

Parent:指定搜索包括元素的父级。不支持。

Ancestors:指定搜索包括元素的上级

Subtree:指定搜索包括搜索的根和全部子代。