13 public static InputSimulator input;
14 public static Dictionary<string, Recording>
Recordings =
new Dictionary<string, Recording>();
15 public static Dictionary<string, Recording> Properties =
new Dictionary<string, Recording>();
16 public static UIA3Automation automation =
new UIA3Automation();
20 private KeyEventArgs key;
21 private KeyPressEventArgs keyPress;
22 private string automationID;
24 private string className;
25 private string process;
28 private System.Drawing.Point point;
30 private MouseEventArgs mouse;
31 private MouseButtons mouseButton;
32 private ValueType autoValue;
55 public Type ActionType
68 return (Keys)keyPress.KeyChar;
73 public MouseButtons Button
75 get {
return mouseButton; }
76 set { mouseButton = value; }
78 public System.Drawing.Point Point
81 set { point = value; }
83 public string AutomationID
85 get {
return automationID; }
86 set { automationID = value; }
93 public string ClassName
95 get {
return className; }
96 set { className = value; }
98 public string ProcessName
100 get {
return process; }
101 set { process = value; }
105 get {
return title; }
106 set { title = value; }
110 get {
return index; }
111 set { index = value; }
113 public ValueType Value
115 get {
return autoValue; }
116 set { autoValue = value; }
118 public Action(Type t,
int proc, KeyEventArgs e)
122 AutomationElement el = Element;
124 automationID = el.AutomationId;
125 className = el.ClassName;
126 Process p = Process.GetProcessById(proc);
127 ProcessName = p.ProcessName;
130 public Action(Type t,
int proc, KeyPressEventArgs e)
134 AutomationElement el = Element;
136 automationID = el.AutomationId;
137 className = el.ClassName;
138 Process p = Process.GetProcessById(proc);
139 ProcessName = p.ProcessName;
142 public Action(Type t,
int proc, MouseEventArgs e)
146 AutomationElement el = Element;
150 automationID = el.AutomationId;
156 className = el.Properties.LocalizedControlType.Value.ToString();
157 mouseButton = e.Button;
158 point =
new Point((
int)(e.Location.X - el.BoundingRectangle.X), (
int)(e.Location.Y - el.BoundingRectangle.Y));
159 Process p = Process.GetProcessById(proc);
160 title = p.MainWindowTitle;
161 ProcessName = p.ProcessName;
162 Index = AutomationHelpers.GetIndex(ProcessName, Title, AutomationID, Name, ClassName, e.Location);
164 public Action(Type t,
string process,
string title,
int index,
string id,
string name,
string classname, MouseEventArgs mo)
167 this.automationID = id;
168 this.process = process;
170 this.className = classname;
171 this.mouseButton = mo.Button;
178 public Action(Type t,
string process,
string title,
int index,
string id,
string name,
string classname, KeyEventArgs ke)
182 this.automationID = id;
184 this.process = process;
185 this.className = classname;
190 public Action(Type t,
string process,
string title,
int index,
string id,
string name,
string classname, KeyPressEventArgs ke)
194 this.automationID = id;
195 this.process = process;
197 this.className = classname;
202 public Action(Type t,
string process,
string title,
int index,
string id,
string name,
string classname)
204 this.automationID = id;
206 this.className = classname;
207 this.automationID = id;
208 this.process = process;
210 this.className = classname;
215 public void Perform()
217 Stopwatch stopwatch =
new Stopwatch();
219 AutomationElement el = AutomationHelpers.GetElementByProcess(ProcessName, Title, AutomationID, Name, ClassName, Index);
220 if (type == Type.mousedown)
222 Cursor.Position =
new Point((
int)el.BoundingRectangle.X + Point.X, (
int)el.BoundingRectangle.Y + Point.Y);
223 if (mouse.Button == MouseButtons.Left)
226 if (mouse.Button == MouseButtons.Right)
229 else if (type == Type.mouseup)
231 Cursor.Position =
new Point((
int)el.BoundingRectangle.X + Point.X, (
int)el.BoundingRectangle.Y + Point.Y);
232 if (mouse.Button == MouseButtons.Left)
234 if (mouse.Button == MouseButtons.Right)
237 else if (type == Type.keydown)
240 input.Keyboard.KeyDown((VirtualKeyCode)key.KeyValue);
242 else if (type == Type.keyup)
245 input.Keyboard.KeyUp((VirtualKeyCode)key.KeyValue);
247 else if (type == Type.keypress)
250 SendKeys.Send(keyPress.KeyChar.ToString());
252 else if (type == Type.wheelup)
258 else if (type == Type.wheeldown)
265 ticks = stopwatch.ElapsedMilliseconds;
268 public override string ToString()
270 if(type == Type.keyup || type == Type.keydown)
271 return Name +
", " + AutomationID +
", " + ClassName +
", " + ActionType +
", " + Index +
", " + key.KeyValue;
273 if (type == Type.keypress)
274 return Name +
", " + AutomationID +
", " + ClassName +
", " + ActionType +
", " + Index +
", " + keyPress.KeyChar;
276 if (type == Type.mouseup || type == Type.mousedown)
277 return Name +
", " + AutomationID +
", " + ClassName +
", " + ActionType +
", " + Index +
", " + point.ToString();
279 return Name +
", " + AutomationID +
", " + ClassName +
", " + ActionType +
", " + Index +
", " + mouse.Delta;
290 set { name = value; }
296 set { file = value; }
298 private List<Action> list =
new List<Action>();
299 public List<Action> List
302 set { list = value; }
307 public Recording(
string fil,List<Action> l)
311 Name = System.IO.Path.GetFileNameWithoutExtension(fil);
315 foreach (
Action a
in list)
323 for (
int i = 0; i < list.Count; i++)
326 if (i == list.Count - 1)
331 AutomationElement ae = AutomationHelpers.GetElementByProcess(la.ProcessName, la.Title, la.AutomationID, la.Name, la.ClassName, la.Index);
332 if (la.Value ==
Action.ValueType.Name)
334 else if (la.Value ==
Action.ValueType.SelectionPattern)
335 return AutomationHelpers.GetSelection(ae);
336 else if (la.Value ==
Action.ValueType.TextPattern)
337 return AutomationHelpers.GetText(ae);
338 else if (la.Value ==
Action.ValueType.ValuePattern)
339 return AutomationHelpers.GetValue(ae);
340 else if (la.Value ==
Action.ValueType.TogglePattern)
341 return AutomationHelpers.GetToggle(ae);
343 return AutomationHelpers.GetImage(ae);
345 public Bitmap GetImage()
348 AutomationElement ae = AutomationHelpers.GetElementByProcess(la.ProcessName, la.Title, la.AutomationID, la.Name, la.ClassName, la.Index);
349 return AutomationHelpers.GetImage(ae);
351 public Rectangle GetBounds()
354 AutomationElement ae = AutomationHelpers.GetElementByProcess(la.ProcessName, la.Title, la.AutomationID, la.Name, la.ClassName, la.Index);
355 return ae.BoundingRectangle;
357 public void Set(
string val)
359 for (
int i = 0; i < list.Count; i++)
362 if (i == list.Count - 1)
367 AutomationElement ae = AutomationHelpers.GetElementByProcess(la.ProcessName, la.Title, la.AutomationID, la.Name, la.ClassName, la.Index);
368 AutomationHelpers.SetValue(ae, val);
370 public override string ToString()
377 public static bool IsRecording
379 get {
return recording; }
382 private IKeyboardMouseEvents m_GlobalHook;
383 private static bool recording =
false;
384 private static bool move =
false;
385 public static Recording rec =
new Recording();
389 m_GlobalHook = Hook.GlobalEvents();
390 m_GlobalHook.MouseDownExt += GlobalHookMouseDownExt;
391 m_GlobalHook.MouseUpExt += GlobalHookMouseUpExt;
392 m_GlobalHook.MouseMoveExt += GlobalHookMouseMoveExt;
393 m_GlobalHook.KeyPress += GlobalHookKeyPress;
394 m_GlobalHook.KeyUp += GlobalHookKeyUp;
395 m_GlobalHook.KeyDown += GlobalHookKeyDown;
396 m_GlobalHook.MouseWheel += GlobalHookMouseWheel;
397 input =
new InputSimulator();
402 public static AutomationElement Element
408 return automation.FromPoint(
new Point(Cursor.Position.X, Cursor.Position.Y));
430 public static string recname;
443 Properties.Add(rec.Name,rec);
452 Recorder.AddLine(
"Automation.GetProperty(" +
'"' + prop +
'"' +
");");
453 return Properties[prop].Get();
463 Recorder.AddLine(
"Automation.SetProperty(" +
'"' + prop +
'"' +
"," +
'"' + val +
'"' +
");");
471 if (Properties.ContainsKey(rec.Name))
472 Properties[rec.Name] = rec;
474 Properties.Add(rec.Name, rec);
483 Recorder.AddLine(
"Automation.RunRecording(" +
'"' + prop +
'"' +
");");
493 private void GlobalHookMouseDownExt(
object sender, MouseEventExtArgs e)
497 AutomationElement el = Element;
500 rec.List.Add(
new Action(
Action.Type.mousedown, el.Properties.ProcessId, e));
510 private void GlobalHookMouseUpExt(
object sender, MouseEventExtArgs e)
514 AutomationElement el = Element;
517 rec.List.Add(
new Action(Action.Type.mouseup, el.Properties.ProcessId, e));
527 private void GlobalHookMouseWheel(
object sender, MouseEventArgs e)
531 AutomationElement el = Element;
535 rec.List.Add(
new Action(Action.Type.wheelup, el.Properties.ProcessId, e));
537 rec.List.Add(
new Action(Action.Type.wheeldown, el.Properties.ProcessId, e));
547 private void GlobalHookMouseMoveExt(
object sender, MouseEventExtArgs e)
553 AutomationElement el = Element;
556 rec.List.Add(
new Action(Action.Type.mousemove, el.Properties.ProcessId, e));
567 private void GlobalHookKeyPress(
object sender, KeyPressEventArgs e)
571 AutomationElement el = Element;
574 rec.List.Add(
new Action(Action.Type.keypress, el.Properties.ProcessId, e));
584 private void GlobalHookKeyUp(
object sender, KeyEventArgs e)
588 AutomationElement el = Element;
591 rec.List.Add(
new Action(Action.Type.keyup, el.Properties.ProcessId, e));
601 private void GlobalHookKeyDown(
object sender, KeyEventArgs e)
605 AutomationElement el = Element;
608 rec.List.Add(
new Action(Action.Type.keydown, el.Properties.ProcessId, e));
612 public static class AutomationHelpers
614 public static List<AutomationElement> GetChildren(AutomationElement parent)
619 throw new ArgumentException();
621 AutomationElement[] collection = automation.GetDesktop().FindAll(TreeScope.Children, FlaUI.Core.Conditions.TrueCondition.Default);
622 if (collection !=
null)
624 List<AutomationElement> result =
new List<AutomationElement>(collection.Cast<AutomationElement>());
633 public static List<AutomationElement> GetAllChildren(AutomationElement parent)
638 throw new ArgumentException();
642 AutomationElement[] collection = automation.GetDesktop().FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
643 if (collection !=
null)
645 List<AutomationElement> result =
new List<AutomationElement>(collection.Cast<AutomationElement>());
659 public static List<AutomationElement> GetName(AutomationElement parent,
string name)
664 throw new ArgumentException();
666 AutomationElement[] collection = automation.GetDesktop().FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
667 if (collection !=
null)
669 List<AutomationElement> result =
new List<AutomationElement>();
670 foreach (AutomationElement item
in collection)
672 if (item.Name == name)
685 public static List<AutomationElement> GetAutomationID(AutomationElement parent,
string id)
690 throw new ArgumentException();
692 AutomationElement[] collection = automation.GetDesktop().FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
693 if (collection !=
null)
695 List<AutomationElement> result =
new List<AutomationElement>();
696 foreach (AutomationElement item
in collection)
698 if (item.AutomationId ==
id)
711 public static List<AutomationElement> GetClassName(AutomationElement parent,
string name)
716 throw new ArgumentException();
718 AutomationElement[] collection = automation.GetDesktop().FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
719 if (collection !=
null)
721 List<AutomationElement> result =
new List<AutomationElement>();
722 foreach (AutomationElement item
in collection)
724 if (item.ClassName == name)
737 public static AutomationElement GetElementByProcess(
string process,
string title,
string id,
string name,
string classname,
int index)
739 Process[] procs = Process.GetProcessesByName(process);
742 ti = procs[0].MainWindowTitle;
743 AutomationElement root = automation.GetDesktop();
744 AutomationElement[] prs = automation.GetDesktop().FindAll(TreeScope.Children, FlaUI.Core.Conditions.TrueCondition.Default);
745 AutomationElement window =
null;
746 List<AutomationElement> ats =
new List<AutomationElement>();
747 foreach (AutomationElement item
in prs)
751 if (item.Name.Contains(ti))
763 throw new ArgumentException(
"Window " + title +
" not found.");
764 AutomationElement[] items = window.FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
765 List<AutomationElement> result =
new List<AutomationElement>();
766 foreach (AutomationElement item
in items)
770 if (item.Properties.LocalizedControlType == classname && item.Name == name)
783 return result[index];
785 public static int GetIndex(
string process,
string title,
string id,
string name,
string classname, Point p)
788 Process[] procs = Process.GetProcessesByName(process);
791 ti = procs[0].MainWindowTitle;
792 AutomationElement[] prs = automation.GetDesktop().FindAll(TreeScope.Children, FlaUI.Core.Conditions.TrueCondition.Default);
793 AutomationElement window =
null;
794 List<AutomationElement> ats =
new List<AutomationElement>();
795 foreach (AutomationElement item
in prs)
799 if (item.Name.Contains(title))
812 AutomationElement[] items =
null;
815 items = window.FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
821 List<AutomationElement> result =
new List<AutomationElement>();
848 public static int[] StringToRuntimeID(
string s)
850 int[] r =
new int[3];
851 string[] sts = s.Split(
',');
852 for (
int i = 0; i < 3; i++)
854 r[i] =
int.Parse(sts[i]);
858 public static List<AutomationElement> GetElement(
string id,
string name)
860 AutomationElement parent = automation.GetDesktop();
861 AutomationElement[] collection = automation.GetDesktop().FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
862 if (collection !=
null)
864 List<AutomationElement> result =
new List<AutomationElement>();
865 foreach (AutomationElement item
in collection)
867 if (item.AutomationId ==
id && item.Name == name)
880 public static List<AutomationElement> GetLocalizedControlType(AutomationElement parent, TreeScope scope,
string type)
885 throw new ArgumentException();
887 AutomationElement[] collection = automation.GetDesktop().FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
888 if (collection !=
null)
890 List<AutomationElement> result =
new List<AutomationElement>();
891 foreach (AutomationElement item
in collection)
893 if (item.Properties.LocalizedControlType == type)
906 public static AutomationElement[] GetRawChildren(AutomationElement parent)
911 throw new ArgumentException();
913 return parent.FindAll(TreeScope.Children, FlaUI.Core.Conditions.TrueCondition.Default);
915 public static List<AutomationElement> GetRawAutomationID(AutomationElement parent,
string auto)
920 throw new ArgumentException();
922 List<AutomationElement> ress =
new List<AutomationElement>();
923 AutomationElement[] all = parent.FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
924 foreach (AutomationElement item
in all)
926 if (item.AutomationId ==
auto)
933 public static List<AutomationElement> GetRawLocalizedControlType(AutomationElement parent,
string auto)
938 throw new ArgumentException();
940 List<AutomationElement> ress =
new List<AutomationElement>();
941 AutomationElement[] all = parent.FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
942 foreach (AutomationElement item
in all)
944 if (item.Properties.LocalizedControlType ==
auto)
951 public static AutomationElement[] GetRaw(AutomationElement parent)
956 throw new ArgumentException();
958 List<AutomationElement> ress =
new List<AutomationElement>();
959 return parent.FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
961 public static string GetValue(AutomationElement control)
963 if(control.ControlType == ControlType.Edit)
965 FlaUI.Core.AutomationElements.TextBox tb = control.AsTextBox();
968 else if(control.ControlType == ControlType.Text)
970 return control.AsLabel().Text;
974 public static string GetSelection(AutomationElement control)
976 object patternProvider;
977 FlaUI.Core.AutomationElements.ListBox lb = control.AsListBox();
978 return lb.SelectedItem.Name;
980 public static string GetText(AutomationElement control)
982 return control.AsLabel().Text;
984 public static void SetValue(AutomationElement targetControl,
string value)
988 throw new ArgumentNullException(
989 "String parameter must not be null.");
990 if (targetControl ==
null)
991 throw new ArgumentNullException(
992 "AutomationElement parameter must not be null");
1000 if (!targetControl.IsEnabled)
1002 throw new InvalidOperationException(
1003 "The control is not enabled.\n\n");
1005 targetControl.AsTextBox().Text = value;
1007 public static int GetCount(AutomationElement parent)
1012 throw new ArgumentException();
1015 return parent.FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default).Length;
1017 public static bool GetToggle(AutomationElement element)
1019 if (element ==
null)
1023 ToggleButton tb = element.AsToggleButton();
1024 if (tb.TogglePattern.ToggleState.ValueOrDefault.HasFlag(ToggleState.On))
1029 public static Bitmap GetImage(AutomationElement el)
1031 System.Drawing.Graphics g;
1032 Rectangle r = el.BoundingRectangle;
1033 Bitmap b =
new Bitmap((
int)r.Width, (
int)r.Height);
1034 g = System.Drawing.Graphics.FromImage(b);
1035 g.CopyFromScreen(
new Point((
int)r.X,(
int)r.Y),
new Point(0, 0),
new Size(r.Width,r.Height));