0

如果我的某些情况发生,我正在寻找一种防止鼠标移动的方法。

请注意,我不想处理 OnMouseMove 事件,因为这对我来说太晚了。

我需要类似 Cursor.Clip 属性的东西。是否有任何 API 能够禁用某些鼠标移动方向?一种 ApiMouse.DisableMovement(Directio.Up | Direction.Down | Direction.Left | Direction.Right) ?

4

2 回答 2

1

如果您不喜欢覆盖OnMouseMove,那么您可能会喜欢覆盖WndProc。“捕捉”窗口鼠标移动消息并根据您的规则丢弃它们。这是我知道的最快的方法。

private const int WM_MOUSEMOVE = 0x0200;
private const int MK_LBUTTON = 0x0001;
protected override void WndProc(ref Messsage msg)
{
    switch(msg.Msg)
    {
      case WM_MOUSEMOVE: // If you don't want the cursor to move, prevent the code from reaching the call to base.WndProc
              switch (msg.WParam.ToInt32())
              {
                    case MK_LBUTTON: // the left button was clicked
                         break; 
              }
              break;
    }
    base.WndProc(ref msg);
}

使用LParamWParam为您提供有关鼠标当前状态的更多信息。检查这个以获得更好的理解。

编辑 以获得鼠标的坐标,检查这个问题。. 它表明:

int x = msg.LParam.ToInt32() & 0x0000FFFF;
int y = (int)((msg.LParam.ToInt32() & 0xFFFF0000) >> 16)
Point pos = new Point(x, y);
于 2012-07-21T21:23:26.427 回答
1

在对先前答案的评论中,您参考了先前的问题“在客户端桌面区域内移动表单”。已接受答案中的代码可防止将窗口移出桌面。现在您想在用户拖动它时将光标“固定”到窗口上。因此,如果窗口不能移动得更远,光标也不应该移动得更远。

如果我对您的问题的理解是正确的,您应该处理WM_NCLBUTTONDOWN消息来计算鼠标移动的限制。然后,您可以使用Cursor.Clip来应用这些限制。

但是,如果您在 WM_NCLBUTTONDOWN 消息中应用剪辑矩形,它将立即被删除。如果你在 WM_MOVING 消息中应用它,它会在拖动结束时自动删除。

这也是上述问题的一个解决方案:如果鼠标只能在用户拖动窗口时在计算出的矩形中移动,那么窗口本身只能在允许的区域内移动。

public const int WM_MOVING = 0x0216;
public const int WM_NCLBUTTONDOWN = 0x00A1;
public const int HT_CAPTION = 0x0002;

private Rectangle _cursorClip = Rectangle.Empty;

protected override void WndProc(ref Message m)
{
    switch (m.Msg)
    {
        case WM_NCLBUTTONDOWN:
            if (m.WParam.ToInt32() == HT_CAPTION)
            {
                Point location = Cursor.Position;
                Rectangle screenBounds = Screen.PrimaryScreen.Bounds;
                Rectangle formBounds = Bounds;

                _cursorClip = Rectangle.FromLTRB(location.X + screenBounds.Left - formBounds.Left,
                                                 location.Y + screenBounds.Top - formBounds.Top,
                                                 location.X + screenBounds.Right - formBounds.Right,
                                                 location.Y + screenBounds.Bottom - formBounds.Bottom);
            }
            break;
        case WM_MOVING:
            Cursor.Clip = _cursorClip;
            break;
    }
    base.WndProc(ref m);
}
于 2012-07-22T02:18:15.390 回答