Client-Side UI Automation Provider -  WinForm Sample

2014-09-15

源代码

目录

引用程序集
实现提供程序接口
分发客户端提供程序
注册和配置客户端提供程序
WinForm Sample
参考

引用程序集[1]


返回

  • UIAutomationClient.dll
  • UIAutomationProviders.dll
  • UIAutomationTypes.dll

实现提供程序接口[2]


返回

以下示列实现提供程序接口:IRawElementProviderSimple

     class WindowProvider : IRawElementProviderSimple
{
IntPtr providerHwnd;
public WindowProvider(IntPtr hwnd)
{
providerHwnd = hwnd;
}
internal static IRawElementProviderSimple Create(
IntPtr hwnd, int idChild, int idObject)
{
System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("WinFormServer");
if (processes.Length == )
return null;
//Check if the target is specified process
//If yes, create and run provider instance
if (processes[].MainWindowHandle != hwnd)
return null;
else
return new WindowProvider(hwnd);
}
ProviderOptions IRawElementProviderSimple.ProviderOptions
{
get
{
//Return ClientSideProvider as the implementation is in client
return ProviderOptions.ClientSideProvider;
}
}
IRawElementProviderSimple IRawElementProviderSimple.HostRawElementProvider
{
get
{
return AutomationInteropProvider.HostProviderFromHandle(providerHwnd);
}
}
object IRawElementProviderSimple.GetPropertyValue(int aid)
{
if (AutomationProperty.LookupById(aid) ==
AutomationElementIdentifiers.NameProperty)
{
//Our UIA.Name property implementation
//In production code, usually it uses Win32 or MSAA to get real information
return "UIA Client Implementation";
}
else
{
return null;
}
}
object IRawElementProviderSimple.GetPatternProvider(int iid)
{
//Return null means it does not support any Pattern Provider
return null;
}
}

分发客户端提供程序[1][2]


返回

UI 自动化应在托管代码程序集中查找客户端提供程序。该程序集中的命名空间应与该程序集同名。例如,称为UIAClientProvider.dll 的程序集应包含UIAClientProvider命名空间。 在该命名空间内创建 UIAutomationClientSideProviders 类。 在静态 ClientSideProviderDescriptionTable 字段的实现中,创建用于描述该提供程序的 ClientSideProviderDescription 结构数组。

ClientSideProviderDescription 结构数组将指定以下属性:

  • 一个回调函数,用于创建提供程序对象。
  • 提供程序支持的控件的类名。
  • 提供程序支持的应用程序的映像名(通常是可执行文件的全名)。
  • 控制如何根据目标应用程序中的窗口类对类名进行匹配的标志。

最后两个参数是可选的。  如果客户端希望对不同的应用程序使用不同的提供程序,则可以指定目标应用程序的映像名。 例如,客户端可以对支持多视图模式的已知应用程序中的 Win32 列表视图控件使用一个提供程序,而对不支持多视图模式的另一个已知应用程序中的类似控件使用另一个提供程序

     class UIAutomationClientSideProviders
{
public static ClientSideProviderDescription[] ClientSideProviderDescriptionTable =
{ new ClientSideProviderDescription(
WindowProvider.Create,
null) };
}

注册和配置客户端提供程序[1][2]


返回

动态链接库 (DLL) 中的客户端提供程序通过调用 RegisterClientSideProviderAssembly 进行加载。  无需进一步的操作,客户端应用程序便可以使用这些提供程序。

在客户端自己的代码中实现的提供程序通过使用 RegisterClientSideProviders 进行注册。  此方法将 ClientSideProviderDescription 结构数组用作参数。

ClientSettings.RegisterClientSideProviders(UIAutomationClientSideProviders.ClientSideProviderDescriptionTable);

WinForm Sample[2]


返回

创建Console Application:UIAClientProvider,代码如下:

 using System;
using System.Windows.Automation.Provider;
using System.Windows.Automation; namespace UIAClientProvider
{
class Program
{
static void Main(string[] args)
{
ClientProviderSample.ClientProviderTest();
}
} public class ClientProviderSample
{
public static void ClientProviderTest()
{
//Calling RegisterClientSideProviders API to register out Client provider
//Client Provider’s type information is stored in ClientSideProviderDescriptionTable Array
ClientSettings.RegisterClientSideProviders(UIAutomationClientSideProviders.ClientSideProviderDescriptionTable);
//Obtain main window of test target
System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("WinFormServer");
if (processes.Length == )
return;
IntPtr hwnd = processes[].MainWindowHandle;
//Get UIA object of the test target main window
AutomationElement elementWindow = AutomationElement.FromHandle(hwnd);
//Read UIA.Name property
Console.WriteLine(elementWindow.Current.Name);
Console.WriteLine();
Console.WriteLine("Press any key to exit.");
Console.ReadLine();
}
}
class UIAutomationClientSideProviders
{
public static ClientSideProviderDescription[] ClientSideProviderDescriptionTable =
{ new ClientSideProviderDescription(
WindowProvider.Create,
null) };
}
class WindowProvider : IRawElementProviderSimple
{
IntPtr providerHwnd;
public WindowProvider(IntPtr hwnd)
{
providerHwnd = hwnd;
}
internal static IRawElementProviderSimple Create(
IntPtr hwnd, int idChild, int idObject)
{
System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("WinFormServer");
if (processes.Length == )
return null;
//Check if the target is specified process
//If yes, create and run provider instance
if (processes[].MainWindowHandle != hwnd)
return null;
else
return new WindowProvider(hwnd);
}
ProviderOptions IRawElementProviderSimple.ProviderOptions
{
get
{
//Return ClientSideProvider as the implementation is in client
return ProviderOptions.ClientSideProvider;
}
}
IRawElementProviderSimple IRawElementProviderSimple.HostRawElementProvider
{
get
{
return AutomationInteropProvider.HostProviderFromHandle(providerHwnd);
}
}
object IRawElementProviderSimple.GetPropertyValue(int aid)
{
if (AutomationProperty.LookupById(aid) ==
AutomationElementIdentifiers.NameProperty)
{
//Our UIA.Name property implementation
//In production code, usually it uses Win32 or MSAA to get real information
return "UIA Client Implementation";
}
else
{
return null;
}
}
object IRawElementProviderSimple.GetPatternProvider(int iid)
{
//Return null means it does not support any Pattern Provider
return null;
}
}
}

创建一个简单的WinForm Application: WinFormServer,代码如下:

 using System;
using System.Windows.Forms; namespace WinFormServer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); this.Name = "testForm";
this.Text = "ClientUIADemo"; }
}
}

操作:

打开WinFormServer.exe。

运行UIAClientProvider。

参考

[1] 客户端 UI 自动化提供程序的实现

[2] UI Automation -- Under the Hood

Client-Side UI Automation Provider - WinForm Sample的更多相关文章

  1. Server-Side UI Automation Provider - WinForm Sample

    Server-Side UI Automation Provider - WinForm Sample 2014-09-14 源代码  目录 引用程序集提供程序接口公开服务器端 UI 自动化提供程序从 ...

  2. Server-Side UI Automation Provider - WPF Sample

    Server-Side UI Automation Provider - WPF Sample 2014-09-14 引用程序集 自动化对等类 WPF Sample 参考 引用程序集 返回 UIAut ...

  3. MS UI Automation Introduction

    MS UI Automation Introduction 2014-09-17 MS UI Automation是什么 UIA架构 UI自动化模型 UI自动化树概述 UI自动化控件模式概述 UI 自 ...

  4. 开源自己用python封装的一个Windows GUI(UI Automation)自动化工具,支持MFC,Windows Forms,WPF,Metro,Qt

    首先,大家可以看下这个链接 Windows GUI自动化测试技术的比较和展望 . 这篇文章介绍了Windows中GUI自动化的三种技术:Windows API, MSAA - Microsoft Ac ...

  5. 使用UI Automation实现自动化测试--5-7

    使用UI Automation实现自动化测试--5 (Winfrom和WPF中弹出和关闭对话框的不同处理方式) 在使用UI Automation对Winform和WPF的程序测试中发现有一些不同的地方 ...

  6. UI Automation 简介

    转载,源地址: http://blog.csdn.net/ffeiffei/article/details/6637418 MS UI Automation(Microsoft User Interf ...

  7. MS UI Automation简介

    转自:http://blog.csdn.net/ffeiffei/article/details/6637418 MS UI Automation(Microsoft User Interface A ...

  8. 使用UI Automation实现自动化测试--1-4

    Introduction UI Automation是Microsoft .NET 3.0框架下提供的一种用于自动化测试的技术,是在MSAA基础上建立的,MSAA就是Microsoft Active ...

  9. Visual Studio UI Automation 学习(二)

    今天恰好有时间,继续学习了一下UI Automation的知识.看了两篇博客,对UI Automation有了进一步的了解. https://blog.csdn.net/qq_37546891/art ...

随机推荐

  1. 【BZOJ】【1036】树的统计

    嗯这题是一道对树进行动态修改&查询的经典题目,可以拿来练习树链剖分~ 啊对于这种动态修改&查询的题目,我们最喜闻乐见的就是在一个序列上去做了,毕竟可以直接套各种数据结构模版啊,比如线段 ...

  2. NYOJ-205 求余数 AC 分类: NYOJ 2014-02-02 12:30 201人阅读 评论(0) 收藏

    这题目看一眼以为难度评级出错了,只是一个求余数的题目,,后来才发现,位数小于百万位,,,我还以为是大小小于百万呢,所以借鉴了另一大神的代码, 用大数,重点是同余定理: (a+b)mod m=((a m ...

  3. mysql date数据类型异常原因0000-00

    1.数据库字段: `dri_lic_first_time` date DEFAULT NULL COMMENT '驾驶证初次领证日期', 2.异常信息 org.springframework.dao. ...

  4. 充分发挥异步在 ASP.NET 中的强大优势

    作者:Brij Bhushan Mishra 最近几年,异步编程受到极大关注,主要是出于两个关键原因:首先,它有助于提供更好的用户体验,因为不会阻塞 UI 线程,避免了处理结束前出现 UI 界面挂起. ...

  5. 为jquery qrcode生成的二维码嵌入图片

    在一次微信项目中,需要实现通过扫描二维码来进行会议签到,二维码的生成选择了qrcode.js的版本,然后使用jquery.qrcode.js插件来绘制二维码. <script type=&quo ...

  6. java基础知识回顾之javaIO类---BufferedInputStream和BufferedOutputStream

    MP3的复制过程: package com.lp.ecjtu; import java.io.BufferedInputStream; import java.io.BufferedOutputStr ...

  7. CXF+Spring 搭建的WebService

    1.创建类 2.接口编写 package com.fan; import javax.jws.WebService; @WebService public interface IHelloWorld ...

  8. hdu 4618(最大回文子矩阵)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4618 昨天多校的一道题,说多了都是泪啊,为了一道图论题,磨了那么久,结果是别的题都没看,没办法,补呗. ...

  9. VISO下载地址

    http://pan.baidu.com/share/home?uk=4011207371#category/type=0

  10. lintcode:形状工厂

    题目 工厂模式是一种常见的设计模式.实现一个形状工厂 ShapeFactory 来创建不同的形状类.这里我们假设只有三角形,正方形和矩形三种形状. 样例 ShapeFactory sf = new S ...