// OnWindowShowOrOpen - Called by the WindowShowOrOpenTracker class when UI is shown or created private static void OnWindowShowOrOpen(IntPtr hwnd, AutomationElement rawEl) { bool doWindowOpenedEvent = false; bool doStructureChangedEvent = false; lock ( _classLock ) { if (_listeners != null) { // if rawEl is w/in the scope of any listeners then register for events in the new UI for (int i = 0; i < _listeners.Count; i++) { EventListenerClientSide ec = (EventListenerClientSide)_listeners[i]; EventListener l = ec.EventListener; if (l.EventId == WindowPattern.WindowOpenedEvent) { doWindowOpenedEvent = true; } if (l.EventId == AutomationElement.StructureChangedEvent) { doStructureChangedEvent = true; } // Only advise UI contexts if the provider might raise that event. if (!ShouldAdviseProviders(l.EventId)) { continue; } // Only advise UI contexts if the element is w/in scope of the reference element if (!ec.WithinScope(rawEl)) { continue; } // Notify the server side UiaCoreApi.UiaEventAddWindow(ec.EventHandle, hwnd); } } } // Piggy-back on the listener for Windows hiding or closing to raise WindowClosed and StructureChanged events. if (doWindowOpenedEvent) { if (HwndProxyElementProvider.IsWindowPatternWindow(NativeMethods.HWND.Cast(hwnd))) { // Go ahead and raise a client-side only WindowOpenedEvent (if anyone is listening) AutomationEventArgs e = new AutomationEventArgs(WindowPattern.WindowOpenedEvent); RaiseEventInThisClientOnly(WindowPattern.WindowOpenedEvent, rawEl, e); } } if (doStructureChangedEvent) { // Filter on the control elements. Otherwise, this is extremely noisy. Consider not filtering if there is feedback. //ControlType ct = (ControlType)rawEl.GetPropertyValue( AutomationElement.ControlTypeProperty ); //if ( ct != null ) { // Last,raise an event for structure changed StructureChangedEventArgs e = new StructureChangedEventArgs(StructureChangeType.ChildAdded, rawEl.GetRuntimeId()); RaiseEventInThisClientOnly(AutomationElement.StructureChangedEvent, rawEl, e); } } }