Ejemplo n.º 1
0
        protected override void OnMouseMove(MouseEventArgs e)
        {
            // do not run unless we are actually selecting a window.
            if (!Focusable)
            {
                return;
            }

            var pos = new POINT();

            if (!GetCursorPos(ref pos))
            {
                return;
            }

            var hwnd = WindowFromPoint(pos);

            if (_currentFrameWindow != null && hwnd == _currentFrameWindow.Hwnd)
            {
                return;
            }

            // select the main window.
            while (true)
            {
                var parentHwnd = GetParent(hwnd);

                if (parentHwnd == IntPtr.Zero)
                {
                    break;
                }

                hwnd = parentHwnd;
            }

            if (hwnd == _currentWindowHwnd)
            {
                return;
            }

            int processId;
            var threadId = GetWindowThreadProcessId(hwnd, out processId);

            var process = Process.GetProcessById(processId);

            _currentWindow = new Window
            {
                Hwnd    = hwnd,
                Process = process,
            };

            _currentWindowHwnd = hwnd;

            _canSelectCurrentWindow = _selfProcessId != processId;

            if (_canSelectCurrentWindow)
            {
                try
                {
                    var path = _currentWindow.Process.MainModule.FileName;

                    _canSelectCurrentWindow = Path.GetFileName(path) != "explorer.exe";
                }
                catch (Win32Exception)
                {
                    // NB when this application is not running in 64-bit we'll get a:
                    //     System.ComponentModel.Win32Exception
                    //     A 32 bit processes cannot access modules of a 64 bit process.

                    _canSelectCurrentWindow = false;
                }
            }

            if (_canSelectCurrentWindow)
            {
                RECT rect;

                if (GetWindowRect(hwnd, out rect))
                {
                    if (_currentFrameWindow == null)
                    {
                        _currentFrameWindow = new FrameWindow();
                    }

                    _currentFrameWindow.Hide();
                    _currentFrameWindow.Resize(rect);
                }
                else
                {
                    _canSelectCurrentWindow = false;
                }
            }

            if (_currentFrameWindow != null)
            {
                if (_canSelectCurrentWindow)
                {
                    _currentFrameWindow.Show();
                }
                else
                {
                    _currentFrameWindow.Hide();
                }
            }

            OnSelectWindow(new RoutedWindowEventArgs(SelectWindowEvent, _canSelectCurrentWindow ? _currentWindow : null));
        }