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; }