BioImager  5.0.0
A .NET microscopy imaging application based on Bio library. Supports various microscopes by using imported libraries & GUI automation. Supports XInput game controllers to move stage, take images, run ImageJ macros on images or Bio C# scripts.
Loading...
Searching...
No Matches
Automation.cs
Go to the documentation of this file.
1using FlaUI.Core.AutomationElements;
2using FlaUI.Core.Definitions;
3using FlaUI.Core.Identifiers;
4using FlaUI.UIA3;
5using Gma.System.MouseKeyHook;
6using System.Diagnostics;
7using WindowsInput;
8
9namespace BioImager
10{
11 public class Automation
12 {
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();
17 /* It's a class that represents an action that can be performed on an automation element */
18 public class Action
19 {
20 private KeyEventArgs key;
21 private KeyPressEventArgs keyPress;
22 private string automationID;
23 private string name;
24 private string className;
25 private string process;
26 private string title;
27 private int index;
28 private System.Drawing.Point point;
29 private long ticks;
30 private MouseEventArgs mouse;
31 private MouseButtons mouseButton;
32 private ValueType autoValue;
33 public enum Type
34 {
36 mouseup,
39 wheelup,
40 keyup,
41 keydown,
43 }
44 public enum ValueType
45 {
46 Name,
51 Image,
52 }
53
54 private Type type;
56 {
57 get { return type; }
58 set { type = value; }
59 }
60 public Keys Key
61 {
62 get
63 {
64 if (key != null)
65 return key.KeyCode;
66 else
67 if (keyPress != null)
68 return (Keys)keyPress.KeyChar;
69 else
70 return Keys.None;
71 }
72 }
73 public MouseButtons Button
74 {
75 get { return mouseButton; }
76 set { mouseButton = value; }
77 }
78 public System.Drawing.Point Point
79 {
80 get { return point; }
81 set { point = value; }
82 }
83 public string AutomationID
84 {
85 get { return automationID; }
86 set { automationID = value; }
87 }
88 public string Name
89 {
90 get { return name; }
91 set { name = value; }
92 }
93 public string ClassName
94 {
95 get { return className; }
96 set { className = value; }
97 }
98 public string ProcessName
99 {
100 get { return process; }
101 set { process = value; }
102 }
103 public string Title
104 {
105 get { return title; }
106 set { title = value; }
107 }
108 public int Index
109 {
110 get { return index; }
111 set { index = value; }
112 }
114 {
115 get { return autoValue; }
116 set { autoValue = value; }
117 }
118 public Action(Type t, int proc, KeyEventArgs e)
119 {
120 type = t;
121 key = e;
122 AutomationElement el = Element;
123 name = el.Name;
124 automationID = el.AutomationId;
125 className = el.ClassName;
126 Process p = Process.GetProcessById(proc);
127 ProcessName = p.ProcessName;
128 title = Win32.GetActiveWindowTitle();
129 }
130 public Action(Type t, int proc, KeyPressEventArgs e)
131 {
132 type = t;
133 keyPress = e;
134 AutomationElement el = Element;
135 name = el.Name;
136 automationID = el.AutomationId;
137 className = el.ClassName;
138 Process p = Process.GetProcessById(proc);
139 ProcessName = p.ProcessName;
140 title = Win32.GetActiveWindowTitle();
141 }
142 public Action(Type t, int proc, MouseEventArgs e)
143 {
144 type = t;
145 mouse = e;
146 AutomationElement el = Element;
147 name = el.Name;
148 try
149 {
150 automationID = el.AutomationId;
151 }
152 catch (Exception)
153 {
154 }
155
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;
163 }
164 public Action(Type t, string process, string title, int index, string id, string name, string classname, MouseEventArgs mo)
165 {
166 type = t;
167 this.automationID = id;
168 this.process = process;
169 this.name = name;
170 this.className = classname;
171 this.mouseButton = mo.Button;
172 this.title = title;
173 this.index = index;
174 type = t;
175 point = mo.Location;
176 mouse = mo;
177 }
178 public Action(Type t, string process, string title, int index, string id, string name, string classname, KeyEventArgs ke)
179 {
180 type = t;
181 key = ke;
182 this.automationID = id;
183 this.name = name;
184 this.process = process;
185 this.className = classname;
186 this.index = index;
187 this.title = title;
188 type = t;
189 }
190 public Action(Type t, string process, string title, int index, string id, string name, string classname, KeyPressEventArgs ke)
191 {
192 type = t;
193 keyPress = ke;
194 this.automationID = id;
195 this.process = process;
196 this.name = name;
197 this.className = classname;
198 this.index = index;
199 this.title = title;
200 type = t;
201 }
202 public Action(Type t, string process, string title, int index, string id, string name, string classname)
203 {
204 this.automationID = id;
205 this.name = name;
206 this.className = classname;
207 this.automationID = id;
208 this.process = process;
209 this.name = name;
210 this.className = classname;
211 this.index = index;
212 this.title = title;
213 type = t;
214 }
215 public void Perform()
216 {
217 Stopwatch stopwatch = new Stopwatch();
218 stopwatch.Start();
220 if (type == Type.mousedown)
221 {
222 Cursor.Position = new Point((int)el.BoundingRectangle.X + Point.X, (int)el.BoundingRectangle.Y + Point.Y);
223 if (mouse.Button == MouseButtons.Left)
225
226 if (mouse.Button == MouseButtons.Right)
228 }
229 else if (type == Type.mouseup)
230 {
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)
236 }
237 else if (type == Type.keydown)
238 {
240 input.Keyboard.KeyDown((VirtualKeyCode)key.KeyValue);
241 }
242 else if (type == Type.keyup)
243 {
245 input.Keyboard.KeyUp((VirtualKeyCode)key.KeyValue);
246 }
247 else if (type == Type.keypress)
248 {
250 SendKeys.Send(keyPress.KeyChar.ToString());
251 }
252 else if (type == Type.wheelup)
253 {
255 //input.Mouse.VerticalScroll(1);
257 }
258 else if (type == Type.wheeldown)
259 {
261 //input.Mouse.VerticalScroll(-1);
263 }
264 stopwatch.Stop();
265 ticks = stopwatch.ElapsedMilliseconds;
266 stopwatch = null;
267 }
268 public override string ToString()
269 {
270 if(type == Type.keyup || type == Type.keydown)
271 return Name + ", " + AutomationID + ", " + ClassName + ", " + ActionType + ", " + Index + ", " + key.KeyValue;
272 else
273 if (type == Type.keypress)
274 return Name + ", " + AutomationID + ", " + ClassName + ", " + ActionType + ", " + Index + ", " + keyPress.KeyChar;
275 else
276 if (type == Type.mouseup || type == Type.mousedown)
277 return Name + ", " + AutomationID + ", " + ClassName + ", " + ActionType + ", " + Index + ", " + point.ToString();
278 else
279 return Name + ", " + AutomationID + ", " + ClassName + ", " + ActionType + ", " + Index + ", " + mouse.Delta;
280 }
281 }
282 /* It's a class that represents a recording of a series of actions that can be performed on a
283 UI element. */
284 public class Recording
285 {
286 private string name;
287 public string Name
288 {
289 get { return name; }
290 set { name = value; }
291 }
292 private string file;
293 public string File
294 {
295 get { return file; }
296 set { file = value; }
297 }
298 private List<Action> list = new List<Action>();
300 {
301 get { return list; }
302 set { list = value; }
303 }
304 public Recording()
305 {
306 }
307 public Recording(string fil,List<Action> l)
308 {
309 list = l;
310 file = fil;
311 Name = System.IO.Path.GetFileNameWithoutExtension(fil);
312 }
313 public void Run()
314 {
315 foreach (Action a in list)
316 {
317 a.Perform();
318 }
319 }
320 public object Get()
321 {
322 if(list.Count > 1)
323 for (int i = 0; i < list.Count; i++)
324 {
325 Action ac = list[i];
326 if (i == list.Count - 1)
327 continue;
328 ac.Perform();
329 }
330 Action la = list.Last();
331 AutomationElement ae = AutomationHelpers.GetElementByProcess(la.ProcessName, la.Title, la.AutomationID, la.Name, la.ClassName, la.Index);
332 if (la.Value == Action.ValueType.Name)
333 return ae.Name;
334 else if (la.Value == Action.ValueType.SelectionPattern)
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);
342 else
343 return AutomationHelpers.GetImage(ae);
344 }
346 {
347 Action la = list.Last();
348 AutomationElement ae = AutomationHelpers.GetElementByProcess(la.ProcessName, la.Title, la.AutomationID, la.Name, la.ClassName, la.Index);
349 return AutomationHelpers.GetImage(ae);
350 }
352 {
353 Action la = list.Last();
354 AutomationElement ae = AutomationHelpers.GetElementByProcess(la.ProcessName, la.Title, la.AutomationID, la.Name, la.ClassName, la.Index);
355 return ae.BoundingRectangle;
356 }
357 public void Set(string val)
358 {
359 for (int i = 0; i < list.Count; i++)
360 {
361 Action ac = list[i];
362 if (i == list.Count - 1)
363 continue;
364 ac.Perform();
365 }
366 Action la = list.Last();
367 AutomationElement ae = AutomationHelpers.GetElementByProcess(la.ProcessName, la.Title, la.AutomationID, la.Name, la.ClassName, la.Index);
369 }
370 public override string ToString()
371 {
372 return Name;
373 }
374 }
375
376 /* A getter method that returns the value of the recording variable. */
377 public static bool IsRecording
378 {
379 get { return recording; }
380 }
381
382 private IKeyboardMouseEvents m_GlobalHook;
383 private static bool recording = false;
384 private static bool move = false;
385 public static Recording rec = new Recording();
386 /* Creating a global hook for the mouse and keyboard. */
387 public Automation()
388 {
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();
398 }
399
400 //This represent the current element under the mouse.
401 /* Getting the element that the mouse is currently over. */
402 public static AutomationElement Element
403 {
404 get
405 {
406 try
407 {
408 return automation.FromPoint(new Point(Cursor.Position.X, Cursor.Position.Y));
409 }
410 catch (Exception)
411 {
412
413 }
414 return null;
415 }
416 }
418 public static void StartRecording()
419 {
420 recording = true;
421 rec = new Recording();
422 }
424 public static void StopRecording()
425 {
426 recording=false;
427 rec.Name = recname;
428 Recordings.Add(rec.Name,rec);
429 }
430 public static string recname;
432 public static void StartPropertyRecording(string name)
433 {
434 recording = true;
435 recname = name;
436 rec = new Recording();
437 rec.Name = recname;
438 }
440 public static void StopPropertyRecording()
441 {
442 recording = false;
443 Properties.Add(rec.Name,rec);
444 }
450 public static object GetProperty(string prop)
451 {
452 Recorder.AddLine("Automation.GetProperty(" + '"' + prop + '"' + ");");
453 return Properties[prop].Get();
454 }
459 public static void SetProperty(string prop, string val)
460 {
461 Recording rec = (Recording)Properties[prop];
462 rec.Set(val);
463 Recorder.AddLine("Automation.SetProperty(" + '"' + prop + '"' + "," + '"' + val + '"' + ");");
464 }
469 public static void AddProperty(Recording rec)
470 {
471 if (Properties.ContainsKey(rec.Name))
472 Properties[rec.Name] = rec;
473 else
474 Properties.Add(rec.Name, rec);
475 }
479 public static void RunRecording(string prop)
480 {
481 Recording rec = (Recording)Properties[prop];
482 rec.Run();
483 Recorder.AddLine("Automation.RunRecording(" + '"' + prop + '"' + ");");
484 }
493 private void GlobalHookMouseDownExt(object sender, MouseEventExtArgs e)
494 {
495 if (!recording)
496 return;
497 AutomationElement el = Element;
498 if (el == null)
499 return;
500 rec.List.Add(new Action(Action.Type.mousedown, el.Properties.ProcessId, e));
501 }
510 private void GlobalHookMouseUpExt(object sender, MouseEventExtArgs e)
511 {
512 if (!recording)
513 return;
514 AutomationElement el = Element;
515 if (el == null)
516 return;
517 rec.List.Add(new Action(Action.Type.mouseup, el.Properties.ProcessId, e));
518 }
527 private void GlobalHookMouseWheel(object sender, MouseEventArgs e)
528 {
529 if (!recording)
530 return;
531 AutomationElement el = Element;
532 if (el == null)
533 return;
534 if (e.Delta > 0)
535 rec.List.Add(new Action(Action.Type.wheelup, el.Properties.ProcessId, e));
536 else
537 rec.List.Add(new Action(Action.Type.wheeldown, el.Properties.ProcessId, e));
538 }
547 private void GlobalHookMouseMoveExt(object sender, MouseEventExtArgs e)
548 {
549 if (!move)
550 return;
551 if (!recording)
552 return;
553 AutomationElement el = Element;
554 if (el == null)
555 return;
556 rec.List.Add(new Action(Action.Type.mousemove, el.Properties.ProcessId, e));
557
558 }
567 private void GlobalHookKeyPress(object sender, KeyPressEventArgs e)
568 {
569 if (!recording)
570 return;
571 AutomationElement el = Element;
572 if (el == null)
573 return;
574 rec.List.Add(new Action(Action.Type.keypress, el.Properties.ProcessId, e));
575 }
584 private void GlobalHookKeyUp(object sender, KeyEventArgs e)
585 {
586 if (!recording)
587 return;
588 AutomationElement el = Element;
589 if (el == null)
590 return;
591 rec.List.Add(new Action(Action.Type.keyup, el.Properties.ProcessId, e));
592 }
601 private void GlobalHookKeyDown(object sender, KeyEventArgs e)
602 {
603 if (!recording)
604 return;
605 AutomationElement el = Element;
606 if (el == null)
607 return;
608 rec.List.Add(new Action(Action.Type.keydown, el.Properties.ProcessId, e));
609 }
610
611 /* It's a class that helps you automate windows applications */
612 public static class AutomationHelpers
613 {
614 public static List<AutomationElement> GetChildren(AutomationElement parent)
615 {
616 if (parent == null)
617 {
618 // null parameter
619 throw new ArgumentException();
620 }
621 AutomationElement[] collection = automation.GetDesktop().FindAll(TreeScope.Children, FlaUI.Core.Conditions.TrueCondition.Default);
622 if (collection != null)
623 {
624 List<AutomationElement> result = new List<AutomationElement>(collection.Cast<AutomationElement>());
625 return result;
626 }
627 else
628 {
629 // some error occured
630 return null;
631 }
632 }
633 public static List<AutomationElement> GetAllChildren(AutomationElement parent)
634 {
635 if (parent == null)
636 {
637 // null parameter
638 throw new ArgumentException();
639 }
640 try
641 {
642 AutomationElement[] collection = automation.GetDesktop().FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
643 if (collection != null)
644 {
645 List<AutomationElement> result = new List<AutomationElement>(collection.Cast<AutomationElement>());
646 return result;
647 }
648 else
649 {
650 // some error occured
651 return null;
652 }
653 }
654 catch (Exception)
655 {
656 return null;
657 }
658 }
659 public static List<AutomationElement> GetName(AutomationElement parent, string name)
660 {
661 if (parent == null)
662 {
663 // null parameter
664 throw new ArgumentException();
665 }
666 AutomationElement[] collection = automation.GetDesktop().FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
667 if (collection != null)
668 {
669 List<AutomationElement> result = new List<AutomationElement>();
670 foreach (AutomationElement item in collection)
671 {
672 if (item.Name == name)
673 {
674 result.Add(item);
675 }
676 }
677 return result;
678 }
679 else
680 {
681 // some error occured
682 return null;
683 }
684 }
685 public static List<AutomationElement> GetAutomationID(AutomationElement parent, string id)
686 {
687 if (parent == null)
688 {
689 // null parameter
690 throw new ArgumentException();
691 }
692 AutomationElement[] collection = automation.GetDesktop().FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
693 if (collection != null)
694 {
695 List<AutomationElement> result = new List<AutomationElement>();
696 foreach (AutomationElement item in collection)
697 {
698 if (item.AutomationId == id)
699 {
700 result.Add(item);
701 }
702 }
703 return result;
704 }
705 else
706 {
707 // some error occured
708 return null;
709 }
710 }
711 public static List<AutomationElement> GetClassName(AutomationElement parent, string name)
712 {
713 if (parent == null)
714 {
715 // null parameter
716 throw new ArgumentException();
717 }
718 AutomationElement[] collection = automation.GetDesktop().FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
719 if (collection != null)
720 {
721 List<AutomationElement> result = new List<AutomationElement>();
722 foreach (AutomationElement item in collection)
723 {
724 if (item.ClassName == name)
725 {
726 result.Add(item);
727 }
728 }
729 return result;
730 }
731 else
732 {
733 // some error occured
734 return null;
735 }
736 }
737 public static AutomationElement GetElementByProcess(string process, string title, string id, string name, string classname, int index)
738 {
739 Process[] procs = Process.GetProcessesByName(process);
740 string ti = title;
741 if (ti == "")
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)
748 {
749 try
750 {
751 if (item.Name.Contains(ti))
752 {
753 window = item;
754 }
755 }
756 catch (Exception)
757 {
758
759 }
760
761 }
762 if (window == null)
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)
767 {
768 try
769 {
770 if (item.Properties.LocalizedControlType == classname && item.Name == name)
771 {
772 result.Add(item);
773 }
774 }
775 catch (Exception)
776 {
777
778 }
779
780 }
781 if (index == -1)
782 return result[0];
783 return result[index];
784 }
785 public static int GetIndex(string process, string title, string id, string name, string classname, Point p)
786 {
787 //For elements which have the same name, tittle, & id we also find the index of the item.
788 Process[] procs = Process.GetProcessesByName(process);
789 string ti = title;
790 if (ti == "")
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)
796 {
797 try
798 {
799 if (item.Name.Contains(title))
800 {
801 window = item;
802 }
803 }
804 catch (Exception)
805 {
806
807 }
808
809 }
810 if (window == null)
811 return 0;
812 AutomationElement[] items = null;
813 try
814 {
815 items = window.FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
816 }
817 catch (Exception)
818 {
819
820 }
821 List<AutomationElement> result = new List<AutomationElement>();
822
823 int index = 0;
824 /*
825 if (items != null)
826 foreach (AutomationElement item in items)
827 {
828 try
829 {
830 int[] r = item.Properties.RuntimeId;
831 if (item.ClassName == classname && item.AutomationId == id && item.Name == name)
832 {
833 result.Add(item);
834 }
835 if (item.BoundingRectangle.IntersectsWith(new Rectangle(p.X, p.Y, 1, 1)) && result.Count > 0)
836 {
837 index = result.Count - 1;
838 }
839 }
840 catch (Exception)
841 {
842
843 }
844 }
845 */
846 return index;
847 }
848 public static int[] StringToRuntimeID(string s)
849 {
850 int[] r = new int[3];
851 string[] sts = s.Split(',');
852 for (int i = 0; i < 3; i++)
853 {
854 r[i] = int.Parse(sts[i]);
855 }
856 return r;
857 }
858 public static List<AutomationElement> GetElement(string id, string name)
859 {
860 AutomationElement parent = automation.GetDesktop();
861 AutomationElement[] collection = automation.GetDesktop().FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
862 if (collection != null)
863 {
864 List<AutomationElement> result = new List<AutomationElement>();
865 foreach (AutomationElement item in collection)
866 {
867 if (item.AutomationId == id && item.Name == name)
868 {
869 result.Add(item);
870 }
871 }
872 return result;
873 }
874 else
875 {
876 // some error occured
877 return null;
878 }
879 }
880 public static List<AutomationElement> GetLocalizedControlType(AutomationElement parent, TreeScope scope, string type)
881 {
882 if (parent == null)
883 {
884 // null parameter
885 throw new ArgumentException();
886 }
887 AutomationElement[] collection = automation.GetDesktop().FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
888 if (collection != null)
889 {
890 List<AutomationElement> result = new List<AutomationElement>();
891 foreach (AutomationElement item in collection)
892 {
893 if (item.Properties.LocalizedControlType == type)
894 {
895 result.Add(item);
896 }
897 }
898 return result;
899 }
900 else
901 {
902 // some error occured
903 return null;
904 }
905 }
906 public static AutomationElement[] GetRawChildren(AutomationElement parent)
907 {
908 if (parent == null)
909 {
910 // null parameter
911 throw new ArgumentException();
912 }
913 return parent.FindAll(TreeScope.Children, FlaUI.Core.Conditions.TrueCondition.Default);
914 }
915 public static List<AutomationElement> GetRawAutomationID(AutomationElement parent, string auto)
916 {
917 if (parent == null)
918 {
919 // null parameter
920 throw new ArgumentException();
921 }
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)
925 {
926 if (item.AutomationId == auto)
927 {
928 ress.Add(item);
929 }
930 }
931 return ress;
932 }
933 public static List<AutomationElement> GetRawLocalizedControlType(AutomationElement parent, string auto)
934 {
935 if (parent == null)
936 {
937 // null parameter
938 throw new ArgumentException();
939 }
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)
943 {
944 if (item.Properties.LocalizedControlType == auto)
945 {
946 ress.Add(item);
947 }
948 }
949 return ress;
950 }
951 public static AutomationElement[] GetRaw(AutomationElement parent)
952 {
953 if (parent == null)
954 {
955 // null parameter
956 throw new ArgumentException();
957 }
958 List<AutomationElement> ress = new List<AutomationElement>();
959 return parent.FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
960 }
961 public static string GetValue(AutomationElement control)
962 {
963 if(control.ControlType == ControlType.Edit)
964 {
965 FlaUI.Core.AutomationElements.TextBox tb = control.AsTextBox();
966 return tb.Text;
967 }
968 else if(control.ControlType == ControlType.Text)
969 {
970 return control.AsLabel().Text;
971 }
972 return control.Name;
973 }
974 public static string GetSelection(AutomationElement control)
975 {
976 object patternProvider;
977 FlaUI.Core.AutomationElements.ListBox lb = control.AsListBox();
978 return lb.SelectedItem.Name;
979 }
980 public static string GetText(AutomationElement control)
981 {
982 return control.AsLabel().Text;
983 }
984 public static void SetValue(AutomationElement targetControl, string value)
985 {
986 // Validate arguments / initial setup
987 if (value == null)
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");
993 // A series of basic checks prior to attempting an insertion.
994 //
995 // Check #1: Is control enabled?
996 // An alternative to testing for static or read-only controls
997 // is to filter using
998 // PropertyCondition(AutomationElement.IsEnabledProperty, true)
999 // and exclude all read-only text controls from the collection.
1000 if (!targetControl.IsEnabled)
1001 {
1002 throw new InvalidOperationException(
1003 "The control is not enabled.\n\n");
1004 }
1005 targetControl.AsTextBox().Text = value;
1006 }
1007 public static int GetCount(AutomationElement parent)
1008 {
1009 if (parent == null)
1010 {
1011 // null parameter
1012 throw new ArgumentException();
1013 }
1014 int i = 0;
1015 return parent.FindAll(TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default).Length;
1016 }
1017 public static bool GetToggle(AutomationElement element)
1018 {
1019 if (element == null)
1020 {
1021 return false;
1022 }
1023 ToggleButton tb = element.AsToggleButton();
1024 if (tb.TogglePattern.ToggleState.ValueOrDefault.HasFlag(ToggleState.On))
1025 return true;
1026 else
1027 return false;
1028 }
1029 public static Bitmap GetImage(AutomationElement el)
1030 {
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));
1036 return b;
1037 }
1038
1039 }
1040
1041 }
1042
1043}
AForge.Bitmap Bitmap
AForge.Point Point
AForge.Rectangle Rectangle
System.Drawing.Size Size
System.Windows.Forms.Keys Keys
Definition ImageView.cs:17
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)
System.Drawing.Point Point
Definition Automation.cs:79
override string ToString()
Action(Type t, int proc, KeyPressEventArgs e)
Action(Type t, string process, string title, int index, string id, string name, string classname, KeyPressEventArgs ke)
Action(Type t, int proc, MouseEventArgs e)
Action(Type t, string process, string title, int index, string id, string name, string classname, KeyEventArgs ke)
Action(Type t, int proc, KeyEventArgs e)
static bool GetToggle(AutomationElement element)
static void SetValue(AutomationElement targetControl, string value)
static List< AutomationElement > GetRawAutomationID(AutomationElement parent, string auto)
static List< AutomationElement > GetAllChildren(AutomationElement parent)
static int GetIndex(string process, string title, string id, string name, string classname, Point p)
static AutomationElement GetElementByProcess(string process, string title, string id, string name, string classname, int index)
static string GetText(AutomationElement control)
static List< AutomationElement > GetName(AutomationElement parent, string name)
static int GetCount(AutomationElement parent)
static List< AutomationElement > GetChildren(AutomationElement parent)
static List< AutomationElement > GetClassName(AutomationElement parent, string name)
static AutomationElement[] GetRaw(AutomationElement parent)
static string GetSelection(AutomationElement control)
static Bitmap GetImage(AutomationElement el)
static List< AutomationElement > GetElement(string id, string name)
static List< AutomationElement > GetAutomationID(AutomationElement parent, string id)
static string GetValue(AutomationElement control)
static List< AutomationElement > GetLocalizedControlType(AutomationElement parent, TreeScope scope, string type)
static AutomationElement[] GetRawChildren(AutomationElement parent)
static int[] StringToRuntimeID(string s)
static List< AutomationElement > GetRawLocalizedControlType(AutomationElement parent, string auto)
Recording(string fil, List< Action > l)
static string recname
static UIA3Automation automation
Definition Automation.cs:16
static Recording rec
static void SetProperty(string prop, string val)
static void StartPropertyRecording(string name)
static void AddProperty(Recording rec)
static void StartRecording()
It starts recording.
static void RunRecording(string prop)
static void StopRecording()
It stops the recording and adds it to the list of recordings.
static AutomationElement Element
static object GetProperty(string prop)
static InputSimulator input
Definition Automation.cs:13
static bool IsRecording
static void StopPropertyRecording()
Stop recording the property and add it to the list of properties.
static void RightClick()
Definition Win32.cs:135
static void LeftClick()
Definition Win32.cs:131
static void AddLine(string s)
Definition Recorder.cs:16
static void MouseWheelDown()
MouseWheelDown() simulates a mouse wheel down event.
Definition Win32.cs:73
static void MouseWheelUp()
"MouseWheelUp()" is a function that simulates a mouse wheel up event.
Definition Win32.cs:68
static string GetActiveWindowTitle()
Definition Win32.cs:90
static void SetFocus(string process)
Definition Win32.cs:81