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.Tasks;
using System.Windows.Automation;
using System.Windows.Forms;
using System.Windows.Input;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private Process process;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//打开笔记本
process = Process.Start(@"C:\Windows\System32\notepad.exe");
}
private void button2_Click(object sender, EventArgs e)
{
//关闭笔记本
process.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, 0, 0, 0, 0); // 模拟鼠标左键按下
mouse_event(0x0004, 0, 0, 0, 0); // 模拟鼠标左键弹起
}
}
}
修改对应代码,每个按钮点击事件下的的该属性都需要进行修改
功能是让鼠标去点击,实现点击按钮的功能
https://www.cnblogs.com/baihuitestsoftware/articles/9047705.html
UI自动化 --- 微软UI Automation_dotNET跨平台的博客-CSDN博客