Exemple #1
0
        // 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);
                }
            }
        }
Exemple #2
0
        // OnWindowHideOrClose - Called by the WindowHideOrCloseTracker class when UI is hidden or destroyed
        private static void OnWindowHideOrClose(IntPtr hwnd, AutomationElement rawEl, int [] runtimeId)
        {
            bool doWindowClosedEvent     = false;
            bool doStructureChangedEvent = false;

            lock ( _classLock )
            {
                if (_listeners != null)
                {
                    // if an hwnd is hidden or closed remove event listeners for the window's provider
                    for (int i = 0; i < _listeners.Count; i++)
                    {
                        EventListenerClientSide ec = (EventListenerClientSide)_listeners[i];

                        EventListener l = ec.EventListener;
                        if (l.EventId == WindowPattern.WindowClosedEvent)
                        {
                            doWindowClosedEvent = true;
                        }
                        if (l.EventId == AutomationElement.StructureChangedEvent)
                        {
                            doStructureChangedEvent = true;
                        }

                        // Only advise UI contexts if the provider still exists
                        // (but keep looking to see if need to do a WindowClosedEvent)
                        if (rawEl == null)
                        {
                            continue;
                        }

                        // 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 that this event is no longer interesting
                        UiaCoreApi.UiaEventRemoveWindow(ec.EventHandle, hwnd);
                    }
                }
            }

            // Piggy-back on the listener for Windows hiding or closing to raise WindowClosed and StructureChanged events.
            // When the hwnd behind rawEl is being destroyed, it can't be determined that rawEl once had the
            // WindowPattern interface.  Therefore raise an event for any window close.
            if (doWindowClosedEvent)
            {
                // When the hwnd is just hidden, rawEl will not be null, so can test if this would support WindowPattern
                // and throw this event away if the window doesn't support that CP
                if (rawEl != null && !HwndProxyElementProvider.IsWindowPatternWindow(NativeMethods.HWND.Cast(hwnd)))
                {
                    return;
                }

                // Go ahead and raise a client-side only WindowClosedEvent (if anyone is listening)
                WindowClosedEventArgs e = new WindowClosedEventArgs(runtimeId);
                RaiseEventInThisClientOnly(WindowPattern.WindowClosedEvent, runtimeId, e);
            }
            if (doStructureChangedEvent)
            {
                // Raise an event for structure changed.  This element has essentially gone away so there isn't an
                // opportunity to do filtering here.  So, just like WindowClosed, this event will be very noisy.
                StructureChangedEventArgs e = new StructureChangedEventArgs(StructureChangeType.ChildRemoved, runtimeId);
                RaiseEventInThisClientOnly(AutomationElement.StructureChangedEvent, runtimeId, e);
            }
        }