对于AutomationElement 类大家都知道是用于做什么事情的吧,最近闲来无聊的我正好搜集了一些相关的C#代码。相信大家用好了这个类一定能够使Windows上自动化工作更上一层楼,如果结合好Microsoft自带的工具inspect,就更能达到事半功倍的效果了。不过笔者弄完了代码后实在是太累了,因此上代码中几乎是一行注释也没有了。好在聪明的你肯定是无师自通的高手了。
想要使用automation首先要做的是在引用中加入UI...dll等几个文件。其实这几个文件不用都选的,只要client和types就好了,但是笔者为了偷懒就一下都选了。如下图:
下面是一个自动操作记事本的命令行代码,就直接给代码了。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Windows.Automation;
using System.Windows.Automation.Text;
using System.Runtime.InteropServices;
using System.IO;
namespace ConsoleApplication3
{
class Program
{
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);
static void Main(string[] args)
{
Program p = new Program();
AutomationEventHandler a = new AutomationEventHandler(p.onWindowOpenOrClose);
Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Children, a);
Process.Start("notepad.exe", "C:\\Users\\Administrator\\Documents\\aaa.txt");
Console.ReadLine();
}
private void onWindowOpenOrClose(object sender, AutomationEventArgs e)
{
Thread.Sleep(2000);
Condition myCondition = null;
Condition myCondition1 = new PropertyCondition(AutomationElement.AutomationIdProperty, "MenuBar");
Condition myCondition3 = new PropertyCondition(AutomationElement.AcceleratorKeyProperty, "F5");
Condition myCondition2 = new PropertyCondition(AutomationElement.NameProperty, "编辑(E)");
myCondition = new OrCondition(myCondition1, myCondition2);
AutomationElement w = sender as AutomationElement;
Console.WriteLine(w.Current.Name);
AutomationElement w1 = w.FindFirst(TreeScope.Children, myCondition1);
Console.WriteLine(w1.Current.Name);
AutomationElement w2 = w1.FindFirst(TreeScope.Children, myCondition2);
Console.WriteLine(w2.Current.Name);
ExpandCollapsePattern ecp = (ExpandCollapsePattern)w2.GetCurrentPattern(ExpandCollapsePattern.Pattern);
ecp.Expand();
Thread.Sleep(500);
AutomationElementCollection myCollection = w2.FindAll(TreeScope.Descendants, myCondition3);
AutomationElement w3 = myCollection[0];
Console.WriteLine(w3.Current.Name);
AutomationPattern[] bbbb = w3.GetSupportedPatterns();
InvokePattern ipn = (InvokePattern)w3.GetCurrentPattern(InvokePattern.Pattern);
ipn.Invoke();
Thread.Sleep(500);
Condition myCondition4 = new PropertyCondition(AutomationElement.ClassNameProperty, "Edit");
AutomationElementCollection myCollection1 = w.FindAll(TreeScope.Descendants, myCondition4);
AutomationElement w4 = myCollection1[0];
Console.WriteLine(w4.Current.Name);
string s = "至强的记事本自动化程序!";
TextPattern tp = (TextPattern)w4.GetCurrentPattern(TextPattern.Pattern);
Console.WriteLine(tp.DocumentRange.GetText(5));
s += tp.DocumentRange.GetText(-1);
Thread.Sleep(500);
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(s));
}
else {
}
Thread.Sleep(500);
AutomationElement file = autoElementGet1(w, "文件(F)");
ExpandCollapsePattern ecp1 = (ExpandCollapsePattern)file.GetCurrentPattern(ExpandCollapsePattern.Pattern);
ecp1.Expand();
if (File.Exists("C:\\Users\\Administrator\\Documents\\aaa.txt"))
{
Console.WriteLine("文件存在");
Thread.Sleep(500);
AutomationElement save = autoElementGet1(w1, "另存为(A)...");
InvokePattern ipn1 = (InvokePattern)save.GetCurrentPattern(InvokePattern.Pattern);
ipn1.Invoke();
Thread.Sleep(1800);
AutomationElement edit1 = autoElementGet1(w, "1001");
ValuePattern vp = (ValuePattern)edit1.GetCurrentPattern(ValuePattern.Pattern);
vp.SetValue("aaa.txt");
Thread.Sleep(500);
AutomationElement btn1 = autoElementGet1(w, "保存(S)");
InvokePattern ipn3 = (InvokePattern)btn1.GetCurrentPattern(InvokePattern.Pattern);
ipn3.Invoke();
Thread.Sleep(200);
AutomationElement btn6 = autoElementGet1(w, "CommandButton_6");
InvokePattern ipn4 = (InvokePattern)btn6.GetCurrentPattern(InvokePattern.Pattern);
ipn4.Invoke();
}
else {
Thread.Sleep(500);
AutomationElement save = autoElementGet1(w1, "保存(S)");
InvokePattern ipn1 = (InvokePattern)save.GetCurrentPattern(InvokePattern.Pattern);
ipn1.Invoke();
Thread.Sleep(800);
AutomationElement edit1 = autoElementGet1(w, "1001");
ValuePattern vp = (ValuePattern)edit1.GetCurrentPattern(ValuePattern.Pattern);
vp.SetValue("aaa.txt");
Thread.Sleep(200);
AutomationElement btn1 = autoElementGet1(w, "保存(S)");
InvokePattern ipn3 = (InvokePattern)btn1.GetCurrentPattern(InvokePattern.Pattern);
ipn3.Invoke();
}
Thread.Sleep(500);
ecp1.Expand();
AutomationElement exit = autoElementGet1(w1, "退出(X)");
InvokePattern ipn2 = (InvokePattern)exit.GetCurrentPattern(InvokePattern.Pattern);
ipn2.Invoke();
}
private void elementInvoke(string id, AutomationElement ae) {
Condition myCondition = null;
Condition myCondition1 = new PropertyCondition(AutomationElement.AutomationIdProperty, id);
Condition myCondition2 = new PropertyCondition(AutomationElement.NameProperty, id);
Condition myCondition3 = new PropertyCondition(AutomationElement.AcceleratorKeyProperty, id);
myCondition = new OrCondition(myCondition1, myCondition2, myCondition3);
AutomationElementCollection myCollection = ae.FindAll(TreeScope.Descendants, myCondition);
AutomationElement element = myCollection[0];
InvokePattern ipn = (InvokePattern)element.GetCurrentPattern(InvokePattern.Pattern);
ipn.Invoke();
Thread.Sleep(500);
}
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];
}
}
}
大概的动作就是打开记事本->点击编辑菜单->点击时间日期菜单选项,然后向编辑框中写入一段字符串,接下来模拟上述动作保存文件,最后模拟按下文件中的退出菜单选项。哦,不要忘了呦,它的运行环境是window10的记事本。