1、第一步:添加引用UIAtuomationClient.dll,UIAtutomationTypes.dll
2、第二步:添加log4net.dll日志类。
3、辅助类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using System.Drawing;
using System.Threading;
using System.Windows.Automation;
using System.Runtime.InteropServices;
namespace YunDouTaxImport.Utils
{
public class UIAutoHelper
{
/// <summary>
/// 日志辅助
/// </summary>
private static readonly ILog Log = LogManager.GetLogger(nameof(UIAutoHelper));
/// <summary>
/// 获取桌面的根
/// </summary>
/// <value>
/// The elmnt root.
/// </value>
public static AutomationElement ElmntRoot
{
get { return AutomationElement.RootElement; }
}
#region 创建条件对象
/// <summary>
/// 通过名称获取条件
/// </summary>
/// <param name="name">The name.</param>
/// <returns></returns>
public static PropertyCondition GetConditionWithName(string name)
{
return GetCondition(AutomationElement.NameProperty, name);
}
/// <summary>
/// 通过类名称获取条件
/// </summary>
/// <param name="className">Name of the class.</param>
/// <returns></returns>
public static PropertyCondition GetConditionWithClassName(string className)
{
return GetCondition(AutomationElement.ClassNameProperty, className);
}
/// <summary>
/// 通过AutomationId获取条件
/// </summary>
/// <param name="id">The identifier.</param>
/// <returns></returns>
public static PropertyCondition GetConditionWithAutomationId(string id)
{
return GetCondition(AutomationElement.AutomationIdProperty, id);
}
/// <summary>
/// 通过ProcessId获取条件
/// </summary>
/// <param name="id">The identifier.</param>
/// <returns></returns>
public static PropertyCondition GetConditionWithProcessId(int id)
{
return GetCondition(AutomationElement.ProcessIdProperty, id);
}
/// <summary>
/// 通过条件组合获取
/// </summary>
/// <param name="array">The array.</param>
/// <returns></returns>
public static Condition GetConditionFromArray(PropertyCondition[] array)
{
return new AndCondition(array);
}
private static PropertyCondition GetCondition(AutomationProperty property, object val)
{
return new PropertyCondition(property, val);
}
#endregion 创建条件对象
#region 查找界面元素
/// <summary>
/// 通过父节点查找第一个字节点元素(只针对下一级元素)
/// </summary>
/// <param name="countRetry">The count retry.</param>
/// <param name="interval">The interval.</param>
/// <param name="parent">The parent.</param>
/// <param name="condition">The condition.</param>
/// <returns></returns>
public static AutomationElement FindFirstOfChildren(int countRetry,int interval,AutomationElement parent,Condition condition)
{
for(int i = 0; i < countRetry; i++)
{
try
{
var elmnt = parent.FindFirst(TreeScope.Children, condition);
if (elmnt != null)
{
return elmnt;
}
}
catch(Exception e)
{
Log.Error(string.Format("用Children参数调用FindFirst方法时出现异常,异常消息:{0}", e.Message));
}
Thread.Sleep(interval);
}
return null;
}
/// <summary>
/// 通过父节点查找所有的子节点(只针对下一级)
/// </summary>
/// <param name="countRetry">The count retry.</param>
/// <param name="interval">The interval.</param>
/// <param name="parent">The parent.</param>
/// <param name="condition">The condition.</param>
/// <returns></returns>
public static AutomationElementCollection FindAllOfChildren(int countRetry, int interval, AutomationElement parent, Condition condition)
{
for (int i = 0; i < countRetry; i++)
{
try
{
var elmnt = parent.FindAll(TreeScope.Children, condition);
if (elmnt != null)
{
return elmnt;
}
}
catch (Exception e)
{
Log.Error(string.Format("用Children参数调用FindAll方法时出现异常,异常消息:{0}", e.Message));
}
Thread.Sleep(interval);
}
return null;
}
/// <summary>
/// 通过父节点查找所有的子节点中的第一个满足条件的子节点
/// </summary>
/// <param name="countRetry">重试次数</param>
/// <param name="interval">时间间隔.</param>
/// <param name="parent">父节点</param>
/// <param name="condition">条件.</param>
/// <returns></returns>
public static AutomationElement FindFirstOfSubtree(int countRetry, int interval, AutomationElement parent, Condition condition)
{
for (int i = 0; i < countRetry; i++)
{
try
{
var elmnt = parent.FindFirst(TreeScope.Subtree, condition);
if (elmnt != null)
{
return elmnt;
}
}
catch (Exception e)
{
Log.Error(string.Format("用Subtree参数调用FindFirst方法时出现异常,异常消息:{0}", e.Message));
}
Thread.Sleep(interval);
}
return null;
}
/// <summary>
/// 通过父节点查找所有的子节点中的所有满足条件的子节点
/// </summary>
/// <param name="countRetry">重试次数.</param>
/// <param name="interval">时间间隔</param>
/// <param name="parent">父节点.</param>
/// <param name="condition">条件</param>
/// <returns></returns>
public static AutomationElementCollection FindAllOfSubtree(int countRetry, int interval, AutomationElement parent, Condition condition)
{
for (int i = 0; i < countRetry; i++)
{
try
{
var elmnt = parent.FindAll(TreeScope.Subtree, condition);
if (elmnt != null)
{
return elmnt;
}
}
catch (Exception e)
{
Log.Error(string.Format("用Subtree参数调用FindAll方法时出现异常,异常消息:{0}", e.Message));
}
Thread.Sleep(interval);
}
return null;
}
#endregion 查找界面元素
/// <summary>
/// 设置编辑框的值
/// </summary>
/// <param name="edit"></param>
/// <param name="val"></param>
public static void SetValue(AutomationElement edit,string val)
{
var pattern = (ValuePattern)edit.GetCurrentPattern(ValuePattern.Pattern);
pattern.SetValue(val);
}
/// <summary>
/// 点击指定位置
/// </summary>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
public static void Click(int x,int y)
{
Point point = new Point();
GetCursorPos(ref point);
SetCursorPos(x, y);
Thread.Sleep(200);
mouse_event(2, x, y, 0, IntPtr.Zero);
mouse_event(4, x, y, 0, IntPtr.Zero);
Thread.Sleep(300);
SetCursorPos((int)point.X, (int)point.Y);
}
private static T Retry<T>(int count,int interval,
Func<TreeScope,Condition,T> func,TreeScope treeScope,Condition condition)
{
for (int i = 0; i < count; i++)
{
try
{
T elmnt = func(treeScope, condition);
if (elmnt != null)
{
return elmnt ;
}
}
catch (Exception e)
{
Log.Error(string.Format("用Children参数调用FindFirst方法时出现异常,异常消息:{0}", e.Message));
}
Thread.Sleep(interval);
}
return default(T);
}
#region Win32
[DllImport("user32.dll")]
private static extern void mouse_event(uint dwFlags, int dX, int dY, uint dwData, IntPtr dwExtraInfo);
[DllImport("user32.dll")]
private static extern void SetCursorPos(int dX, int dY);
[DllImport("user32.dll")]
private static extern void GetCursorPos(ref Point point);
//置顶窗体
[DllImport("user32.dll", CharSet = CharSet.None, ExactSpelling = false)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
#endregion Win32
}
}
4、使用
/// <summary>
/// 多企业管理窗口
/// </summary>
protected AutomationElement ElmntCompanyManager
{
get
{
return UIAutoHelper.FindFirstOfChildren(10, 200, UIAutoHelper.ElmntRoot,
UIAutoHelper.GetConditionFromArray(new PropertyCondition[]
{
UIAutoHelper.GetConditionWithClassName("TFrmCompanyMgrView"),
UIAutoHelper.GetConditionWithName("多企业管理")
}
));
}
}
protected AutomationElement EditTaxpayer
{
get
{
var elmnt = ElmntCompanyManager;
var edits = UIAutoHelper.FindAllOfSubtree(10, 100, elmnt, UIAutoHelper.GetConditionFromArray(new PropertyCondition[]
{
UIAutoHelper.GetConditionWithProcessId(elmnt.Current.ProcessId),
UIAutoHelper.GetConditionWithClassName("TITSEdit"),
}));
if (edits != null && edits.Count > 0)
{
return edits[0];
}
return null;
}
}