如何防止其他鼠标按钮中断MouseMove?

发布于 2024-12-01 12:03:49 字数 410 浏览 0 评论 0原文

如果您将一个面板放置在一个新的 C# 项目中并捕获它的 MouseMove 事件,如下所示:

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button != MouseButtons.Left)
        return;

    Console.WriteLine("e.X: {0}, e.Y: {1}", e.X, e.Y);
}

它会告诉您按住鼠标左键时鼠标的客户端坐标,即使光标移到面板之外也是如此。

但是,如果您在容器中按住鼠标左键,然后在按住鼠标左键的同时单击鼠标上的任何其他鼠标按钮,则在容器边界之外时,它不再调用 MouseMove。

有什么办法可以改变这个吗?感谢您的阅读。

If you place a panel in a new C# project and capture it's MouseMove event like this:

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button != MouseButtons.Left)
        return;

    Console.WriteLine("e.X: {0}, e.Y: {1}", e.X, e.Y);
}

It tells you the client coordinates of the mouse while the left mouse button is held down, even if the cursor goes outside of the panel.

However, if you are holding down left mouse button in the container and then, while holding down left mouse button, click any other mouse button on your mouse, it no longer calls MouseMove while outside the bounds of the container.

Is there any way to change this? Thanks for reading.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

再见回来 2024-12-08 12:03:49

1:如果您试图让它在左按钮按下时工作,请尝试以下操作:

bool mouseDown = false;

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
    if (!mouseDown)
        return;

    Console.WriteLine("e.X: {0}, e.Y: {1}", e.X, e.Y);
}

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
        mouseDown = true;
}

private void panel1_MouseUp(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
        mouseDown = false;
}

2:否则,如果您希望它在任意鼠标按钮按下时工作,请尝试以下操作:

int mouseDown = 0;

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
    if (mouseDown == 0)
        return;

    Console.WriteLine("e.X: {0}, e.Y: {1}", e.X, e.Y);
}

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
    mouseDown++;
}

private void panel1_MouseUp(object sender, MouseEventArgs e)
{
    mouseDown--;
}

1: If you are trying to get it to work only when the left button is down, try the following:

bool mouseDown = false;

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
    if (!mouseDown)
        return;

    Console.WriteLine("e.X: {0}, e.Y: {1}", e.X, e.Y);
}

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
        mouseDown = true;
}

private void panel1_MouseUp(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
        mouseDown = false;
}

2: Otherwise, if you want it to work when any mouse button is down, try the following:

int mouseDown = 0;

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
    if (mouseDown == 0)
        return;

    Console.WriteLine("e.X: {0}, e.Y: {1}", e.X, e.Y);
}

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
    mouseDown++;
}

private void panel1_MouseUp(object sender, MouseEventArgs e)
{
    mouseDown--;
}
夜清冷一曲。 2024-12-08 12:03:49

除了贾斯汀的解决方案之外,

我认为这是因为,如果在拖动过程中您走出面板,右键单击面板外面,则会强制面板失去焦点,因此控件不再成为一个活跃的一位。

如果您将鼠标拖动到面板内并在面板内单击鼠标右键,则会发生类似的情况。

我刚刚使用 Spy++ windows 资源管理器捕获并进行了测试,因此用 LButton 向下移动鼠标,并在某个时刻不释放它进行了右键单击。这是结果:

我用箭头签署了用右键单击的行,其中

发送到失去鼠标捕获的窗口。

查看下一行带箭头的内容。下一个窗口的句柄是0,所以没有任何窗口。所以这意味着,就像一个简单的命令:您丢失了鼠标捕获。

希望这有帮助。

本地测试截图

In addition to Justin's solution will say that:

I think it's because, if during drag you go out of the panel right click out of the panel, forces panel to lose the focus, so control no more becomes an active one.

In case of when you're dragging mouse inside panel and click with right click inside panel, happens something like this.

I just captured with Spy++ windows explorer and did a test, so moved the mouse with LButton down and at some point without releasing it made a right click. And here is a result:

With arrows I sign the row where I clicked with right button, where WM_CAPTURECHANGED
message sent. This message according to documentation is:

Sent to the window that is losing the mouse capture.

Look on next line with arrow. The handle of the next window is 0, so there is no any window. So this means, like a simple command: You lost a capture on mouse.

Hope this helps.

Screenshot of local test

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文