Esempio n. 1
0
        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 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;
        }