/Commands/Get/GetUIAWindowsCommand.cs

http://uiautomation.codeplex.com · C# · 224 lines · 183 code · 4 blank · 37 comment · 16 complexity · fdd39b9eb4782275eaa421ec1e6b3746 MD5 · raw file

  1. /*
  2. * Created by SharpDevelop.
  3. * User: Alexander Petrovskiy
  4. * Date: 29.11.2011
  5. * Time: 4:00
  6. *
  7. * To change this template use Tools | Options | Coding | Edit Standard Headers.
  8. */
  9. using System;
  10. using System.Management.Automation;
  11. using System.Windows.Automation;
  12. namespace UIAutomation.Commands
  13. {
  14. /// <summary>
  15. /// Description of GetUIAWindows.
  16. /// </summary>
  17. [Cmdlet(VerbsCommon.Get, "UIAWindows")]
  18. [OutputType(new[]{ typeof(object) })]
  19. // disabled in the current release
  20. internal class GetUIAWindowsCommand : GetWindowCmdletBase
  21. {
  22. #region Constructor
  23. public GetUIAWindowsCommand()
  24. {
  25. ProcessName = String.Empty;
  26. Name = String.Empty;
  27. }
  28. #endregion Constructor
  29. #region Parameters
  30. const string ParamSetProcessName = "ProcessNameSet";
  31. const string ParamSetName = "TitleSet";
  32. #endregion Parameters
  33. protected override void BeginProcessing()
  34. {
  35. startDate = System.DateTime.Now;
  36. try{
  37. // if (this.ProcessName=="" && this.Title=="")
  38. if (this.ProcessName=="" && this.Name=="")
  39. {
  40. WriteVerbose(this, "ProcessName==null && Title==null");
  41. WriteObject(this, null);
  42. return;
  43. } // describe
  44. WriteVerbose(this, "timeout countdown started for process: " +
  45. this.ProcessName + ", title: " + this.Name);
  46. // this.ProcessName + ", title: " + this.Title);
  47. } catch (Exception eCheckParameters)
  48. {
  49. WriteDebug(this, eCheckParameters.Message);
  50. WriteObject(this, null);
  51. return;
  52. } // describe
  53. System.Windows.Automation.AutomationElement[]aeForm = null;
  54. WriteVerbose(this, "getting the root element");
  55. System.Windows.Automation.AutomationElement rootElement =
  56. System.Windows.Automation.AutomationElement.RootElement;
  57. if (rootElement==null)
  58. {
  59. WriteVerbose(this, "rootElement==null");
  60. WriteObject(this, null);
  61. return;
  62. }
  63. else
  64. {
  65. WriteVerbose(this, rootElement.Current);
  66. }
  67. //#}
  68. //#Process{
  69. System.Collections.Generic.List<System.Windows.Automation.AutomationElement> aeWndArray = null;
  70. WriteVerbose(this, "getting process Id");
  71. int processId = 0;
  72. do {
  73. System.Windows.Automation.AndCondition conditions = null;
  74. System.Windows.Automation.AutomationElementCollection aeForms = null;
  75. if (this.ProcessName.Length>0){
  76. WriteVerbose(this, "processName.Length>0");
  77. try{
  78. System.Diagnostics.Process[] processes =
  79. System.Diagnostics.Process.GetProcessesByName(this.ProcessName);
  80. // only the first
  81. processId = processes[0].Id;
  82. conditions =
  83. new System.Windows.Automation.AndCondition(
  84. new System.Windows.Automation.PropertyCondition(
  85. System.Windows.Automation.AutomationElement.ProcessIdProperty,
  86. processId),
  87. new System.Windows.Automation.PropertyCondition(
  88. System.Windows.Automation.AutomationElement.ControlTypeProperty,
  89. System.Windows.Automation.ControlType.Window));
  90. } catch{
  91. conditions =
  92. new System.Windows.Automation.AndCondition(
  93. new System.Windows.Automation.PropertyCondition(
  94. System.Windows.Automation.AutomationElement.ProcessIdProperty,
  95. processId),
  96. new System.Windows.Automation.PropertyCondition(
  97. System.Windows.Automation.AutomationElement.ControlTypeProperty,
  98. System.Windows.Automation.ControlType.Window));
  99. }
  100. aeForms =
  101. rootElement.FindAll(System.Windows.Automation.TreeScope.Children,
  102. conditions);
  103. }
  104. else
  105. {
  106. WriteVerbose(this, "processName.Length==0");
  107. System.Windows.Automation.PropertyCondition condition =
  108. new System.Windows.Automation.PropertyCondition(
  109. System.Windows.Automation.AutomationElement.ControlTypeProperty,
  110. System.Windows.Automation.ControlType.Window);
  111. aeForms =
  112. rootElement.FindAll(System.Windows.Automation.TreeScope.Children,
  113. condition);
  114. }
  115. // System.Windows.Automation.AutomationElementCollection aeForms =
  116. // rootElement.FindAll(System.Windows.Automation.TreeScope.Children,
  117. // conditions);
  118. // System.Collections.ArrayList aeWndArray =
  119. // new System.Collections.ArrayList();
  120. aeWndArray =
  121. new System.Collections.Generic.List<System.Windows.Automation.AutomationElement>();
  122. if (aeForms!=null && aeForms.Count>0)
  123. {
  124. WriteVerbose(this, "aeForms!=null && aeForms.Count>0");
  125. foreach (System.Windows.Automation.AutomationElement form in aeForms)
  126. {
  127. // if (this.Title.Length>0){
  128. if (this.Name.Length>0){
  129. if (System.Text.RegularExpressions.Regex.IsMatch(
  130. form.Current.Name.ToUpper(), this.Name.ToUpper()))
  131. // form.Current.Name.ToUpper(), this.Title.ToUpper()))
  132. {
  133. WriteVerbose(this, "app name: " +
  134. form.Current.Name);
  135. aeWndArray.Add(form);
  136. }
  137. }
  138. else{
  139. WriteVerbose(this, "app name: " + form.Current.Name);
  140. aeWndArray.Add(form);
  141. }
  142. }
  143. }
  144. // System.Threading.Thread.Sleep(Preferences.SleepInterval);
  145. // RunScriptBlocks(this);
  146. // SleepAndRunScriptBlocks(this);
  147. System.DateTime nowDate = System.DateTime.Now;
  148. WriteVerbose(this, "process: " +
  149. this.ProcessName +
  150. ", title: " +
  151. this.Name +
  152. // this.Title +
  153. ", seconds: " + (nowDate - startDate).TotalSeconds);
  154. if (aeWndArray.Count>0 || (nowDate - startDate).TotalSeconds>this.Timeout/1000)
  155. {
  156. if (aeWndArray.Count==0){
  157. WriteVerbose(this, "timeout expired for process: " +
  158. this.ProcessName + ", title: " + this.Name);
  159. // this.ProcessName + ", title: " + this.Title);
  160. }else{
  161. WriteVerbose(this, "got the window: " +
  162. ((System.Windows.Automation.AutomationElement)aeWndArray[0]).Current.Name);
  163. }
  164. WriteVerbose(this, "no wait");
  165. this.Wait = false;
  166. WriteVerbose(this, "break");
  167. break;
  168. }
  169. WriteVerbose(this, "while");
  170. } while (this.Wait);
  171. if (aeWndArray!=null && aeWndArray.Count>0)
  172. {
  173. WriteVerbose(this, "aeWndArray.count>0");
  174. WriteVerbose(this, aeWndArray.Count.ToString());
  175. WriteVerbose(this, aeWndArray[0].ToString());
  176. WriteVerbose(this, aeWndArray[0].Current.ToString());
  177. WriteVerbose(this, aeWndArray[0].Current.Name);
  178. //#Paint-Rect $aeWndArray[0].Current.NativeWindowHandle `
  179. //# $aeWndArray[0].Current.BoundingRectangle;
  180. try{
  181. WriteVerbose(this, "before foreach");
  182. foreach(System.Windows.Automation.AutomationElement elt in aeWndArray)
  183. {
  184. WriteVerbose(this, "foreach");
  185. if (elt==null)
  186. {
  187. WriteVerbose(this, "clean-up");
  188. WriteVerbose(this, "removing element: " + elt.Current.Name);
  189. aeWndArray.Remove(elt);
  190. }
  191. }
  192. WriteVerbose(this, "after foreach");
  193. System.Windows.Automation.AutomationElement[] outArray =
  194. (System.Windows.Automation.AutomationElement[])aeWndArray.ToArray();
  195. WriteVerbose(this, outArray.Length.ToString());
  196. WriteVerbose(this, outArray[0].Current.Name);
  197. WriteVerbose(this, "before WriteObject");
  198. // WriteObject((object[])outArray);
  199. // WriteObject(outArray);
  200. // WriteObject(aeWndArray);
  201. WriteObject(this, (object[])aeWndArray.ToArray());
  202. return;
  203. // WriteObject(this, aeWndArray);
  204. } catch (Exception outException){
  205. WriteDebug(this, "Could not cast the output array to System.Object[]");
  206. WriteDebug(this, outException.Message);
  207. }
  208. }
  209. if (aeForm!=null)
  210. {
  211. WriteVerbose(this, aeForm);
  212. WriteVerbose(this, "aeForm.Current.GetType() = aeForm.Current.GetType()");
  213. }
  214. WriteVerbose(this, "aeForm==null");
  215. //#Paint-Rect $aeForm.Current.NativeWindowHandle `
  216. //# $aeForm.Current.BoundingRectangle;
  217. WriteObject(this, aeForm);
  218. }
  219. }
  220. }