Example #1
0
        private void MouseHook_OnMouseUp(object sender, Point p)
        {
            //Point GetMousePos = Mouse.GetPosition(App.Current.MainWindow);
            //Name = System.Windows.Forms.Cursor.Position.X + "," + System.Windows.Forms.Cursor.Position.Y;
            MouseHook.SystemParametersInfo((uint)MouseHook.SystemParametersDesktopInfo.SPI_SETCURSORS, 0, IntPtr.Zero, (uint)MouseHook.SystemParametersDesktopInfo.SPIF_SENDWININICHANGE);
            MouseHook.OnMouseUp -= MouseHook_OnMouseUp;
            //Name = Control.MousePosition.X + "," + Control.MousePosition.Y;
            var handle = MouseHook.WindowFromPoint(new POINT(Control.MousePosition.X, Control.MousePosition.Y));
            var title  = new StringBuilder(256);

            User32Window.GetWindowText(handle, title, title.Capacity);//得到窗口的标题
            var className = new StringBuilder(256);

            User32Window.GetClassName(handle, className, className.Capacity);//得得到窗口的句柄 类名
            //SelectWindow.Title = title.ToString();
            //SelectWindow.ClassName = className.ToString();
            User32Window.GetWindowThreadProcessId(handle, out int pid);
            //SelectWindow.Process = Process.GetProcessById(pid);
            SelectWindow = new HandleWindow
            {
                Title     = title.ToString(),
                ClassName = className.ToString(),
                Handle    = handle,
                Process   = Process.GetProcessById(pid)
            };
        }
        /// <summary>
        /// 拖拽指针获取目标窗口
        /// </summary>
        /// <param name="action">目标窗口回调</param>
        public void GetMoveMouseDownWindow(Action <NativeWindowModel> action)
        {
            void MouseHook_OnMouseUp(object?sender, Point p)
            {
                //Point GetMousePos = Mouse.GetPosition(App.Current.MainWindow);
                //Name = System.Windows.Forms.Cursor.Position.X + "," + System.Windows.Forms.Cursor.Position.Y;
                MouseHook.SystemParametersInfo((uint)MouseHook.SystemParametersDesktopInfo.SPI_SETCURSORS, 0, IntPtr.Zero, (uint)MouseHook.SystemParametersDesktopInfo.SPIF_SENDWININICHANGE);
                MouseHook.OnMouseUp -= MouseHook_OnMouseUp;
                //Name = Control.MousePosition.X + "," + Control.MousePosition.Y;
                MouseHook.GetCursorPos(out var pointInt);
                var handle = MouseHook.WindowFromPoint(pointInt);
                var title  = new StringBuilder(256);

                User32Window.GetWindowText(handle, title, title.Capacity);//得到窗口的标题
                var className = new StringBuilder(256);

                User32Window.GetClassName(handle, className, className.Capacity);//得得到窗口的句柄 类名
                //SelectWindow.Title = title.ToString();
                //SelectWindow.ClassName = className.ToString();
                User32Window.GetWindowThreadProcessId(handle, out int pid);
                //SelectWindow.Process = Process.GetProcessById(pid);
                try
                {
                    var    process = Process.GetProcessById(pid);
                    string?path    = null;
                    if (process != null)
                    {
                        try
                        {
                            path = process.MainModule?.FileName;
                        }
                        catch // 32位进程无法访问64位进程
                        {
                            path = NativeMethods.QueryFullProcessImageName(process);
                        }
                    }
                    var window = new NativeWindowModel
                    {
                        Title     = title.ToString(),
                        ClassName = className.ToString(),
                        Handle    = handle,
                        Process   = process,
                        Path      = path,
                        Name      = process?.ProcessName,
                    };
                    action?.Invoke(window);
                }
                catch (Exception e)
                {
                    e.LogAndShowT(TAG);
                }
            }

            MouseHook.SetSystemCursor(MouseHook.LoadCursor(IntPtr.Zero, MouseHook.OCR_CROSS), MouseHook.OCR_NORMAL);
            MouseHook.OnMouseUp += MouseHook_OnMouseUp;
        }
        /// <summary>
        /// Copy the current code to the clipboard
        /// </summary>
        public void CopyCodeToClipboard(string code = null, bool showError = false)
        {
            if (code == null)
            {
                code = this.CurrentCode;
            }

            bool clipRetry = false;

            do
            {
                bool failed = false;
                // check if the clipboard is locked
                IntPtr hWnd = User32Window.GetOpenClipboardWindow();
                if (hWnd != IntPtr.Zero)
                {
                    int len = User32Window.GetWindowTextLength(hWnd);
                    if (len == 0)
                    {
                        //WinAuthMain.LogException(new ApplicationException("Clipboard in use by another process"));
                    }
                    else
                    {
                        StringBuilder sb = new StringBuilder(len + 1);
                        User32Window.GetWindowText(hWnd, sb, sb.Capacity);
                        //WinAuthMain.LogException(new ApplicationException("Clipboard in use by '" + sb.ToString() + "'"));
                    }

                    failed = true;
                }
                else
                {
                    // Issue#170: can still get error copying even though it works, so just increase retries and ignore error
                    try
                    {
                        Clipboard.Clear();

                        // add delay for clip error
                        System.Threading.Thread.Sleep(100);
                        Clipboard.SetDataObject(code, true);
                    }
                    catch (ExternalException)
                    {
                    }
                }

                if (failed == true && showError == true)
                {
                    // only show an error the first time
                    //clipRetry = (MessageBox.Show(form, strings.ClipboardInUse,
                    //    WinAuthMain.APPLICATION_NAME,
                    //    MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes);
                }
            }while (clipRetry == true);
        }