UIAutomation command bat

简介: References: System; System.Data; System.Data.DataSetExtensions; System.

References: System; System.Data; System.Data.DataSetExtensions; System.Windows.Forms; System.Xml; System.Xml.Linq; UIAutomationClient; UIAutomationClientsideProviders; UIAutomationProvider; UIAutomationProvider; UIAutomationTypes; WindowsBase; using System; using System.Collections.Generic; using System.Text; using System.Windows.Automation; using System.Runtime.InteropServices; using System.Security.Permissions; namespace ConsoleApplication2 { [PermissionSet(SecurityAction.Demand, Name = "FullTrust" )] [ComVisibleAttribute(true )] class Program { #region Variations const int MOUSEEVENTF_MOVE = 0x0001; const int MOUSEEVENTF_LEFTDOWN = 0x0002; const int MOUSEEVENTF_LEFTUP = 0x0004; const int MOUSEEVENTF_RIGHTDOWN = 0x0008; const int MOUSEEVENTF_RIGHTUP = 0x0010; const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; const int MOUSEEVENTF_MIDDLEUP = 0x0040; #endregion #region SetCursoPos [DllImport("user32.dll" )] extern static int SetCursorPos(int incrementX, int incrementY); #endregion #region Mouse click event /// <summary> /// Mouse click event /// </summary> /// <param name="mouseEventFlag">MouseEventFlag </param> /// <param name="incrementX">X coordinate </param> /// <param name="incrementY">Y coordinate </param> /// <param name="data"></param> /// <param name="extraInfo"></param> [DllImport("user32.dll" )] extern static void mouse_event(int mouseEventFlag, int incrementX, int incrementY, uint data, UIntPtr extraInfo); #endregion #region Click AutomationElement /// <summary> /// /// </summary> /// <param name="ae"></param> public static void LeftClick(AutomationElement ae) { System.Windows.Rect rect = ae.Current.BoundingRectangle; int IncrementX = (int )(rect.Left + rect.Width / 2); int IncrementY = (int )(rect.Top + rect.Height / 2); //Make the cursor position to the element. SetCursorPos(IncrementX, IncrementY); //Make the left mouse down and up. mouse_event(MOUSEEVENTF_LEFTDOWN, IncrementX, IncrementY, 0, UIntPtr .Zero); mouse_event(MOUSEEVENTF_LEFTUP, IncrementX, IncrementY, 0, UIntPtr .Zero); } public static void RightClick(AutomationElement Element) { System.Windows.Rect rect = Element.Current.BoundingRectangle; int IncrementX = (int )(rect.Left + rect.Width / 2); int IncrementY = (int )(rect.Top + rect.Height / 2); //Make the cursor position to the element. SetCursorPos(IncrementX, IncrementY); //Make the left mouse down and up. mouse_event(MOUSEEVENTF_RIGHTDOWN, IncrementX, IncrementY, 0, UIntPtr.Zero); mouse_event(MOUSEEVENTF_RIGHTUP, IncrementX, IncrementY, 0, UIntPtr .Zero); } #endregion #region DelaySeconds /// <summary> /// Delay Seconds /// </summary> /// <param name="num"></param> private static void DelaySeconds(int num) { System.Threading.Thread .Sleep(1000 * num); } #endregion static void Main(string [] args) { string computerName = System.Configuration.ConfigurationSettings.AppSettings["ComputerName" ].ToUpper(); computerName = computerName == "" ? System.Net.Dns.GetHostEntry("localhost").HostName.Remove(System.Net.Dns.GetHostEntry("localhost").HostName.IndexOf('.' )).ToUpper() : computerName; string process1 = System.Configuration.ConfigurationSettings.AppSettings["Process1" ]; for (int i = 1; i < 100; i++) { string processi = System.Configuration.ConfigurationSettings.AppSettings["Process" + i.ToString()]; if (processi == null ) { Console .WriteLine(i); break ; } string[] processiArr = processi.Split('' ); System.Diagnostics.Process process1 = System.Diagnostics.Process.Start(@"" + processiArr[0]); process1.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle .Normal; process1.WaitForInputIdle(1000); Console.WriteLine(processi); } string step1 = System.Configuration.ConfigurationSettings.AppSettings["Step1"]; AutomationElement[] UIElement = new AutomationElement [100]; for (int i = 1; i < 100; i++) { string stepi = System.Configuration.ConfigurationSettings.AppSettings["Step" + i.ToString()]; if (stepi == null ) { Console.WriteLine(i); break; } else { stepi = stepi.Replace("%ComputerName%" , computerName); string[] stepiArr = stepi.Split('$'); if (stepiArr[0].ToUpper() == "Edit".ToUpper()) { System.Windows.Forms.SendKeys.SendWait(stepiArr[2]); Console.WriteLine("Edit: " + stepiArr[2]); } else if (stepiArr[0].ToUpper() == "Delay".ToUpper()) { DelaySeconds(Convert .ToInt32(stepiArr[2])); Console.WriteLine("DelaySeconds: " +stepiArr[2]); } else { if (stepiArr.Length > 5) { UIElement[i] = FindAutomationElement(stepiArr[1] == "Step0" ? AutomationElement.RootElement : UIElement[Convert.ToInt32(stepiArr[1].Substring(4, stepiArr[1].Length - 4))], TreeScope.Descendants, AutomationElement.NameProperty, stepiArr[2], AutomationElement.AutomationIdProperty, stepiArr[3], AutomationElement.ControlTypeProperty, StringToControlType(stepiArr[4]), AutomationElement.ClassNameProperty, stepiArr[5]); } else { UIElement[i] = FindAutomationElement(stepiArr[1] == "Step0" ? AutomationElement.RootElement : UIElement[Convert.ToInt32(stepiArr[1].Substring(4, stepiArr[1].Length - 4))], TreeScope .Descendants, AutomationElement.NameProperty, stepiArr[2], AutomationElement.AutomationIdProperty, stepiArr[3], AutomationElement.ControlTypeProperty, StringToControlType(stepiArr[4])); } if (UIElement == null ) { Console.WriteLine("UIElement is null" ); } else { Console.WriteLine("UIElement " + stepiArr[2] + " found" ); if (stepiArr[0].ToUpper() == "LeftClick".ToUpper()) { LeftClick(UIElement[i]); Console.WriteLine("LeftClick " + stepiArr[2]); } else if (stepiArr[0].ToUpper() == "RightClick".ToUpper()) { RightClick(UIElement[i]); Console.WriteLine("RightClick " + stepiArr[2]); } } } } Console.WriteLine(stepi); } Console.Read(); } public static object StringToControlType(string str) { object controlType = null ; if (str == "Window") controlType = ControlType.Window; else if (str == "MenuItem") controlType = ControlType.MenuItem; else if (str == "Document") controlType = ControlType.Document; else if (str == "TreeItem") controlType = ControlType.TreeItem; else if (str == "Menu" ) controlType = ControlType.Menu; else if (str == "MenuItem") controlType = ControlType.MenuItem; else if (str == "Document") controlType = ControlType.Document; else if (str == "Document") controlType = ControlType.Document; else if (str == "Document") controlType = ControlType.Document; return controlType; } public static AutomationElement FindAutomationElement(AutomationElement automationElement, TreeScope treeScope, AutomationProperty automationProperty1, object value1, AutomationProperty automationProperty2, object value2, AutomationProperty automationProperty3, object value3) { return automationElement.FindFirst ( treeScope, new AndCondition ( new PropertyCondition(automationProperty1, value1), new PropertyCondition(automationProperty2, value2), new PropertyCondition(automationProperty3, value3) ) ); } public static AutomationElement FindAutomationElement(AutomationElement automationElement, TreeScope treeScope, AutomationProperty automationProperty1, object value1, AutomationProperty automationProperty2, object value2, AutomationProperty automationProperty3, object value3, AutomationProperty automationProperty4, object value4) { return automationElement.FindFirst ( treeScope, new AndCondition ( new PropertyCondition (automationProperty1, value1), new PropertyCondition (automationProperty2, value2), new PropertyCondition (automationProperty3, value3), new PropertyCondition (automationProperty4, value4) ) ); } } } App.config <?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <add key="ComputerName" value=""/> <add key="Process1" value="C:/aaa.exe"/> <!-- value=action' ); System.Diagnostics.Process process1 = System.Diagnostics.Process.Start(@"" + processiArr[0]); process1.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle .Normal; process1.WaitForInputIdle(1000); Console.WriteLine(processi); } string step1 = System.Configuration.ConfigurationSettings.AppSettings["Step1"]; AutomationElement[] UIElement = new AutomationElement [100]; for (int i = 1; i < 100; i++) { string stepi = System.Configuration.ConfigurationSettings.AppSettings["Step" + i.ToString()]; if (stepi == null ) { Console.WriteLine(i); break; } else { stepi = stepi.Replace("%ComputerName%" , computerName); string[] stepiArr = stepi.Split('$'); if (stepiArr[0].ToUpper() == "Edit".ToUpper()) { System.Windows.Forms.SendKeys.SendWait(stepiArr[2]); Console.WriteLine("Edit: " + stepiArr[2]); } else if (stepiArr[0].ToUpper() == "Delay".ToUpper()) { DelaySeconds(Convert .ToInt32(stepiArr[2])); Console.WriteLine("DelaySeconds: " +stepiArr[2]); } else { if (stepiArr.Length > 5) { UIElement[i] = FindAutomationElement(stepiArr[1] == "Step0" ? AutomationElement.RootElement : UIElement[Convert.ToInt32(stepiArr[1].Substring(4, stepiArr[1].Length - 4))], TreeScope.Descendants, AutomationElement.NameProperty, stepiArr[2], AutomationElement.AutomationIdProperty, stepiArr[3], AutomationElement.ControlTypeProperty, StringToControlType(stepiArr[4]), AutomationElement.ClassNameProperty, stepiArr[5]); } else { UIElement[i] = FindAutomationElement(stepiArr[1] == "Step0" ? AutomationElement.RootElement : UIElement[Convert.ToInt32(stepiArr[1].Substring(4, stepiArr[1].Length - 4))], TreeScope .Descendants, AutomationElement.NameProperty, stepiArr[2], AutomationElement.AutomationIdProperty, stepiArr[3], AutomationElement.ControlTypeProperty, StringToControlType(stepiArr[4])); } if (UIElement == null ) { Console.WriteLine("UIElement is null" ); } else { Console.WriteLine("UIElement " + stepiArr[2] + " found" ); if (stepiArr[0].ToUpper() == "LeftClick".ToUpper()) { LeftClick(UIElement[i]); Console.WriteLine("LeftClick " + stepiArr[2]); } else if (stepiArr[0].ToUpper() == "RightClick".ToUpper()) { RightClick(UIElement[i]); Console.WriteLine("RightClick " + stepiArr[2]); } } } } Console.WriteLine(stepi); } Console.Read(); } public static object StringToControlType(string str) { object controlType = null ; if (str == "Window") controlType = ControlType.Window; else if (str == "MenuItem") controlType = ControlType.MenuItem; else if (str == "Document") controlType = ControlType.Document; else if (str == "TreeItem") controlType = ControlType.TreeItem; else if (str == "Menu" ) controlType = ControlType.Menu; else if (str == "MenuItem") controlType = ControlType.MenuItem; else if (str == "Document") controlType = ControlType.Document; else if (str == "Document") controlType = ControlType.Document; else if (str == "Document") controlType = ControlType.Document; return controlType; } public static AutomationElement FindAutomationElement(AutomationElement automationElement, TreeScope treeScope, AutomationProperty automationProperty1, object value1, AutomationProperty automationProperty2, object value2, AutomationProperty automationProperty3, object value3) { return automationElement.FindFirst ( treeScope, new AndCondition ( new PropertyCondition(automationProperty1, value1), new PropertyCondition(automationProperty2, value2), new PropertyCondition(automationProperty3, value3) ) ); } public static AutomationElement FindAutomationElement(AutomationElement automationElement, TreeScope treeScope, AutomationProperty automationProperty1, object value1, AutomationProperty automationProperty2, object value2, AutomationProperty automationProperty3, object value3, AutomationProperty automationProperty4, object value4) { return automationElement.FindFirst ( treeScope, new AndCondition ( new PropertyCondition (automationProperty1, value1), new PropertyCondition (automationProperty2, value2), new PropertyCondition (automationProperty3, value3), new PropertyCondition (automationProperty4, value4) ) ); } } } App.config <?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <add key="ComputerName" value=""/> <add key="Process1" value="C:/aaa.exe"/> <!-- value=actionStepNNamePropertyAutomationIdPropertyControlTypePropertyvalue=actionStepNNamePropertyAutomationIdPropertyControlTypePropertyClassNameProperty value=EditStepNEditValue value=DelayStepNseconds 1.action:LeftClick;RightClick;Edit;Delay; StepN: NameProperty: AutomationIdProperty: ControlTypeProperty: 2.EditValue:/when action is Edit; "%{N}" is ALT+N; "^s" is Ctrl+S; "{Enter}" is press Enter key; "%{F4}" is Alt+F4; --> <add key="Step1" value="LeftClickStep0aaa Server ManagerWindow"/><addkey="Step2"value="LeftClick$Step1$ManagementServer

TreeItem"/> <add key="Step3" value="EditStep1{RIGHT}" /> <add key="Step4" value="LeftClickStep1Action ServersTreeItem"/><addkey="Step5"value="Edit$Step1$RIGHT"/><addkey="Step6"value="Delay$Step0$2"/><addkey="Step7"value="RightClick$Step1$
TreeItem"/> <add key="Step8" value="DelayStep02" /> <add key="Step9" value="RightClickStep0Menu$$Menu$#32768"/> <add key="Step10" value="LeftClick$Step9$Uninstall Action Server$Item 44101$MenuItem"/> </appSettings> </configuration>

目录
打赏
0
0
0
0
20
分享
相关文章
|
10月前
|
python安装HLL报错unable to find vcvarsall.bat
通过上述方法之一,你应该能够解决"unable to find vcvarsall.bat"的错误,并成功安装HLL或其他需要编译的Python扩展。确保在安装之前先安装了Visual C++构建工具。
103 0
MAC The default interactive shell is now zsh.
MAC The default interactive shell is now zsh.
671 0
msys2_shell.cmd源码分析
msys2_shell.cmd源码分析
463 0
Windows cmd(bat) 脚本简单使用
前提知识 命令提示符介绍 命令提示符是在操作系统中,提示进行命令输入的一种工作提示符。在不同的操作系统环境下,命令提示符各不相同。在 windows 环境下,命令行程序为 cmd.exe,微软 Windows 系统基于 Windows 上的命令解释程序,类似于微软的 DOS 操作系统。
975 0
"\Tools\QtCreator\bin\clangbackend.exe" could not be started
"\Tools\QtCreator\bin\clangbackend.exe" could not be started
441 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等