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

时间:2010-06-23 10:14:27

标签: c# winforms mouse key move

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

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

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

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

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()之后获取特定窗口的窗口句柄?

有可能吗?

2 个答案:

答案 0 :(得分:1)

您可以通过捕获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;
        }
    }

答案 1 :(得分:0)

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

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中完成的静态类。