With UI Automation APIerne search the web you can grab and interact with the UI elements on the desktop and in other applications. UI element (control) to support UI Automation, which the vast majority do. UI automation has, as I see there, two main applications Accessibility and UI Interoperability.
A small example would be that I like to teach my children mathematics, knowing that they probably will want more, so I will not even make a calculator, but just recycle the Calculator (calc.exe) that comes with Windows. Think yourself a fun UI in with apples, bananas and pears;-).
Anyways, I need to get hold of the Calculator program as well grab the buttons I want to use and the result field (UI elements). I do this below: Process calcInstance = Process .Start ("calc.exe"); // Window search the web shopping is initialized asynchronously, so easy Wait Until we having a shop while (calcInstance.MainWindowHandle == IntPtr .Zero) {System.Threading. Thread .SpinWait (1000000); search the web } Automation element calculator = Automation element .FromHandle (calcInstance.MainWindowHandle); // Get all buttons Automation Element Collection buttons = calculator.FindAll (TreeScope .Descendants, search the web New Property Condition (Automation Element .ControlTypeProperty, Control Type .button)); search the web Automation Element btnOne = null; Automation Element btnPlus = null; Automation Element btnEquals = null; Automation Element btnClear = null; Automation Element btnFour = null; Automation Element btnMaximize = null; Automation Element resultPane = null; // Get reference two buttons - Use "UI Spy" to get names and Automation IDs from applications foreach (Automation Element button in buttons) {switch (button.Current.Name) {case "1": btnOne = button; break; case "Maximize": btnMaximize = button; break; case "4": btnFour = button; break; case "Add": btnPlus = button; break; case "Clear": btnClear = button; break; case "Equals": btnEquals = button; break; default: break; }} Try {// Invoke buttons InvokeControl (btnClear); InvokeControl (btnOne); InvokeControl (btnOne); search the web InvokeControl (btnOne); InvokeControl (btnPlus); InvokeControl (btnFour); InvokeControl (btnEquals); } Catch (Exception ex) {Console .WriteLine ("E:" search the web + ex.Message); } // Find all text controls Automation Element Collection txt = calculator.FindAll (TreeScope .Descendants, New Property Condition (Automation Element search the web .ControlTypeProperty, Control Type .Text)); foreach search the web (Automation Element t in txt) {// the automation ID of the result pane is 150 if (t.Current.AutomationId.Equals ("150")) resultPane = t; } Console .WriteLine ("=" + resultPane.Current.Name); Console .ReadKey (); }
On MSDN, I found a little code to get executed command behind my Automation search the web Element. What is interesting in the following is to be used invokepattern. There are a variety of patterns search the web you can use, depending on what you want to do with Automation element. static void InvokeControl (Automation Element target control) {InvokePattern invokePattern = null; try {invokePattern search the web = targetControl.GetCurrentPattern (InvokePattern search the web .Pattern) as InvokePattern; } Catch (ElementNotEnabledException) {return; } Catch (InvalidOperationException) {return; } InvokePattern.Invoke (); }
To identify search the web the individual UI elements, you can use "UI Spy" which comes with Visual Studio. Below I started UI Spy and Calculator. By holding search the web down the Control key as focused UI Spy on the UI element that is under the mouse cursor. When in focus, so I can see the properties of the UI element.
No comments:
Post a Comment