Beispiel #1
0
            /// <summary>
            /// ウィンドウ選択開始
            /// </summary>
            /// <exception cref="WindowSelectionStartException"></exception>
            public void StartWindowSelection()
            {
                try
                {
                    if (Handle == System.IntPtr.Zero)
                    {
                        SelectedHwnd = System.IntPtr.Zero;
                        FrameWindow  = new FrameWindow
                        {
                            BackColor = ColorFrameWindow,
                            Visible   = false
                        };
                        FrameWindow.Show();

                        HookCallback = MouseHookProcedure;
                        Handle       = NativeMethods.SetWindowsHookEx(14, HookCallback, System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetEntryAssembly().GetModules()[0]), 0); // 14 = WH_MOUSE_LL
                        if (Handle == System.IntPtr.Zero)
                        {
                            throw new System.ComponentModel.Win32Exception();
                        }
                    }
                }
                catch
                {
                    Dispose();
                    throw new WindowSelectionStartException();
                }
            }
        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));
        }