步骤
UI Automation 只适用于,标准的win32和 WPF程序
需要添加对UIAutomationClient、 UIAutomationProvider、 UIAutomationTypes的引用
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Automation;
using System.Windows.Forms;
using System.Windows.Input;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private Process processnotepad;
private Process processcalc;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//打开笔记本
processnotepad = Process.Start(@"C:\Windows\System32\notepad.exe");
}
private void button2_Click(object sender, EventArgs e)
{
//关闭笔记本
processnotepad.Kill();
}
private void button3_Click(object sender, EventArgs e)
{
//AutomationElement表示 UI 自动化树中的一个 UI 自动化元素,并包含由 UI 自动化客户端应用程序用作标识符的值。
//获取当前桌面的根 AutomationElement。
AutomationElement desktop = AutomationElement.RootElement;
//StringBuilder不在内存中创建新对象,而是动态扩展内存以容纳修改后的字符串。
StringBuilder sb = new StringBuilder();
//TreeScope(枚举)包含指定 UI 自动化目录树内元素的范围的值。具体参考请点击以下链接进行查看
//TreeScope官方链接:https://learn.microsoft.com/zh-cn/dotnet/api/system.windows.automation.treescope?view=windowsdesktop-7.0
AutomationElementCollection topWindows = desktop.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "ApplicationFrameWindow"));//查找计算器
for (int i = 0; i < topWindows.Count; i++)
{
AutomationElement topWindow = topWindows[i];
sb.AppendLine("Name:" + topWindow.Current.Name + ";ClassName=" + topWindow.Current.ClassName);
}
MessageBox.Show(sb.ToString());
}
private void button4_Click(object sender, EventArgs e)
{
AutomationElement desktop = AutomationElement.RootElement;
var calcFrame1 = desktop.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "ApplicationFrameWindow"));
AutomationElementCollection btn2 = calcFrame1.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, "num9Button"));
AutomationElement btn = btn2[0];
MessageBox.Show(btn.Current.Name);
}
private void button5_Click(object sender, EventArgs e)
{
AutomationElement desktop = AutomationElement.RootElement;
var calcFrame1 = desktop.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.ClassNameProperty, "ApplicationFrameWindow"));
Condition conditionBtn6 = new AndCondition(
new PropertyCondition(AutomationElement.ClassNameProperty, "Button"),
new PropertyCondition(AutomationElement.NameProperty, "六")
);
var btn6 = calcFrame1.FindFirst(TreeScope.Descendants, conditionBtn6);
//InvokePattern 表示用于启动或执行单个明确操作的控件,并且这些控件在激活时不保持其状态。
//InvokePattern.Pattern 标识 InvokePattern 控件模式。
//InvokePattern官方链接 https://learn.microsoft.com/zh-cn/dotnet/api/system.windows.automation.invokepattern?view=windowsdesktop-7.0
InvokePattern button6Invoke = (InvokePattern)btn6.GetCurrentPattern(InvokePattern.Pattern);
//Invoke() 发送请求以激活控件并启动其单一、明确的操作。
button6Invoke.Invoke();
Condition conditionBtnPlus = new AndCondition(
new PropertyCondition(AutomationElement.ClassNameProperty, "Button"),
new PropertyCondition(AutomationElement.NameProperty, "加")
);
var btnPlus = calcFrame1.FindFirst(TreeScope.Descendants, conditionBtnPlus);
InvokePattern buttonPlusInvoke = (InvokePattern)btnPlus.GetCurrentPattern(InvokePattern.Pattern);
buttonPlusInvoke.Invoke();
}
private static void InvokeButton(AutomationElement e)
{
InvokePattern Invoke = (InvokePattern)e.GetCurrentPattern(InvokePattern.Pattern);
Invoke.Invoke();
}
private static void ClickCalculatorButton(AutomationElement calcFrame1, String name)
{
Condition conditionBtn = new AndCondition(
new PropertyCondition(AutomationElement.ClassNameProperty, "Button"),
new PropertyCondition(AutomationElement.NameProperty, name)
);
var btn = calcFrame1.FindFirst(TreeScope.Descendants, conditionBtn);
// MessageBox.Show(btn.Current.Name);
if (btn == null)
{
throw new Exception("找不到此" + name + "的按钮");
}
InvokeButton(btn);
}
private void button6_Click(object sender, EventArgs e)
{
AutomationElement desktop = AutomationElement.RootElement;
var calcFrame1 = desktop.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.ClassNameProperty, "ApplicationFrameWindow"));
ClickCalculatorButton(calcFrame1, "三");
ClickCalculatorButton(calcFrame1, "乘以");
ClickCalculatorButton(calcFrame1, "五");
ClickCalculatorButton(calcFrame1, "五");
ClickCalculatorButton(calcFrame1, "等于");
}
[DllImport("user32.dll")]
public static extern void SetCursorPos(int x, int y);
[DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
private void button7_Click(object sender, EventArgs e)
{
AutomationElement desktop = AutomationElement.RootElement;
var calcFrame1 = desktop.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.ClassNameProperty, "ApplicationFrameWindow"));
Condition conditionBtn6 = new AndCondition(
new PropertyCondition(AutomationElement.ClassNameProperty, "Button"),
new PropertyCondition(AutomationElement.NameProperty, "六")
);
var btn6 = calcFrame1.FindFirst(TreeScope.Descendants, conditionBtn6);
SetCursorPos((int)btn6.GetClickablePoint().X, (int)btn6.GetClickablePoint().Y);
//mouse_event(0x0002 | 0x0004, 0, 0, 0, 0);
mouse_event(0x0002, 0, 0, 0, 0); // 模拟鼠标左键按下
mouse_event(0x0004, 0, 0, 0, 0); // 模拟鼠标左键弹起
}
private void button8_Click(object sender, EventArgs e)
{
AutomationElement desktop = AutomationElement.RootElement;
var calcFrame1 = desktop.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.ClassNameProperty, "Notepad"));
Condition conditionEdit = new AndCondition(
new PropertyCondition(AutomationElement.ClassNameProperty, "Edit"), new PropertyCondition(AutomationElement.NameProperty, "文本编辑器"));
AutomationElement txtEdit = calcFrame1.FindFirst(TreeScope.Descendants, conditionEdit);
txtEdit.SetFocus();
SendKeys.Send("追加123456789");
}
private const int WM_SETTEXT = 0x000C;
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindows);
[DllImport("User32.dll")]
private static extern Int32 SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, StringBuilder lParam);
private void button9_Click(object sender, EventArgs e)
{
AutomationElement desktop = AutomationElement.RootElement;
var calcFrame1 = desktop.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.ClassNameProperty, "Notepad"));
Condition conditionEdit = new AndCondition(
new PropertyCondition(AutomationElement.ClassNameProperty, "Edit"), new PropertyCondition(AutomationElement.NameProperty, "文本编辑器"));
AutomationElement txtEdit = calcFrame1.FindFirst(TreeScope.Descendants, conditionEdit);
//.NET提供了一个结构体System.IntPtr专门用来代表句柄或指针。
//句柄是对象的标识符,当调用这些API创建对象时,它们并不直接返回指向对象的指针,
//而是会返回一个32位或64位的整数值,这个在进程或系统范围内唯一的整数值就是句柄(Handle),
//随后程序再次访问对象,或者删除对象,都将句柄作为Windows API的参数来间接对这些对象进行操作。
//句柄是一个结构体,简单的来说,它是指针的一个封装,是C#中指针的替代者
//句柄链接:https://blog.csdn.net/sinat_40003796/article/details/127244155
IntPtr hWnd = FindWindow("Notepad", null);
if (!hWnd.Equals(IntPtr.Zero))
{
IntPtr edithWnd = FindWindowEx(hWnd, IntPtr.Zero, "Edit", null);
if (!edithWnd.Equals(IntPtr.Zero))
{
SendMessage(edithWnd, WM_SETTEXT, IntPtr.Zero, new StringBuilder("重写123456789"));
}
}
else
{
}
}
private void button10_Click(object sender, EventArgs e)
{
//点击后有列表的按钮不要当成点击事件
AutomationElement desktop = AutomationElement.RootElement;
var calcFrame1 = desktop.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.ClassNameProperty, "Notepad"));
Condition myCondition2 = new PropertyCondition(AutomationElement.NameProperty, "编辑(E)");
AutomationElement w2 = calcFrame1.FindFirst(TreeScope.Descendants, myCondition2);
ExpandCollapsePattern ecp = (ExpandCollapsePattern)w2.GetCurrentPattern(ExpandCollapsePattern.Pattern);
ecp.Expand();
}
private AutomationElement autoElementGet1(AutomationElement e, string s)
{
Condition myCondition1 = new PropertyCondition(AutomationElement.AutomationIdProperty, s);
Condition myCondition2 = new PropertyCondition(AutomationElement.NameProperty, s);
Condition myCondition3 = new PropertyCondition(AutomationElement.AcceleratorKeyProperty, s);
Condition myCondition4 = new PropertyCondition(AutomationElement.ClassNameProperty, s);
Condition myCondition5 = new PropertyCondition(AutomationElement.AccessKeyProperty, s);
Condition myCondition6 = new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, s);
Condition myCondition = new OrCondition(myCondition1, myCondition2, myCondition3, myCondition4, myCondition5,
myCondition6);
AutomationElementCollection myCollection = e.FindAll(TreeScope.Descendants, myCondition);
return myCollection[0];
}
private void button11_Click(object sender, EventArgs e)
{
//随机元素获取
AutomationElement desktop = AutomationElement.RootElement;
var calcFrame1 = desktop.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.ClassNameProperty, "Notepad"));
AutomationElement filename = autoElementGet1(calcFrame1, "文件(F)");
MessageBox.Show("Name:" + filename.Current.Name + ";ClassName=" + filename.Current.ClassName);
}
private void button12_Click(object sender, EventArgs e)
{
//遍历子控件
AutomationElement desktop = AutomationElement.RootElement;
var calcFrame1 = desktop.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.ClassNameProperty, "Notepad"));
Condition myCondition1 = new PropertyCondition(AutomationElement.AutomationIdProperty, "MenuBar");
AutomationElement MenuBar = calcFrame1.FindFirst(TreeScope.Descendants, myCondition1);
AutomationElementCollection MenuBarChildAll = MenuBar.FindAll(TreeScope.Children, Condition.TrueCondition);
for (int i = 0; i < MenuBarChildAll.Count; i++)
{
AutomationElement MenuBarChild = MenuBarChildAll[i];
MessageBox.Show("Name:" + MenuBarChild.Current.Name + ";ClassName=" + MenuBarChild.Current.ClassName + ";ControlType=" + MenuBarChild.Current.ControlType);
}
}
private void button13_Click(object sender, EventArgs e)
{
processcalc = Process.Start(@"C:\Windows\System32\calc.exe");
}
private void button14_Click(object sender, EventArgs e)
{
processcalc.Kill();
}
private void button15_Click(object sender, EventArgs e)
{
//点击后有列表的按钮不要当成点击事件
AutomationElement desktop = AutomationElement.RootElement;
var calcFrame1 = desktop.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.ClassNameProperty, "Notepad"));
Condition myCondition1 = new PropertyCondition(AutomationElement.NameProperty, "文件(F)");
AutomationElement w1 = calcFrame1.FindFirst(TreeScope.Descendants, myCondition1);
ExpandCollapsePattern ecp = (ExpandCollapsePattern)w1.GetCurrentPattern(ExpandCollapsePattern.Pattern);
ecp.Expand();
Condition myCondition2 = new PropertyCondition(AutomationElement.NameProperty, "退出(X)");
AutomationElementCollection myCollection = w1.FindAll(TreeScope.Descendants, myCondition2);
AutomationElement w2 = myCollection[0];
InvokePattern ipn = (InvokePattern)w2.GetCurrentPattern(InvokePattern.Pattern);
ipn.Invoke();
Thread.Sleep(500);
AutomationElement savefile = calcFrame1.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "保存(S)"));
if (savefile != null)
{
InvokePattern savefilebutton = (InvokePattern)savefile.GetCurrentPattern(InvokePattern.Pattern);
savefilebutton.Invoke();
Thread.Sleep(500);
AutomationElement Saveasform = calcFrame1.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "另存为"));
AutomationElement edit1 = Saveasform.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, "1001"));
ValuePattern vp = (ValuePattern)edit1.GetCurrentPattern(ValuePattern.Pattern);
vp.SetValue("aaa.txt");
AutomationElement savefilepath = Saveasform.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "保存(S)"));
InvokePattern savefilepathbutton = (InvokePattern)savefilepath.GetCurrentPattern(InvokePattern.Pattern);
savefilepathbutton.Invoke();
Thread.Sleep(500);
AutomationElement yesSaveasform = Saveasform.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "确认另存为"));
AutomationElement issavefilepath = Saveasform.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "是(Y)"));
if (issavefilepath != null) {
InvokePattern issavefilepathbutton = (InvokePattern)issavefilepath.GetCurrentPattern(InvokePattern.Pattern);
issavefilepathbutton.Invoke();
}
}
}
}
}
各按钮的功能展示
打开记事本
关闭记事本
获取计算器窗体
修改对应代码,每个按钮点击事件下的的该属性都需要进行修改
结果
获取控件属性
结果
点击按钮
完成计算器计算
鼠标点击按钮
功能是让鼠标去点击,实现点击按钮的功能
追加记事本内容
原始
运行
重写记事本内容
展开按钮列表
随机获取元素方法
遍历记事本菜单栏
打开计算器
控件关闭计算器
保存记事本
工程示例
https://download.csdn.net/download/qq_39397927/88215681
参考
https://www.cnblogs.com/baihuitestsoftware/articles/9047705.html
UI自动化 --- 微软UI Automation_dotNET跨平台的博客-CSDN博客