Exemplo n.º 1
0
 public void Update()
 {
     if (!IsTopMostActiveWindowDifferent(out _args))
     {
         _args = null;
     }
 }
Exemplo n.º 2
0
            public bool IsTopMostActiveWindowDifferent(out UnexpectedAlertEventArgs args)
            {
                var windows = GetWindows();

                args = null;
                foreach (var window in windows)
                {
                    if (_childWindows.Contains(window))
                    {
                        continue;
                    }

                    /*raiseEvent =
                     *  behavior == UnexpectedAlertBehaviorReaction.DismissAndNotify ||
                     *  behavior == UnexpectedAlertBehaviorReaction.AcceptAndNotify;*/

                    /*if (raiseEvent)
                     * {*/
                    args = new UnexpectedAlertEventArgs
                    {
                        Title   = window.Title,
                        Content = window.AllDescendantWindows
                                  .Where(s => s.Visible)
                                  .Select(d => d.Title?.Trim())
                                  .Where(s => !string.IsNullOrEmpty(s))
                                  .ToArray()
                    };

                    // TODO requires rewrite

                    // menus slip through
                    AutomationElement aw = null;
                    try
                    {
                        aw = AutomationElement.FromHandle(window.HWnd);
                    }
                    catch (ElementNotAvailableException)
                    {
                        continue; // menus are causing this exception
                    }

                    if (aw.Current.ControlType != ControlType.Window)
                    {
                        continue; // but sometimes they slip through
                    }

                    System.Diagnostics.Debug.WriteLine("New modal window " + window.HWnd);

                    return(true);
                }

                return(false);
            }
Exemplo n.º 3
0
            public Handler(AutomationElement automationElement, IntPtr hWnd, CommandEnvironment commandEnvironment)
            {
                var behavior = commandEnvironment.UnexpectedAlertBehavior;

                if (behavior == UnexpectedAlertBehaviorReaction.Ignore)
                {
                    return;
                }

                _rootWindow = new SystemWindow(hWnd);

                IList <SystemWindow> GetWindows()
                {
                    // dialog windows happen not be in the same tree as the root window
                    return(SystemWindow.DesktopWindow.AllDescendantWindows
                           .Where(w => w.Visible && w.TopMost && w.Process.MainWindowHandle == hWnd)
                           .ToList());
                }

                _childWindows = GetWindows();

                var token = _cancellationTokenSource.Token;

                _thread = new Thread(() =>
                {
                    while (true)
                    {
                        //System.Diagnostics.Debug.WriteLine("Thread work ... " + Thread.CurrentThread.ManagedThreadId);
                        if (token.IsCancellationRequested)
                        {
                            break;
                        }

                        var windows = GetWindows();
                        UnexpectedAlertEventArgs args = null;
                        var raiseEvent = false;

                        foreach (var window in windows)
                        {
                            if (_childWindows.Contains(window) || !window.Visible)
                            {
                                continue;
                            }

                            raiseEvent =
                                behavior == UnexpectedAlertBehaviorReaction.DismissAndNotify ||
                                behavior == UnexpectedAlertBehaviorReaction.AcceptAndNotify;

                            if (raiseEvent)
                            {
                                args = new UnexpectedAlertEventArgs
                                {
                                    Title   = window.Title,
                                    Content = window.AllDescendantWindows
                                              .Where(s => s.Visible)
                                              .Select(d => d.Title?.Trim())
                                              .Where(s => !string.IsNullOrEmpty(s))
                                              .ToArray()
                                };
                            }

                            // menus slip through
                            AutomationElement aw = null;
                            try
                            {
                                aw = AutomationElement.FromHandle(window.HWnd);
                            }
                            catch (ElementNotAvailableException)
                            {
                                continue; // menus are causing this exception
                            }

                            if (aw.Current.ControlType != ControlType.Window)
                            {
                                continue; // but sometimes they slip through
                            }

                            System.Diagnostics.Debug.WriteLine("New modal window " + window.HWnd);

                            switch (behavior)
                            {
                            case UnexpectedAlertBehaviorReaction.Ignore:
                                break;

                            case UnexpectedAlertBehaviorReaction.Dismiss:
                            case UnexpectedAlertBehaviorReaction.DismissAndNotify:
                            case UnexpectedAlertBehaviorReaction.Accept:
                            case UnexpectedAlertBehaviorReaction.AcceptAndNotify:
                                //window.SendClose();
                                break;
                            }
                        }

                        if (raiseEvent)
                        {
                            OnUnexpectedAlert?.Invoke(this, args);
                        }

                        _childWindows = GetWindows();

                        Thread.Sleep(1000);
                    }
                });

                _thread.Start(); // TODO new thread for each session (OK), not disposed properly
            }