public void WaitForClosed(TimeSpan timeout, Action closeCommand = null) { using (var closed = new AutoResetEvent(false)) { AutomationEventHandler handler = (s, e) => { closed.Set(); }; Automation.AddAutomationEventHandler( WindowPattern.WindowClosedEvent, Element, TreeScope.Element, handler ); if (closeCommand != null) { closeCommand(); } closed.WaitOne(timeout); Automation.RemoveAutomationEventHandler( WindowPattern.WindowClosedEvent, Element, handler ); } }
internal static void Remove( AutomationEvent eventId, AutomationElement element, AutomationEventHandler handlingDelegate) { Remove(predicate: item => item._eventId == eventId.Id && 1 == Automation.AutomationClass.CompareElements(el1: item._uiAutomationElement, el2: element.IUIAutomationElement) && item._handlingDelegate == handlingDelegate); }
public static void AddAutomationEventHandler( AutomationEvent eventId, AutomationElement element, TreeScope scope, AutomationEventHandler eventHandler) { AutomationEventHandlerImpl.Add(eventId: eventId, element: element, scope: scope, handlingDelegate: eventHandler); }
public static void AddAutomationEventHandler (AutomationEvent eventId, IRawElementProviderSimple provider, TreeScope scope, AutomationEventHandler eventHandler) { var entry = new AutomationEventEntry (eventId, provider, scope, eventHandler); lock (automationEventEntries) automationEventEntries.Add (entry); }
public Handler(AutomationEvent automationEvent, AutomationElement automationElement, AutomationEventHandler automationEventHandler) { _automationEvent = automationEvent; _automationElement = automationElement; _handler = automationEventHandler; Automation.AddAutomationEventHandler(_automationEvent, _automationElement, TreeScope.Element, _handler); }
public static void RemoveAutomationEventHandler (AutomationEvent eventId, IRawElementProviderSimple provider, AutomationEventHandler eventHandler) { lock (automationEventEntries) automationEventEntries.RemoveAll (e => e.EventId == eventId && e.Provider == provider && e.Handler == eventHandler); }
private void StartAppWatcher(string elementName, FindWindowMethod method) { windowElement = GetAppElement(elementName, method); // (...) // You may want to to something if the watched application is already running when you start your app Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Subtree, (elm, e) => { AutomationElement element = elm as AutomationElement; try { if (element == null || element.Current.ProcessId == currentProcessId) { return; } if (windowElement == null) { windowElement = GetAppElement(elementName, method); } if (windowElement == null || windowElement.ProcessId != element.Current.ProcessId) { return; } // If the Window is a MessageBox generated by the watched app, attach the handler if (element.Current.ClassName == "#32770") { buttonElement = element; var msgBoxButton = element.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "OK")); if (msgBoxButton != null && msgBoxButton.GetSupportedPatterns().Any(p => p.Equals(InvokePattern.Pattern))) { Automation.AddAutomationEventHandler( InvokePattern.InvokedEvent, msgBoxButton, TreeScope.Element, DialogButtonHandler = new AutomationEventHandler(MessageBoxButtonHandler)); } } } catch (ElementNotAvailableException) { // Ignore: this exception may be raised if you show a modal dialog, // in your own app, that blocks the execution. When the dialog is closed, // AutomationElement element is no longer available } }); Automation.AddAutomationEventHandler(WindowPattern.WindowClosedEvent, AutomationElement.RootElement, TreeScope.Subtree, (elm, e) => { AutomationElement element = elm as AutomationElement; if (element == null || element.Current.ProcessId == currentProcessId || windowElement == null) { return; } if (windowElement.ProcessId == element.Current.ProcessId) { if (windowElement.MainWindowTitle == element.Current.Name) { windowElement = null; } } }); }
public static void AddAutomationEventHandler(AutomationEvent eventId, IRawElementProviderSimple provider, TreeScope scope, AutomationEventHandler eventHandler) { var entry = new AutomationEventEntry(eventId, provider, scope, eventHandler); lock (automationEventEntries) automationEventEntries.Add(entry); }
public AutomationEventEntry(AutomationEvent eventId, IRawElementProviderSimple provider, TreeScope scope, AutomationEventHandler handler) : base(provider, scope) { this.EventId = eventId; this.Handler = handler; }
/// -------------------------------------------------------------------- /// <summary> /// Register the target closed event listener. /// </summary> /// -------------------------------------------------------------------- private void RegisterTargetClosedListener() { _targetCloseListener = OnTargetClosed; Automation.AddAutomationEventHandler( WindowPattern.WindowClosedEvent, _targetApp, TreeScope.Element, _targetCloseListener); }
///-------------------------------------------------------------------- /// <summary> /// Register the target closed event listener. /// </summary> ///-------------------------------------------------------------------- private void RegisterTargetCloseEventListener() { targetCloseListener = new AutomationEventHandler(OnTargetClosed); Automation.AddAutomationEventHandler( WindowPattern.WindowClosedEvent, targetApp, TreeScope.Element, targetCloseListener); }
static void Main(string[] args) { CalcAutomationClient autoClient = new CalcAutomationClient(); AutomationEventHandler eventHandler = new AutomationEventHandler(autoClient.OnWindowOpenOrClose); Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Children, eventHandler); Process.Start("calc.exe"); Console.ReadLine(); }
private void RegisterForEvents() { // Subscribe to events of interest. // Do this while a CacheRequest is activated, so that the AutomationElement passed // to the event handlers will have cached properties. CacheRequest cacheRequest = new CacheRequest(); cacheRequest.Add(AutomationElement.NameProperty); cacheRequest.Add(AutomationElement.ControlTypeProperty); cacheRequest.TreeScope = TreeScope.Element; using (cacheRequest.Activate()) { // Focus changes are global; we'll get cached properties for all elements that receive focus. AutomationFocusChangedEventHandler focusHandler = new AutomationFocusChangedEventHandler(OnFocusChange); Automation.AddAutomationFocusChangedEventHandler(focusHandler); //foreach (AutomationElement element in controlStore) //{ // AutomationPattern[] automationPatterns = element.GetSupportedPatterns(); // foreach (AutomationPattern automationPattern in automationPatterns) // { // switch (automationPattern.ProgrammaticName) // { // case "InvokePatternIdentifiers.Pattern": // AutomationEventHandler invokeHandler = // new AutomationEventHandler(OnInvoke); // Automation.AddAutomationEventHandler(InvokePattern.InvokedEvent, element, TreeScope.Element, invokeHandler); // break; // } // } //} //AutomationEventHandler invokeHandler = new AutomationEventHandler(OnInvoke); //Automation.AddAutomationEventHandler(InvokePattern.InvokedEvent, // targetApp, TreeScope.Descendants, invokeHandler); foreach (AutomationElement element in controlStore) { if ((bool)element.GetCurrentPropertyValue(AutomationElement.IsInvokePatternAvailableProperty)) { AutomationEventHandler invokeHandler = new AutomationEventHandler(OnInvoke); Automation.AddAutomationEventHandler(InvokePattern.InvokedEvent, element, TreeScope.Element, invokeHandler); } if ((bool)element.GetCurrentPropertyValue(AutomationElement.IsRangeValuePatternAvailableProperty)) { //AutomationPropertyChangedEventHandler rangevalueHandler = // new AutomationPropertyChangedEventHandler(OnRangeValueChange); //Automation.AddAutomationPropertyChangedEventHandler( // element, TreeScope.Element, rangevalueHandler, RangeValuePattern. //Automation.AddAutomationEventHandler(InvokePattern.InvokedEvent, // element, TreeScope.Element, invokeHandler)); } } } }
public override void Start(IEventSink sink) { Validate.ArgumentNotNull(parameter: sink, parameterName: nameof(sink)); Stop(); Log.Out(msg: "{0} Start", (object)ToString()); this._sinkReference = new WeakReference(target: sink); this._handlingDelegate = Handler; Automation.AddAutomationEventHandler(eventId: this._eventId, element: this._root.AutomationElement, scope: (TreeScope)this._scope, eventHandler: this._handlingDelegate); }
public void StructureEventTest() //this test also tested WindowPattern.WindowOpenedEvent (i.e. AddAutomationEventHandler) { int automationEventCount = 0; int structureEventCount = 0; AutomationEvent eventId = null; AutomationEventHandler automationEventHandler = (o, e) => { automationEventCount++; eventId = e.EventId; }; At.AddAutomationEventHandler( WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Children, automationEventHandler); StructureChangedEventHandler structureEventHandler = (o, e) => { if (e.StructureChangeType == StructureChangeType.ChildAdded) { structureEventCount++; } }; At.AddStructureChangedEventHandler( AutomationElement.RootElement, TreeScope.Children, structureEventHandler); int pid = OpenForm(); Thread.Sleep(3000); Assert.AreEqual(1, structureEventCount, "[OpenForm] count of StructureChangedEvent"); Assert.AreEqual(1, automationEventCount, "[OpenForm] count of WindowOpenedEvent"); Assert.AreEqual(WindowPattern.WindowOpenedEvent, eventId); automationEventCount = 0; structureEventCount = 0; At.RemoveAllEventHandlers(); int pid2 = OpenForm(); Thread.Sleep(3000); Assert.AreEqual(0, structureEventCount); Assert.AreEqual(0, automationEventCount); structureEventHandler = (o, e) => { structureEventCount++; }; At.AddStructureChangedEventHandler( AutomationElement.RootElement, TreeScope.Children, structureEventHandler); CloseForm(pid); CloseForm(pid2); Thread.Sleep(3000); // Note: I expect 2 events here (whose StructureChangeType are both ChildRemoved) // But as tested on Win 7, we'll actually get no event, // And with our current implementation, we'll get 4 events (i.e. besides the 2 expected events, we // get other 2 ChildRemoved events, whose sender is the "testFormElement") Assert.AreEqual(0, structureEventCount, "[CloseForm] count of StructureChangedEvent"); }
AutomationEventHandlerImpl( int eventId, IUIAutomationElement uiAutomationElement, AutomationEventHandler handlingDelegate) { this._eventId = eventId; this._uiAutomationElement = uiAutomationElement; this._handlingDelegate = handlingDelegate; }
public static void HeadlessSetup(int processId) { SetupWhitelist(); handler = GetOpenWindowHandler(processId); Automation.AddAutomationEventHandler( WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Descendants, handler); }
public void AddAutomationEventHandler(AutomationEvent eventId, IElement element, TreeScope scope, AutomationEventHandler eventHandler) { AutomationEventHandlerData data = new AutomationEventHandlerData( eventId, element, scope, eventHandler); automationEventHandlers.Add(data); }
internal AutomationEventHandlerData(AutomationEvent eventId, IElement element, TreeScope scope, AutomationEventHandler eventHandler) { this.EventId = eventId; this.Element = element; this.Scope = scope; this.EventHandler = eventHandler; }
private void AddSelectionEvenhandler(AutomationElement element) { TreeScope scope = TreeScope.Descendants; AutomationEventHandler handler = SelectionChanged; Automation.AddAutomationEventHandler(SelectionItemPatternIdentifiers.ElementSelectedEvent, element, scope, handler); Automation.AddAutomationEventHandler(SelectionItemPatternIdentifiers.ElementAddedToSelectionEvent, element, scope, handler); Automation.AddAutomationEventHandler(SelectionItemPatternIdentifiers.ElementRemovedFromSelectionEvent, element, scope, handler); Automation.AddAutomationEventHandler(SelectionPatternIdentifiers.InvalidatedEvent, element, scope, handler); }
/// <summary> /// Register an event handler for InvokedEvent on the specified element. /// </summary> /// <param name="elementButton">The automation element.</param> public void SubscribeToInvoke(AutomationElement elementButton) { if (elementButton != null) { Automation.AddAutomationEventHandler(InvokePattern.InvokedEvent, elementButton, TreeScope.Element, UIAeventHandler = new AutomationEventHandler(OnUIAutomationEvent)); ElementSubscribeButton = elementButton; } }
public void EventTest() { int eventCount = 0; AutomationEventHandler handler = (o, e) => eventCount++; At.AddAutomationEventHandler(InvokePattern.InvokedEvent, child, TreeScope.Element, handler); childProvider.PerformInvoke(); Thread.Sleep(500); Assert.AreEqual(1, eventCount); }
public override void Stop() { if (!IsStarted) { return; } Log.Out(msg: "{0} Stop", (object)ToString()); Automation.RemoveAutomationEventHandler(eventId: this._eventId, element: this._root.AutomationElement, eventHandler: this._handlingDelegate); this._handlingDelegate = null; this._sinkReference = null; }
private void RemoveSelectionEvenhandler(AutomationElement element) { if (!IsInvalidAutomationElement(element)) { AutomationEventHandler handler = SelectionChanged; Automation.RemoveAutomationEventHandler(SelectionItemPatternIdentifiers.ElementSelectedEvent, element, handler); Automation.RemoveAutomationEventHandler(SelectionItemPatternIdentifiers.ElementAddedToSelectionEvent, element, handler); Automation.RemoveAutomationEventHandler(SelectionItemPatternIdentifiers.ElementRemovedFromSelectionEvent, element, handler); //Automation.RemoveAutomationEventHandler(SelectionPatternIdentifiers.InvalidatedEvent, element, handler); } }
// </Snippet1025> // <Snippet103> ///-------------------------------------------------------------------- /// <summary> /// Subscribe to the selection events of interest. /// </summary> /// <param name="selectionContainer"> /// Automation element that supports SelectionPattern /// </param> ///-------------------------------------------------------------------- private void SetSelectionEventHandlers (AutomationElement selectionContainer) { AutomationEventHandler selectionInvalidatedHandler = new AutomationEventHandler(SelectionInvalidatedHandler); Automation.AddAutomationEventHandler( SelectionPattern.InvalidatedEvent, selectionContainer, TreeScope.Element, SelectionInvalidatedHandler); }
internal static void Add( AutomationEvent eventId, AutomationElement element, TreeScope scope, AutomationEventHandler handlingDelegate) { var e = new AutomationEventHandlerImpl(eventId: eventId.Id, uiAutomationElement: element.IUIAutomationElement, handlingDelegate: handlingDelegate); var cacheRequest = AutomationElement.DefaultCacheRequest.IUIAutomationCacheRequest; Boundary.UIAutomation(a: () => Automation.AutomationClass.AddAutomationEventHandler(eventId: e._eventId, element: e._uiAutomationElement, scope: UiaConvert.Convert(treeScope: scope), cacheRequest: cacheRequest, handler: e)); Add(instance: e); }
public void TextSelectionChangedEvent() { int eventCount = 0; AutomationEventHandler handler = (o, e) => eventCount++; At.AddAutomationEventHandler(TextPattern.TextSelectionChangedEvent, textbox3Element, TreeScope.Element, handler); RunCommand("select textbox3"); Assert.AreEqual(1, eventCount, "TextSelectionChangedEvent fired"); At.RemoveAutomationEventHandler(TextPattern.TextSelectionChangedEvent, textbox3Element, handler); }
public virtual void Observe() { try { _eventHandler = new AutomationEventHandler(OnEvent); Automation.AddAutomationEventHandler(Event, AutomationElement.RootElement, TreeScope.Descendants, _eventHandler); } catch (Exception ex) { System.Diagnostics.Debug.Print($"{ex.Message}"); } }
private void Start() { var condition = new OrCondition(new PropertyCondition(AutomationElement.ClassNameProperty, ShellTrayWnd), new PropertyCondition(AutomationElement.ClassNameProperty, ShellSecondaryTrayWnd)); var cacheRequest = new CacheRequest(); cacheRequest.Add(AutomationElement.NameProperty); cacheRequest.Add(AutomationElement.BoundingRectangleProperty); _bars.Clear(); _children.Clear(); _lasts.Clear(); using (cacheRequest.Activate()) { var lists = Desktop.FindAll(TreeScope.Children, condition); if (lists == null) { Debug.WriteLine("Null values found, aborting"); return; } Debug.WriteLine(lists.Count + " bar(s) detected"); _lasts.Clear(); Parallel.ForEach(lists.OfType <AutomationElement>(), trayWnd => { var taskList = trayWnd.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, MSTaskListWClass)); if (taskList == null) { Debug.WriteLine("Null values found, aborting"); } else { _propChangeHandler = OnUIAutomationEvent; Automation.AddAutomationPropertyChangedEventHandler(taskList, TreeScope.Element, _propChangeHandler, AutomationElement.BoundingRectangleProperty); _bars.Add(trayWnd); _children.Add(trayWnd, taskList); _positionThreads[trayWnd] = Task.Run(() => LoopForPosition(trayWnd), _loopCancellationTokenSource.Token); } }); } _uiaEventHandler = OnUIAutomationEvent; Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, Desktop, TreeScope.Subtree, _uiaEventHandler); Automation.AddAutomationEventHandler(WindowPattern.WindowClosedEvent, Desktop, TreeScope.Subtree, _uiaEventHandler); SystemEvents.DisplaySettingsChanging += SystemEvents_DisplaySettingsChanged; }
// </Snippet103> // <Snippet104> ///-------------------------------------------------------------------- /// <summary> /// Register for events of interest. /// </summary> /// <param name="targetControl"> /// The automation element of interest. /// </param> ///-------------------------------------------------------------------- private void RegisterForAutomationEvents( AutomationElement targetControl) { AutomationEventHandler eventHandler = new AutomationEventHandler(OnWindowOpenOrClose); Automation.AddAutomationEventHandler( WindowPattern.WindowClosedEvent, targetControl, TreeScope.Element, eventHandler); Automation.AddAutomationEventHandler( WindowPattern.WindowOpenedEvent, targetControl, TreeScope.Element, eventHandler); }
public void AddAutomationEventHandler (AutomationEvent eventId, IElement element, TreeScope scope, AutomationEventHandler eventHandler) { if (element == null) // elements from local providers are not descendants of the RootElement. return; ClientElement clientElement = element as ClientElement; if (clientElement == null) { Log.Error ("[ClientAutomationSource.AddAutomationEventHandler] Not ClientElement"); return; } ClientEventManager.AddAutomationEventHandler (eventId, clientElement.Provider, scope, eventHandler); }
/// <summary> /// Registers a method that handles UI Automation events /// </summary> /// <param name="eventType">The specific event type to monitor</param> /// <param name="control">The control to monitor for events.</param> public static void RegisterEvent(AutomationEvent eventType, AutomationElement control) { _control = control; if (eventType.Id == InvokePattern.InvokedEvent.Id) { _eventHandler = new AutomationEventHandler(OnAutomationEvent); Automation.AddAutomationEventHandler(eventType, control, TreeScope.Element, _eventHandler); _patternEventType = eventType; return; } SubscribeToChildNotification(eventType, control); }
private static void StartProcessAndStartWindowWatching(Process process, Process childProcess, string processName) { childProcess.WaitForInputIdle(); WindowWatcher.process = process; WindowWatcher.processName = processName; RevalidateApp(); handler = GetOpenWindowHandler(process.Id); Automation.AddAutomationEventHandler( WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Descendants, handler); }
private void AddEventHandler( AutomationEvent automationEvent, AutomationEventHandler eventHandler) { _currentElement = AutomationElement.FocusedElement; if (_currentElement != null) { Automation.AddAutomationEventHandler( automationEvent, _currentElement, TreeScope.Element, eventHandler); } else this.UiFeedback("(no element)"); }
internal Waiter(AutomationEvent eventId, AutomationElement element = null, TreeScope scope = TreeScope.Descendants) { this._eventId = eventId; this._element = element ?? AutomationElement.RootElement; this._scope = scope; this._handler = new AutomationEventHandler((src, e) => { this.EvtArgs = e; this.Completion.SetResult(true); }); Automation.AddAutomationEventHandler( this._eventId, this._element, this._scope, this._handler); }
public static void AddAutomationEventHandler(AutomationEvent eventId, AutomationElement element, TreeScope scope, AutomationEventHandler eventHandler) { Utility.ValidateArgumentNonNull(element, "element"); Utility.ValidateArgumentNonNull(eventHandler, "eventHandler"); Utility.ValidateArgument(eventId != AutomationElement.AutomationFocusChangedEvent, "Use FocusChange notification instead"); Utility.ValidateArgument(eventId != AutomationElement.StructureChangedEvent, "Use StructureChange notification instead"); Utility.ValidateArgument(eventId != AutomationElement.AutomationPropertyChangedEvent, "Use PropertyChange notification instead"); try { BasicEventListener listener = new BasicEventListener(eventId, element, eventHandler); Factory.AddAutomationEventHandler( eventId.Id, element.NativeElement, (UIAutomationClient.TreeScope)scope, CacheRequest.CurrentNativeCacheRequest, listener); ClientEventList.Add(listener); } catch (System.Runtime.InteropServices.COMException e) { Exception newEx; if (Utility.ConvertException(e, out newEx)) { throw newEx; } else { throw; } } }
public WindowListner(IntPtr windowHandle) { Check.Require(windowHandle != IntPtr.Zero, "Window handle must not be zero"); // Listen for destruction event this.automationWindowElement = AutomationElement.FromHandle(windowHandle); this.automationWindowCloseEventHandler = new AutomationEventHandler(this.OnWindowCloseHandler); try { Automation.AddAutomationEventHandler( WindowPattern.WindowClosedEvent, this.automationWindowElement, TreeScope.Element, this.automationWindowCloseEventHandler); } catch (ArgumentException) { // Workaround for some specific windows like "Start button" that isn't following automation pattern } // Listen for move events this.moveListner = new AccessibleEventListener { MinimalEventType = AccessibleEventType.EVENT_OBJECT_LOCATIONCHANGE, MaximalEventType = AccessibleEventType.EVENT_OBJECT_LOCATIONCHANGE, ProcessId = (uint)GetPidFromWindow(windowHandle), Enabled = true, }; this.moveListner.EventOccurred += this.OnWindowMoveHandler; // Use fallback strategy that manually redraws tag position once in 1/3 of a second this.fallbackStrategyTimer = new DispatcherTimer(); this.fallbackStrategyTimer.Interval = TimeSpan.FromMilliseconds(300); this.fallbackStrategyTimer.Tick += this.FallbackStrategyHandler; this.fallbackStrategyTimer.Start(); }
public void RemoveAutomationEventHandler (AutomationEvent eventId, IElement element, AutomationEventHandler eventHandler) { if (element == null) return; ClientElement clientElement = element as ClientElement; if (clientElement == null) { Log.Error ("[ClientAutomationSource.RemoveAutomationEventHandler] Not ClientElement"); return; } ClientEventManager.RemoveAutomationEventHandler (eventId, clientElement.Provider, eventHandler); }
public override void HookEvents(UIItemEventListener eventListener) { handler = delegate { eventListener.EventOccured(new MultilineTextBoxEvent(this)); }; Automation.AddAutomationEventHandler(TextPattern.TextSelectionChangedEvent, automationElement, TreeScope.Element, handler); }
public override void Add(WrappedEventHandler handler, AutomationElementWrapper element) { _handler = (o, e) => handler(element, e); _element = element.Element; Automation.AddAutomationEventHandler(_eventId, _element, _scope, _handler); }
public void StartWindowMonitor() { var windows = new List<IntPtr>(); EnumWindowsProc ewDelegate = (wnd, param) => { if (wnd == IntPtr.Zero || !ShouldIncludeWindow(wnd)) { return true; } //HandleWindowOpenedEvent(wnd); windows.Add(wnd); return true; }; EnumWindows(ewDelegate, IntPtr.Zero); windows.ForEach(HandleWindowOpenedEvent); _windowEventHandler = (sender, args) => { var element = sender as AutomationElement; if (element == null) { return; } var handle = new IntPtr(element.Current.NativeWindowHandle); HandleWindowOpenedEvent(handle); }; Automation.AddAutomationEventHandler( WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Children, _windowEventHandler ); var activationListener = new AccessibleEventListener { MinimalEventType = AccessibleEventType.EVENT_SYSTEM_FOREGROUND, MaximalEventType = AccessibleEventType.EVENT_SYSTEM_FOREGROUND, ThreadId = 0, ProcessId = 0 }; activationListener.EventOccurred += (sender, args) => { HandleWindowActivatedEvent(args.HWnd); }; activationListener.Enabled = true; var closedListener = new AccessibleEventListener { MinimalEventType = AccessibleEventType.EVENT_OBJECT_DESTROY, MaximalEventType = AccessibleEventType.EVENT_OBJECT_DESTROY, ThreadId = 0, ProcessId = 0 }; closedListener.EventOccurred += (sender, args) => { if (args.ObjectID != (uint)AccessibleObjectID.OBJID_WINDOW) { return; } if (args.ChildID != 0) { return; } if (args.EventType == AccessibleEventType.EVENT_OBJECT_DESTROY) { HandleWindowClosedEvent(args.HWnd); } }; closedListener.Enabled = true; var titleChangeListener = new AccessibleEventListener { MinimalEventType = AccessibleEventType.EVENT_OBJECT_NAMECHANGE, MaximalEventType = AccessibleEventType.EVENT_OBJECT_NAMECHANGE, ThreadId = 0, ProcessId = 0 }; titleChangeListener.EventOccurred += (sender, args) => { if (!_openWindows.ContainsKey(args.HWnd) || args.ObjectID != (uint) AccessibleObjectID.OBJID_WINDOW) { return; } StringBuilder titleBuilder = new StringBuilder{Length = 300}; User32.GetWindowText(args.HWnd, titleBuilder, 300); if (_openWindows.ContainsKey(args.HWnd)) { _openWindows[args.HWnd].Name = titleBuilder.ToString(); } }; titleChangeListener.Enabled = true; }
/// ------------------------------------------------------------------- /// <summary> /// Method that registers the event handler OnEvent() /// </summary> /// ------------------------------------------------------------------- public virtual void AddEventHandler(AutomationEvent eventId, string eventName, AutomationElement element, TreeScope treeScope) { if (eventName == null) throw new ArgumentException(); base.AddEventHandler(); Comment("Adding AddAutomationEventHandler({0}, [{1}], TreeScope.{2}) Control Path = {3}", eventId.ProgrammaticName, Library.GetUISpyLook(element), treeScope.ToString(), Helpers.GetXmlPathFromAutomationElement(element)); handler = new AutomationEventHandler(OnEvent); Automation.AddAutomationEventHandler(eventId, element, treeScope, handler); }
public static void RemoveAutomationEventHandler(AutomationEvent eventId, AutomationElement element, AutomationEventHandler eventHandler) { Utility.ValidateArgumentNonNull(element, "element"); Utility.ValidateArgumentNonNull(eventHandler, "eventHandler"); Utility.ValidateArgument(eventId != AutomationElement.AutomationFocusChangedEvent, "Use FocusChange notification instead"); Utility.ValidateArgument(eventId != AutomationElement.StructureChangedEvent, "Use StructureChange notification instead"); Utility.ValidateArgument(eventId != AutomationElement.AutomationPropertyChangedEvent, "Use PropertyChange notification instead"); try { BasicEventListener listener = (BasicEventListener)ClientEventList.Remove(eventId, element, eventHandler); Factory.RemoveAutomationEventHandler(eventId.Id, element.NativeElement, listener); } catch (System.Runtime.InteropServices.COMException e) { Exception newEx; if (Utility.ConvertException(e, out newEx)) { throw newEx; } else { throw; } } }
/// ------------------------------------------------------------------- /// <summary></summary> /// ------------------------------------------------------------------- static void VoiceLanguageWarningForm() { Trace.WriteLine("Setting up window handler for the language error dialog"); _handler = new AutomationEventHandler(WindowOpenedEventHandler); Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Subtree, _handler); }
public static void AddAutomationEventHandler (AutomationEvent eventId, AutomationElement element, TreeScope scope, AutomationEventHandler eventHandler) { CheckAutomationEventId (eventId); ArgumentCheck.NotNull (element, "element"); ArgumentCheck.NotNull (eventHandler, "eventHandler"); //TODO In theory we shall also check scope not equals to Parent or Ancestors, //but .Net didn't test/throw exceptions for "scope" if (element == AutomationElement.RootElement) foreach (var source in SourceManager.GetAutomationSources ()) source.AddAutomationEventHandler ( eventId, null, scope, eventHandler); else { var source = element.SourceElement.AutomationSource; source.AddAutomationEventHandler ( eventId, element.SourceElement, scope, eventHandler); } }
/// -------------------------------------------------------------------- /// <summary> /// Handles the click event for the Start App button. /// </summary> /// <param name="sender">The object that raised the event.</param> /// <param name="e">Event arguments.</param> /// -------------------------------------------------------------------- private void btnStartApp_Click(object sender, RoutedEventArgs e) { _targetWindow = StartTargetApp(_filePath); if (_targetWindow == null) { return; } Feedback("Target started."); var clientLocationTop = Client.Top; var clientLocationRight = Client.Left + Client.Width + 100; var transformPattern = _targetWindow.GetCurrentPattern(TransformPattern.Pattern) as TransformPattern; transformPattern?.Move(clientLocationRight, clientLocationTop); // Get required control patterns // // Once you have an instance of an AutomationElement for the target // obtain a WindowPattern object to handle the WindowClosed event. try { var windowPattern = _targetWindow.GetCurrentPattern(WindowPattern.Pattern) as WindowPattern; } catch (InvalidOperationException) { Feedback("Object does not support the Window Pattern"); return; } // Register for an Event // // The WindowPattern allows you to programmatically // manipulate a window. // It also exposes a window closed event. // The following code shows an example of listening // for the WindowClosed event. // // To intercept the WindowClosed event for our target application // you define an AutomationEventHandler delegate. var targetClosedHandler = new AutomationEventHandler(OnTargetClosed); // Use AddAutomationEventHandler() to add this event handler. // When listening for a WindowClosed event you must either scope // the event to the automation element as done here, or cast // the AutomationEventArgs in the handler to WindowClosedEventArgs // and compare the RuntimeId of the automation element that raised // the WindowClosed event to the automation element in the // class member data. Automation.AddAutomationEventHandler( WindowPattern.WindowClosedEvent, _targetWindow, TreeScope.Element, targetClosedHandler); SetClientControlProperties(false, true); // Get our collection of interesting controls. _textControls = FindTextControlsInTarget(); }
private AutomationElement StartApp(string app) { if (File.Exists(app)) { AutomationElement targetElement; // Start application. var p = Process.Start(app); //// Give application a second to startup. Thread.Sleep(2000); targetElement = AutomationElement.FromHandle(p.MainWindowHandle); if (targetElement == null) { return null; } var targetClosedHandler = new AutomationEventHandler(OnTargetClose); Automation.AddAutomationEventHandler( WindowPattern.WindowClosedEvent, targetElement, TreeScope.Element, targetClosedHandler); // Set size and position of target. var targetTransformPattern = targetElement.GetCurrentPattern(TransformPattern.Pattern) as TransformPattern; if (targetTransformPattern == null) return null; targetTransformPattern.Resize(550, 400); targetTransformPattern.Move( _clientWindow.Left + _clientWindow.Width + 25, _clientWindow.Top); Output("Target started."); // Return the AutomationElement return (targetElement); } Output(app + " not found."); return (null); }
public static void RemoveAutomationEventHandler ( AutomationEvent eventId, AutomationElement element, AutomationEventHandler eventHandler) { CheckAutomationEventId (eventId); ArgumentCheck.NotNull (element, "element"); ArgumentCheck.NotNull (eventHandler, "eventHandler"); if (element == AutomationElement.RootElement) foreach (var source in SourceManager.GetAutomationSources ()) source.RemoveAutomationEventHandler ( eventId, element.SourceElement, eventHandler); else { var source = element.SourceElement.AutomationSource; source.RemoveAutomationEventHandler (eventId, element.SourceElement, eventHandler); } }
/// -------------------------------------------------------------------- /// <summary> /// Unsubscribe from the selection events. /// </summary> /// <remarks> /// The events are raised by the SelectionItem elements, /// not the Selection container. /// </remarks> /// -------------------------------------------------------------------- internal void RemoveTargetSelectionEventHandlers() { foreach (AutomationElement control in TargetControls) { var selectionHandler = new AutomationEventHandler(TargetSelectionHandler); Automation.RemoveAutomationEventHandler( SelectionItemPattern.ElementSelectedEvent, control, selectionHandler); Automation.RemoveAutomationEventHandler( SelectionItemPattern.ElementAddedToSelectionEvent, control, selectionHandler); Automation.RemoveAutomationEventHandler( SelectionItemPattern.ElementRemovedFromSelectionEvent, control, selectionHandler); } }
/// <summary> /// Called by a client to add a listener for pattern or custom events. /// </summary> /// <param name="eventId">A control pattern or custom event identifier.</param> /// <param name="element">Element on which to listen for control pattern or custom events.</param> /// <param name="scope">Specifies whether to listen to property changes events on the specified element, and/or its ancestors and children.</param> /// <param name="eventHandler">Delegate to call when the specified event occurs.</param> /// /// <outside_see conditional="false"> /// This API does not work inside the secure execution environment. /// <exception cref="System.Security.Permissions.SecurityPermission"/> /// </outside_see> public static void AddAutomationEventHandler( AutomationEvent eventId, AutomationElement element, TreeScope scope, AutomationEventHandler eventHandler ) { Misc.ValidateArgumentNonNull(element, "element" ); Misc.ValidateArgumentNonNull(eventHandler, "eventHandler" ); Misc.ValidateArgument( eventId != AutomationElement.AutomationFocusChangedEvent, SRID.EventIdMustNotBeAutomationFocusChanged ); Misc.ValidateArgument( eventId != AutomationElement.StructureChangedEvent,SRID.EventIdMustNotBeStructureChanged ); Misc.ValidateArgument( eventId != AutomationElement.AutomationPropertyChangedEvent, SRID.EventIdMustNotBeAutomationPropertyChanged ); if (eventId == WindowPattern.WindowClosedEvent) { // Once a window closes and the hwnd is destroyed we won't be able to determine where it was in the // Automation tree; therefore only support WindowClosed events for all windows (eg. src==root and scope // is descendants) or a specific WindowPattern element (src==root of a Window and scope is the element). // Also handle odd combinations (eg. src==specific element and scope is subtree|ancestors). bool paramsValidated = false; if ( Misc.Compare( element, AutomationElement.RootElement ) ) { // For root element need to have Descendants scope set (Note: Subtree includes Descendants) if ( ( scope & TreeScope.Descendants ) == TreeScope.Descendants ) { paramsValidated = true; } } else { // otherwise non-root elements must have the entire tree (Anscestors, Element and Descendants)... if ( ( scope & ( TreeScope.Ancestors | TreeScope.Element | TreeScope.Descendants ) ) == ( TreeScope.Ancestors | TreeScope.Element | TreeScope.Descendants ) ) { paramsValidated = true; } else if ( ( scope & TreeScope.Element ) == TreeScope.Element ) { // ...OR Element where the element implements WindowPattern // PRESHARP will flag this as warning 56506/6506:Parameter 'element' to this public method must be validated: A null-dereference can occur here. // False positive, element is checked, see above #pragma warning suppress 6506 object val = element.GetCurrentPropertyValue(AutomationElement.NativeWindowHandleProperty); if ( val != null && val is int && (int)val != 0 ) { if ( HwndProxyElementProvider.IsWindowPatternWindow( NativeMethods.HWND.Cast( new IntPtr( (int)val ) ) ) ) { paramsValidated = true; } } } } if ( !paramsValidated ) { throw new ArgumentException( SR.Get( SRID.ParamsNotApplicableToWindowClosedEvent ) ); } } // Add a client-side Handler for for this event request EventListener l = new EventListener(eventId, scope, null, CacheRequest.CurrentUiaCacheRequest); ClientEventManager.AddListener(element, eventHandler, l); }
public BasicEventListener(AutomationEvent eventKind, AutomationElement element, AutomationEventHandler handler) : base(eventKind.Id, element.GetRuntimeId(), handler) { Debug.Assert(handler != null); this._basicHandler = handler; }
protected internal void SubscribeToEvents(HasControlInputCmdletBase cmdlet, AutomationElement inputObject, AutomationEvent eventType, AutomationProperty prop) { AutomationEventHandler uiaEventHandler; AutomationPropertyChangedEventHandler uiaPropertyChangedEventHandler; StructureChangedEventHandler uiaStructureChangedEventHandler; AutomationFocusChangedEventHandler uiaFocusChangedEventHandler; // 20130109 if (null == CurrentData.Events) { CurrentData.InitializeEventCollection(); } try { CacheRequest cacheRequest = new CacheRequest(); cacheRequest.AutomationElementMode = AutomationElementMode.Full; //.None; cacheRequest.TreeFilter = Automation.RawViewCondition; cacheRequest.Add(AutomationElement.NameProperty); cacheRequest.Add(AutomationElement.AutomationIdProperty); cacheRequest.Add(AutomationElement.ClassNameProperty); cacheRequest.Add(AutomationElement.ControlTypeProperty); //cacheRequest.Add(AutomationElement.ProcessIdProperty); // cache patterns? // cacheRequest.Activate(); cacheRequest.Push(); switch (eventType.ProgrammaticName) { case "InvokePatternIdentifiers.InvokedEvent": this.WriteVerbose(cmdlet, "subscribing to the InvokedEvent handler"); Automation.AddAutomationEventHandler( InvokePattern.InvokedEvent, inputObject, TreeScope.Element, // TreeScope.Subtree, // TreeScope.Element, // uiaEventHandler = new AutomationEventHandler(OnUIAutomationEvent)); //uiaEventHandler = new AutomationEventHandler(handler)); //uiaEventHandler = new AutomationEventHandler(((EventCmdletBase)cmdlet).AutomationEventHandler)); uiaEventHandler = new AutomationEventHandler(cmdlet.AutomationEventHandler)); UIAHelper.WriteEventToCollection(cmdlet, uiaEventHandler); // 20130327 //this.WriteObject(cmdlet, uiaEventHandler); if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); } break; case "TextPatternIdentifiers.TextChangedEvent": this.WriteVerbose(cmdlet, "subscribing to the TextChangedEvent handler"); Automation.AddAutomationEventHandler( TextPattern.TextChangedEvent, inputObject, TreeScope.Element, // uiaEventHandler = new AutomationEventHandler(OnUIAutomationEvent)); //uiaEventHandler = new AutomationEventHandler(handler)); //uiaEventHandler = new AutomationEventHandler(((EventCmdletBase)cmdlet).AutomationEventHandler)); uiaEventHandler = new AutomationEventHandler(cmdlet.AutomationEventHandler)); UIAHelper.WriteEventToCollection(cmdlet, uiaEventHandler); // 20130327 //this.WriteObject(cmdlet, uiaEventHandler); if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); } break; case "TextPatternIdentifiers.TextSelectionChangedEvent": this.WriteVerbose(cmdlet, "subscribing to the TextSelectionChangedEvent handler"); Automation.AddAutomationEventHandler( TextPattern.TextSelectionChangedEvent, inputObject, TreeScope.Element, // uiaEventHandler = new AutomationEventHandler(OnUIAutomationEvent)); //uiaEventHandler = new AutomationEventHandler(handler)); //uiaEventHandler = new AutomationEventHandler(((EventCmdletBase)cmdlet).AutomationEventHandler)); uiaEventHandler = new AutomationEventHandler(cmdlet.AutomationEventHandler)); UIAHelper.WriteEventToCollection(cmdlet, uiaEventHandler); // 20130327 //this.WriteObject(cmdlet, uiaEventHandler); if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); } break; case "WindowPatternIdentifiers.WindowOpenedProperty": this.WriteVerbose(cmdlet, "subscribing to the WindowOpenedEvent handler"); Automation.AddAutomationEventHandler( WindowPattern.WindowOpenedEvent, inputObject, TreeScope.Subtree, // uiaEventHandler = new AutomationEventHandler(OnUIAutomationEvent)); //uiaEventHandler = new AutomationEventHandler(handler)); //uiaEventHandler = new AutomationEventHandler(((EventCmdletBase)cmdlet).AutomationEventHandler)); uiaEventHandler = new AutomationEventHandler(cmdlet.AutomationEventHandler)); UIAHelper.WriteEventToCollection(cmdlet, uiaEventHandler); // 20130327 //this.WriteObject(cmdlet, uiaEventHandler); if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); } break; case "AutomationElementIdentifiers.AutomationPropertyChangedEvent": if (prop != null) { this.WriteVerbose(cmdlet, "subscribing to the AutomationPropertyChangedEvent handler"); Automation.AddAutomationPropertyChangedEventHandler( inputObject, TreeScope.Subtree, uiaPropertyChangedEventHandler = // new AutomationPropertyChangedEventHandler(OnUIAutomationPropertyChangedEvent), //new AutomationPropertyChangedEventHandler(handler), //new AutomationPropertyChangedEventHandler(((EventCmdletBase)cmdlet).AutomationPropertyChangedEventHandler), new AutomationPropertyChangedEventHandler(cmdlet.AutomationPropertyChangedEventHandler), prop); UIAHelper.WriteEventToCollection(cmdlet, uiaPropertyChangedEventHandler); // 20130327 //this.WriteObject(cmdlet, uiaPropertyChangedEventHandler); if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaPropertyChangedEventHandler); } else { cmdlet.WriteObject(cmdlet, true); } } break; case "AutomationElementIdentifiers.StructureChangedEvent": this.WriteVerbose(cmdlet, "subscribing to the StructureChangedEvent handler"); Automation.AddStructureChangedEventHandler( inputObject, TreeScope.Subtree, uiaStructureChangedEventHandler = // new StructureChangedEventHandler(OnUIStructureChangedEvent)); //new StructureChangedEventHandler(handler)); //new StructureChangedEventHandler(((EventCmdletBase)cmdlet).StructureChangedEventHandler)); new StructureChangedEventHandler(cmdlet.StructureChangedEventHandler)); UIAHelper.WriteEventToCollection(cmdlet, uiaStructureChangedEventHandler); // 20130327 //this.WriteObject(cmdlet, uiaStructureChangedEventHandler); if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaStructureChangedEventHandler); } else { cmdlet.WriteObject(cmdlet, true); } break; case "WindowPatternIdentifiers.WindowClosedProperty": this.WriteVerbose(cmdlet, "subscribing to the WindowClosedEvent handler"); Automation.AddAutomationEventHandler( WindowPattern.WindowClosedEvent, inputObject, TreeScope.Subtree, // uiaEventHandler = new AutomationEventHandler(OnUIAutomationEvent)); //uiaEventHandler = new AutomationEventHandler(handler)); //uiaEventHandler = new AutomationEventHandler(((EventCmdletBase)cmdlet).AutomationEventHandler)); uiaEventHandler = new AutomationEventHandler(cmdlet.AutomationEventHandler)); UIAHelper.WriteEventToCollection(cmdlet, uiaEventHandler); // 20130327 //this.WriteObject(cmdlet, uiaEventHandler); if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); } break; case "AutomationElementIdentifiers.MenuClosedEvent": this.WriteVerbose(cmdlet, "subscribing to the MenuClosedEvent handler"); Automation.AddAutomationEventHandler( AutomationElement.MenuClosedEvent, inputObject, TreeScope.Subtree, uiaEventHandler = new AutomationEventHandler(cmdlet.AutomationEventHandler)); UIAHelper.WriteEventToCollection(cmdlet, uiaEventHandler); // 20130327 //this.WriteObject(cmdlet, uiaEventHandler); if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); } break; case "AutomationElementIdentifiers.MenuOpenedEvent": this.WriteVerbose(cmdlet, "subscribing to the MenuOpenedEvent handler"); Automation.AddAutomationEventHandler( AutomationElement.MenuOpenedEvent, inputObject, TreeScope.Subtree, uiaEventHandler = new AutomationEventHandler(cmdlet.AutomationEventHandler)); UIAHelper.WriteEventToCollection(cmdlet, uiaEventHandler); // 20130327 //this.WriteObject(cmdlet, uiaEventHandler); if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); } break; case "AutomationElementIdentifiers.ToolTipClosedEvent": this.WriteVerbose(cmdlet, "subscribing to the ToolTipClosedEvent handler"); Automation.AddAutomationEventHandler( AutomationElement.ToolTipClosedEvent, inputObject, TreeScope.Subtree, uiaEventHandler = new AutomationEventHandler(cmdlet.AutomationEventHandler)); UIAHelper.WriteEventToCollection(cmdlet, uiaEventHandler); // 20130327 //this.WriteObject(cmdlet, uiaEventHandler); if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); } break; case "AutomationElementIdentifiers.ToolTipOpenedEvent": this.WriteVerbose(cmdlet, "subscribing to the ToolTipOpenedEvent handler"); Automation.AddAutomationEventHandler( AutomationElement.ToolTipOpenedEvent, inputObject, TreeScope.Subtree, uiaEventHandler = new AutomationEventHandler(cmdlet.AutomationEventHandler)); UIAHelper.WriteEventToCollection(cmdlet, uiaEventHandler); // 20130327 //this.WriteObject(cmdlet, uiaEventHandler); if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); } break; case "AutomationElementIdentifiers.AutomationFocusChangedEvent": WriteVerbose(cmdlet, "subscribing to the AutomationFocusChangedEvent handler"); Automation.AddAutomationFocusChangedEventHandler( //AutomationElement.AutomationFocusChangedEvent, //inputObject, //System.Windows.Automation.AutomationElement.RootElement, //TreeScope.Subtree, uiaFocusChangedEventHandler = new AutomationFocusChangedEventHandler(cmdlet.AutomationEventHandler)); UIAHelper.WriteEventToCollection(cmdlet, uiaFocusChangedEventHandler); // 20130327 //this.WriteObject(cmdlet, uiaFocusChangedEventHandler); if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaFocusChangedEventHandler); } else { cmdlet.WriteObject(cmdlet, true); } break; default: this.WriteVerbose(cmdlet, "the following event has not been subscribed to: " + eventType.ProgrammaticName); break; } this.WriteVerbose(cmdlet, "on the object " + inputObject.Current.Name); cacheRequest.Pop(); } catch (Exception e) { // try { // ErrorRecord err = new ErrorRecord( // e, // "RegisteringEvent", // ErrorCategory.OperationStopped, // inputObject); // err.ErrorDetails = // new ErrorDetails("Unable to register event handler " + // // handler.ToString()); // eventType.ProgrammaticName + // " for " + // inputObject.Current.Name); // // this.OnSuccessAction.ToString()); // WriteError(this, err, false); // } // catch { // ErrorRecord err = new ErrorRecord( // e, // "RegisteringEvent", // ErrorCategory.OperationStopped, // inputObject); // err.ErrorDetails = // new ErrorDetails("Unable to register event handler " + // eventType.ProgrammaticName);; // WriteError(this, err, false); // } WriteVerbose(cmdlet, "Unable to register event handler " + eventType.ProgrammaticName + " for " + inputObject.Current.Name); WriteVerbose(cmdlet, e.Message); } }
public override void HookEvents(UIItemEventListener eventListener) { handler = delegate { eventListener.EventOccured(new RadioButtonEvent(this)); }; Automation.AddAutomationEventHandler(SelectionItemPattern.ElementSelectedEvent, automationElement, TreeScope.Element, handler); }
public override void HookEvents(UIItemEventListener eventListener) { handler = delegate { }; Automation.AddAutomationEventHandler(AutomationElement.MenuOpenedEvent, automationElement, TreeScope.Descendants, handler); }
public AutomationEventEntry (AutomationEvent eventId, IRawElementProviderSimple provider, TreeScope scope, AutomationEventHandler handler) : base (provider, scope) { this.EventId = eventId; this.Handler = handler; }
static void Main(string[] args) { windowTitle = "Infragistics NetAdvantage Windows Forms 2011.1"; string nextButton = "512"; steps = new string[][] { new string[] {nextButton}, // start new string[] {"604", nextButton}, // license new string[] {"602", nextButton}, // optionalInstall new string[] {nextButton}, // userDetails new string[] {nextButton}, // installFolder new string[] {"578"}, // installType new string[] {"596"}, // installFinally new string[] {"734"}, // finishFinally }; step = 0; AutomationEventHandler eventHandler = new AutomationEventHandler(OnWindowOpen); Automation.AddAutomationEventHandler( WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Descendants, eventHandler); if (args.Length != 1) { Console.WriteLine("Please pass as a first argument the path to the installer to automate."); System.Environment.Exit(1); } System.Diagnostics.Process.Start(args[0]); Console.ReadLine(); }
protected virtual void HookClickEvent(UIItemEventListener eventListener) { handler = delegate { eventListener.EventOccured(new UIItemClickEvent(this)); }; Automation.AddAutomationEventHandler(InvokePattern.InvokedEvent, automationElement, TreeScope.Element, handler); }
/// <summary> /// Called by a client to remove a listener for pattern or custom events. /// </summary> /// <param name="eventId">a UIAccess or custom event identifier.</param> /// <param name="element">Element to remove listener for</param> /// <param name="eventHandler">The handler object that was passed to AddEventListener</param> /// /// <outside_see conditional="false"> /// This API does not work inside the secure execution environment. /// <exception cref="System.Security.Permissions.SecurityPermission"/> /// </outside_see> public static void RemoveAutomationEventHandler( AutomationEvent eventId, AutomationElement element, AutomationEventHandler eventHandler ) { Misc.ValidateArgumentNonNull(element, "element" ); Misc.ValidateArgumentNonNull(eventHandler, "eventHandler" ); Misc.ValidateArgument( eventId != AutomationElement.AutomationFocusChangedEvent, SRID.EventIdMustNotBeAutomationFocusChanged ); Misc.ValidateArgument( eventId != AutomationElement.StructureChangedEvent, SRID.EventIdMustNotBeStructureChanged ); Misc.ValidateArgument( eventId != AutomationElement.AutomationPropertyChangedEvent, SRID.EventIdMustNotBeAutomationPropertyChanged ); //CASRemoval:AutomationPermission.Demand( AutomationPermissionFlag.Read ); // Remove the client-side listener for for this event ClientEventManager.RemoveListener( eventId, element, eventHandler ); }
/// -------------------------------------------------------------------- /// <summary> /// Subscribe to the selection events. /// </summary> /// <remarks> /// The events are raised by the SelectionItem elements, /// not the Selection container. /// </remarks> /// -------------------------------------------------------------------- internal void SetTargetSelectionEventHandlers() { foreach (AutomationElement control in TargetControls) { var selectionHandler = new AutomationEventHandler(TargetSelectionHandler); Automation.AddAutomationEventHandler( SelectionItemPattern.ElementSelectedEvent, control, TreeScope.Element | TreeScope.Descendants, selectionHandler); Automation.AddAutomationEventHandler( SelectionItemPattern.ElementAddedToSelectionEvent, control, TreeScope.Element | TreeScope.Descendants, selectionHandler); Automation.AddAutomationEventHandler( SelectionItemPattern.ElementRemovedFromSelectionEvent, control, TreeScope.Element | TreeScope.Descendants, selectionHandler); } }