1using FlaUI.Core.AutomationElements;
2using FlaUI.Core.Definitions;
4using System.Diagnostics;
6using Gma.System.MouseKeyHook;
11 public static InputSimulator
input;
12 public static Dictionary<string, Recording>
Recordings =
new Dictionary<string, Recording>();
13 public static Dictionary<string, Recording>
Properties =
new Dictionary<string, Recording>();
14 public static UIA3Automation
automation =
new UIA3Automation();
18 private KeyEventArgs key;
19 private KeyPressEventArgs keyPress;
20 private string automationID;
22 private string className;
23 private string process;
26 private System.Drawing.Point point;
28 private MouseEventArgs mouse;
29 private MouseButtons mouseButton;
66 return (
Keys)keyPress.KeyChar;
73 get {
return mouseButton; }
74 set { mouseButton = value; }
76 public System.Drawing.Point
Point
79 set { point = value; }
83 get {
return automationID; }
84 set { automationID = value; }
93 get {
return className; }
94 set { className = value; }
98 get {
return process; }
99 set { process = value; }
103 get {
return title; }
104 set { title = value; }
108 get {
return index; }
109 set { index = value; }
113 get {
return autoValue; }
114 set { autoValue = value; }
120 AutomationElement el =
Element;
122 automationID = el.AutomationId;
123 className = el.ClassName;
124 Process p = Process.GetProcessById(proc);
132 AutomationElement el =
Element;
134 automationID = el.AutomationId;
135 className = el.ClassName;
136 Process p = Process.GetProcessById(proc);
144 AutomationElement el =
Element;
148 automationID = el.AutomationId;
154 className = el.Properties.LocalizedControlType.Value.ToString();
155 mouseButton = e.Button;
156 point =
new Point((
int)(e.Location.X - el.BoundingRectangle.X), (
int)(e.Location.Y - el.BoundingRectangle.Y));
157 Process p = Process.GetProcessById(proc);
158 title = p.MainWindowTitle;
162 public Action(
Type t,
string process,
string title,
int index,
string id,
string name,
string classname, MouseEventArgs mo)
165 this.automationID = id;
166 this.process = process;
168 this.className = classname;
169 this.mouseButton = mo.Button;
176 public Action(
Type t,
string process,
string title,
int index,
string id,
string name,
string classname, KeyEventArgs ke)
180 this.automationID = id;
182 this.process = process;
183 this.className = classname;
188 public Action(
Type t,
string process,
string title,
int index,
string id,
string name,
string classname, KeyPressEventArgs ke)
192 this.automationID = id;
193 this.process = process;
195 this.className = classname;
200 public Action(
Type t,
string process,
string title,
int index,
string id,
string name,
string classname)
202 this.automationID = id;
204 this.className = classname;
205 this.automationID = id;
206 this.process = process;
208 this.className = classname;
215 Stopwatch stopwatch =
new Stopwatch();
218 if (type ==
Type.mousedown)
220 Cursor.Position =
new Point((
int)el.BoundingRectangle.X +
Point.X, (
int)el.BoundingRectangle.Y +
Point.Y);
221 if (mouse.Button == MouseButtons.Left)
224 if (mouse.Button == MouseButtons.Right)
227 else if (type ==
Type.mouseup)
229 Cursor.Position =
new Point((
int)el.BoundingRectangle.X +
Point.X, (
int)el.BoundingRectangle.Y +
Point.Y);
230 if (mouse.Button == MouseButtons.Left)
232 if (mouse.Button == MouseButtons.Right)
235 else if (type ==
Type.keydown)
238 input.Keyboard.KeyDown((VirtualKeyCode)key.KeyValue);
240 else if (type ==
Type.keyup)
243 input.Keyboard.KeyUp((VirtualKeyCode)key.KeyValue);
245 else if (type ==
Type.keypress)
248 SendKeys.Send(keyPress.KeyChar.ToString());
250 else if (type ==
Type.wheelup)
256 else if (type ==
Type.wheeldown)
263 ticks = stopwatch.ElapsedMilliseconds;
268 if(type ==
Type.keyup || type ==
Type.keydown)
271 if (type ==
Type.keypress)
274 if (type ==
Type.mouseup || type ==
Type.mousedown)
288 set { name = value; }
294 set { file = value; }
300 set { list = value; }
309 Name = System.IO.Path.GetFileNameWithoutExtension(fil);
313 foreach (
Action a
in list)
321 for (
int i = 0; i < list.Count; i++)
324 if (i == list.Count - 1)
353 return ae.BoundingRectangle;
355 public void Set(
string val)
357 for (
int i = 0; i < list.Count; i++)
360 if (i == list.Count - 1)
377 get {
return recording; }
380 private IKeyboardMouseEvents m_GlobalHook;
381 private static bool recording =
false;
382 private static bool move =
false;
387 m_GlobalHook = Hook.GlobalEvents();
388 m_GlobalHook.MouseDownExt += GlobalHookMouseDownExt;
389 m_GlobalHook.MouseUpExt += GlobalHookMouseUpExt;
390 m_GlobalHook.MouseMoveExt += GlobalHookMouseMoveExt;
391 m_GlobalHook.KeyPress += GlobalHookKeyPress;
392 m_GlobalHook.KeyUp += GlobalHookKeyUp;
393 m_GlobalHook.KeyDown += GlobalHookKeyDown;
394 m_GlobalHook.MouseWheel += GlobalHookMouseWheel;
395 input =
new InputSimulator();
406 return automation.FromPoint(
new Point(Cursor.Position.X, Cursor.Position.Y));
461 Recorder.
AddLine(
"Automation.SetProperty(" +
'"' + prop +
'"' +
"," +
'"' + val +
'"' +
");");
481 Recorder.
AddLine(
"Automation.RunRecording(" +
'"' + prop +
'"' +
");");
491 private void GlobalHookMouseDownExt(
object sender, MouseEventExtArgs e)
495 AutomationElement el =
Element;
498 rec.
List.Add(
new Action(Action.Type.mousedown, el.Properties.ProcessId, e));
508 private void GlobalHookMouseUpExt(
object sender, MouseEventExtArgs e)
512 AutomationElement el =
Element;
515 rec.
List.Add(
new Action(Action.Type.mouseup, el.Properties.ProcessId, e));
525 private void GlobalHookMouseWheel(
object sender, MouseEventArgs e)
529 AutomationElement el =
Element;
533 rec.
List.Add(
new Action(Action.Type.wheelup, el.Properties.ProcessId, e));
535 rec.
List.Add(
new Action(Action.Type.wheeldown, el.Properties.ProcessId, e));
545 private void GlobalHookMouseMoveExt(
object sender, MouseEventExtArgs e)
551 AutomationElement el =
Element;
554 rec.
List.Add(
new Action(Action.Type.mousemove, el.Properties.ProcessId, e));
565 private void GlobalHookKeyPress(
object sender, KeyPressEventArgs e)
569 AutomationElement el =
Element;
572 rec.
List.Add(
new Action(Action.Type.keypress, el.Properties.ProcessId, e));
582 private void GlobalHookKeyUp(
object sender, KeyEventArgs e)
586 AutomationElement el =
Element;
589 rec.
List.Add(
new Action(Action.Type.keyup, el.Properties.ProcessId, e));
599 private void GlobalHookKeyDown(
object sender, KeyEventArgs e)
603 AutomationElement el =
Element;
606 rec.
List.Add(
new Action(Action.Type.keydown, el.Properties.ProcessId, e));
612 public static List<AutomationElement>
GetChildren(AutomationElement parent)
617 throw new ArgumentException();
619 AutomationElement[] collection =
automation.GetDesktop().FindAll(TreeScope.Children, FlaUI.Core.Conditions.TrueCondition.Default);
620 if (collection !=
null)
622 List<AutomationElement> result =
new List<AutomationElement>(collection.Cast<AutomationElement>());
636 throw new ArgumentException();
640 AutomationElement[] collection =
automation.GetDesktop().FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
641 if (collection !=
null)
643 List<AutomationElement> result =
new List<AutomationElement>(collection.Cast<AutomationElement>());
657 public static List<AutomationElement>
GetName(AutomationElement parent,
string name)
662 throw new ArgumentException();
664 AutomationElement[] collection =
automation.GetDesktop().FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
665 if (collection !=
null)
667 List<AutomationElement> result =
new List<AutomationElement>();
668 foreach (AutomationElement item
in collection)
670 if (item.Name == name)
683 public static List<AutomationElement>
GetAutomationID(AutomationElement parent,
string id)
688 throw new ArgumentException();
690 AutomationElement[] collection =
automation.GetDesktop().FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
691 if (collection !=
null)
693 List<AutomationElement> result =
new List<AutomationElement>();
694 foreach (AutomationElement item
in collection)
696 if (item.AutomationId ==
id)
709 public static List<AutomationElement>
GetClassName(AutomationElement parent,
string name)
714 throw new ArgumentException();
716 AutomationElement[] collection =
automation.GetDesktop().FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
717 if (collection !=
null)
719 List<AutomationElement> result =
new List<AutomationElement>();
720 foreach (AutomationElement item
in collection)
722 if (item.ClassName == name)
735 public static AutomationElement
GetElementByProcess(
string process,
string title,
string id,
string name,
string classname,
int index)
737 Process[] procs = Process.GetProcessesByName(process);
740 ti = procs[0].MainWindowTitle;
741 AutomationElement root =
automation.GetDesktop();
742 AutomationElement[] prs =
automation.GetDesktop().FindAll(TreeScope.Children, FlaUI.Core.Conditions.TrueCondition.Default);
743 AutomationElement window =
null;
744 List<AutomationElement> ats =
new List<AutomationElement>();
745 foreach (AutomationElement item
in prs)
749 if (item.Name.Contains(ti))
761 throw new ArgumentException(
"Window " + title +
" not found.");
762 AutomationElement[] items = window.FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
763 List<AutomationElement> result =
new List<AutomationElement>();
764 foreach (AutomationElement item
in items)
768 if (item.Properties.LocalizedControlType == classname && item.Name == name)
781 return result[index];
783 public static int GetIndex(
string process,
string title,
string id,
string name,
string classname,
Point p)
786 Process[] procs = Process.GetProcessesByName(process);
789 ti = procs[0].MainWindowTitle;
790 AutomationElement[] prs =
automation.GetDesktop().FindAll(TreeScope.Children, FlaUI.Core.Conditions.TrueCondition.Default);
791 AutomationElement window =
null;
792 List<AutomationElement> ats =
new List<AutomationElement>();
793 foreach (AutomationElement item
in prs)
797 if (item.Name.Contains(title))
810 AutomationElement[] items =
null;
813 items = window.FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
819 List<AutomationElement> result =
new List<AutomationElement>();
822 foreach (AutomationElement item
in items)
826 int[] r = item.Properties.RuntimeId;
827 if (item.ClassName == classname && item.AutomationId ==
id && item.Name == name)
831 if (item.BoundingRectangle.IntersectsWith(
new System.Drawing.Rectangle(p.X, p.Y, 1, 1)) && result.Count > 0)
833 index = result.Count - 1;
845 int[] r =
new int[3];
846 string[] sts = s.Split(
',');
847 for (
int i = 0; i < 3; i++)
849 r[i] =
int.Parse(sts[i]);
853 public static List<AutomationElement>
GetElement(
string id,
string name)
855 AutomationElement parent =
automation.GetDesktop();
856 AutomationElement[] collection =
automation.GetDesktop().FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
857 if (collection !=
null)
859 List<AutomationElement> result =
new List<AutomationElement>();
860 foreach (AutomationElement item
in collection)
862 if (item.AutomationId ==
id && item.Name == name)
880 throw new ArgumentException();
882 AutomationElement[] collection =
automation.GetDesktop().FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
883 if (collection !=
null)
885 List<AutomationElement> result =
new List<AutomationElement>();
886 foreach (AutomationElement item
in collection)
888 if (item.Properties.LocalizedControlType == type)
906 throw new ArgumentException();
908 return parent.FindAll(TreeScope.Children, FlaUI.Core.Conditions.TrueCondition.Default);
915 throw new ArgumentException();
917 List<AutomationElement> ress =
new List<AutomationElement>();
918 AutomationElement[] all = parent.FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
919 foreach (AutomationElement item
in all)
921 if (item.AutomationId ==
auto)
933 throw new ArgumentException();
935 List<AutomationElement> ress =
new List<AutomationElement>();
936 AutomationElement[] all = parent.FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
937 foreach (AutomationElement item
in all)
939 if (item.Properties.LocalizedControlType ==
auto)
946 public static AutomationElement[]
GetRaw(AutomationElement parent)
951 throw new ArgumentException();
953 List<AutomationElement> ress =
new List<AutomationElement>();
954 return parent.FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
956 public static string GetValue(AutomationElement control)
958 if(control.ControlType == ControlType.Edit)
960 FlaUI.Core.AutomationElements.TextBox tb = control.AsTextBox();
963 else if(control.ControlType == ControlType.Text)
965 return control.AsLabel().Text;
971 object patternProvider;
972 FlaUI.Core.AutomationElements.ListBox lb = control.AsListBox();
973 return lb.SelectedItem.Name;
975 public static string GetText(AutomationElement control)
977 return control.AsLabel().Text;
979 public static void SetValue(AutomationElement targetControl,
string value)
983 throw new ArgumentNullException(
984 "String parameter must not be null.");
985 if (targetControl ==
null)
986 throw new ArgumentNullException(
987 "AutomationElement parameter must not be null");
995 if (!targetControl.IsEnabled)
997 throw new InvalidOperationException(
998 "The control is not enabled.\n\n");
1000 targetControl.AsTextBox().Text = value;
1007 throw new ArgumentException();
1010 return parent.FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default).Length;
1014 if (element ==
null)
1018 ToggleButton tb = element.AsToggleButton();
1019 if (tb.TogglePattern.ToggleState.ValueOrDefault.HasFlag(ToggleState.On))
1024 public static System.Drawing.Bitmap
GetImage(AutomationElement el)
1026 System.Drawing.Graphics g;
1027 System.Drawing.Rectangle r = el.BoundingRectangle;
1028 System.Drawing.Bitmap b =
new System.Drawing.Bitmap((
int)r.Width, (
int)r.Height);
1029 g = System.Drawing.Graphics.FromImage(b);
1030 g.CopyFromScreen(
new Point((
int)r.X,(
int)r.Y),
new Point(0, 0),
new Size(r.Width,r.Height));
System.Windows.Forms.Keys Keys
System.Drawing.Point Point
Action(Type t, int proc, MouseEventArgs e)
System.Drawing.Point Point
Action(Type t, string process, string title, int index, string id, string name, string classname, KeyPressEventArgs ke)
override string ToString()
Action(Type t, string process, string title, int index, string id, string name, string classname, MouseEventArgs mo)
Action(Type t, string process, string title, int index, string id, string name, string classname, KeyEventArgs ke)
Action(Type t, string process, string title, int index, string id, string name, string classname)
Action(Type t, int proc, KeyEventArgs e)
Action(Type t, int proc, KeyPressEventArgs e)
static List< AutomationElement > GetRawLocalizedControlType(AutomationElement parent, string auto)
static AutomationElement[] GetRawChildren(AutomationElement parent)
static int[] StringToRuntimeID(string s)
static List< AutomationElement > GetClassName(AutomationElement parent, string name)
static int GetCount(AutomationElement parent)
static List< AutomationElement > GetRawAutomationID(AutomationElement parent, string auto)
static List< AutomationElement > GetLocalizedControlType(AutomationElement parent, TreeScope scope, string type)
static List< AutomationElement > GetAutomationID(AutomationElement parent, string id)
static List< AutomationElement > GetName(AutomationElement parent, string name)
static int GetIndex(string process, string title, string id, string name, string classname, Point p)
static string GetText(AutomationElement control)
static AutomationElement[] GetRaw(AutomationElement parent)
static List< AutomationElement > GetElement(string id, string name)
static System.Drawing.Bitmap GetImage(AutomationElement el)
static bool GetToggle(AutomationElement element)
static List< AutomationElement > GetChildren(AutomationElement parent)
static List< AutomationElement > GetAllChildren(AutomationElement parent)
static string GetValue(AutomationElement control)
static void SetValue(AutomationElement targetControl, string value)
static string GetSelection(AutomationElement control)
static AutomationElement GetElementByProcess(string process, string title, string id, string name, string classname, int index)
System.Drawing.Bitmap GetImage()
Recording(string fil, List< Action > l)
System.Drawing.Rectangle GetBounds()
override string ToString()
static UIA3Automation automation
static InputSimulator input
static object GetProperty(string prop)
static void StartRecording()
It starts recording.
static void StopPropertyRecording()
Stop recording the property and add it to the list of properties.
static AutomationElement Element
static Dictionary< string, Recording > Properties
static void StartPropertyRecording(string name)
static void SetProperty(string prop, string val)
static void StopRecording()
It stops the recording and adds it to the list of recordings.
static void AddProperty(Recording rec)
static void RunRecording(string prop)
static void AddLine(string s)
static string GetActiveWindowTitle()
static void MouseWheelDown()
MouseWheelDown() simulates a mouse wheel down event.
static void SetFocus(string process)
static void MouseWheelUp()
"MouseWheelUp()" is a function that simulates a mouse wheel up event.