在按键+鼠标上移动一个窗口(如linux ALT +鼠标向下)

很简单,我想移动一个按ALT + MOUSE的窗口,就像linux os(ALT +拖动)一样。

有可能将win32 api(移动api)传递到有兴趣点击它的窗口吗?

我有一个按下挂钩键的Windows服务(特定的ALT按钮)。 当按下ALT键并validation鼠标按下事件时,我想在任何地方移动窗口,而不仅仅是在标题栏上!

目前我以这种方式移动我的表单窗口:

using System.Runtime.InteropServices; [DllImport( "user32.dll", CharSet = CharSet.Auto, SetLastError = false )] static extern IntPtr SendMessage( IntPtr hWnd, uint Msg, int wParam, int lParam ); [DllImportAttribute( "user32.dll", CharSet = CharSet.Auto, SetLastError = false )] public static extern bool ReleaseCapture(); private void Form1_MouseDown( object sender, MouseEventArgs e ) { ReleaseCapture(); SendMessage( this.Handle, 0xa1, 0x2, 0 ); } 

如何通过单击并在调用SendMessage()之后获取特定窗口的窗口句柄?

这是可能的?

您可以通过捕获Windows发送的WM_NCHITTEST消息来查看窗口的哪个区域被单击。 您可以通过返回HTCAPTION来欺骗它,它会尽职尽责地执行您在单击窗口标题时通常会获得的鼠标操作。 包括移动窗口。 将此代码粘贴到您的表单中:

  protected override void WndProc(ref Message m) { base.WndProc(ref m); // Trap WM_NCHITTEST when the ALT key is down if (m.Msg == 0x84 && (Control.ModifierKeys == Keys.Alt)) { // Translate HTCLIENT to HTCAPTION if (m.Result == (IntPtr)1) m.Result = (IntPtr)2; } } 

我自己解决了这个问题,想出了一些有趣的事情,对我来说,对任何窗口(任何活动的前景窗口)都很有用。 有点长,但很容易理解,如果你按照评论,希望它有所帮助:)它的工作方式,是你按下某个注册的键组合,如Ctrl + Alt + M,鼠标将坚持在中心在活动窗口中,您移动鼠标,窗口跟随它,再次按下SAME组合,以释放,不需要鼠标点击或任何东西。

 public void MoveWindow_AfterMouse() { // 1- get a handle to the foreground window (or any window that you want to move). // 2- set the mouse pos to the window's center. // 3- let the window move with the mouse in a loop, such that: // win(x) = mouse(x) - win(width)/2 // win(y) = mouse(y) - win(height)/2 // This is because the origin (point of rendering) of the window, is at its top-left corner and NOT its center! // 1- IntPtr hWnd = WinAPIs.GetForegroundWindow(); // 2- Then: // first we need to get the x, y to the center of the window. // to do this, we have to know the width/height of the window. // to do this, we could use GetWindowRect which will give us the coords of the bottom right and upper left corners of the window, // with some math, we could deduce the width/height of the window. // after we do that, we simply set the x, y coords of the mouse to that center. RECT wndRect = new RECT(); WinAPIs.GetWindowRect(hWnd, out wndRect); int wndWidth = wndRect.right - wndRect.left; int wndHeight = wndRect.bottom - wndRect.top; // cuz the more you go down, the more y value increases. Point wndCenter = new Point(wndWidth / 2, wndHeight / 2); // this is the center of the window relative to itself. WinAPIs.ClientToScreen(hWnd, out wndCenter); // this will make its center relative to the screen coords. WinAPIs.SetCursorPos(wndCenter.X, wndCenter.Y); // 3- Moving :))) while (true) { Point cursorPos = new Point(); WinAPIs.GetCursorPos(out cursorPos); int xOffset = cursorPos.X - wndWidth / 2; int yOffset = cursorPos.Y - wndHeight / 2; WinAPIs.MoveWindow(hWnd, xOffset, yOffset, wndWidth, wndHeight, true); Thread.Sleep(25); } } 

现在:

 int moveCommandToggle = 0; protected override void WndProc(ref Message m) { if (m.Msg == 0x0312) { int keyID = m.WParam.ToInt32(); if(keyID == some_key_combo_you_registered_for_the_moving) { if (moveCommandToggle++ % 2 == 0) { mover = new Thread(() => MoveWindow_AfterMouse()); mover.Start(); } else mover.Abort(); } } } 

如果你想知道RECT:

  public struct RECT { public int left; // xCoor of upper left corner. public int top; // yCoor of upper left corner. public int right; // xCoor of lower right corner. public int bottom; // yCoor of lower right corner. }; 

WinAPIs只是一个静态类,我做了我的DllImports。