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