如何在不跳过的情况下精确跟踪鼠标坐标 [英] How to precisely track the mouse cordinates without skips

查看:62
本文介绍了如何在不跳过的情况下精确跟踪鼠标坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种比这段代码更精确地跟踪鼠标坐标的方法.如果您运行此代码并快速移动鼠标或快速更改方向,则坐标可能如下所示:50 然后是 40.似乎当鼠标快速移动时,它不会跟踪指针覆盖的所有点,就像这样例如,它跳过了 10 个坐标.如果我慢慢移动它,没有问题.我需要一种方法来跟踪所有指针坐标而不会跳过.我已经在使用全局钩子的 Code Project 上尝试过示例,结果相同.我怎样才能做到这一点?是否可以进行注册表更改以强制窗口跟踪所有坐标.是否可以?我更喜欢用 C# 来做,但也会考虑其他方式.谢谢.

I need a way to track the mouse coordinates more precisely than this code. If you run this code and move your mouse really fast or change directions fast, the coordinates might look this: 50 and then 40. It seems that when the mouse moves fast it doesn't track all the points the pointer covers, like in this example there are 10 coordinates that it skips. If I move it slowly, there's no problem. I need a way to track all the pointer coordinates with no skips. I've tried the sample on Code Project that uses global hooks, with the same result. How can I do this? Is there a registry change that can be made that forces windows to track all the coordinates. Is it possible? I would prefer to do it with C# but will consider other ways too. Thanks.

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    if (num != 1)
    {
        listBox1.Items.Add(e.X.ToString());

    }
}

推荐答案

鼠标指针不会在每个像素上移动,如果快速移动鼠标,它会在事件之间移动一大堆像素.硬件不会为鼠标移动的每个像素发送信号,而是报告自上次报告以来鼠标移动的距离.

The mouse pointer doesn't move across every pixel, if you move the mouse fast, it will move a whole bunch of pixels between events. The hardware simply doesn't send a signal for each pixel that the mouse moves, it reports the distance that the mouse has moved since the last report.

不要试图跟踪鼠标的位置,而是使用 Cursor.Clip 属性来限制鼠标的移动:

Instead of trying to track where the mouse is, use the Cursor.Clip property to limit the movement of the mouse:

var rect = someControl.RectangleToScreen(new Rectangle(Point.Empty, someControl.ClientSize));
Cursor.Position = new Point(rect.Left + rect.Width / 2, rect.Top + rect.Height / 2);
Cursor.Clip = rect;

使用空矩形释放鼠标:

Cursor.Clip = new Rectangle(0, 0, 0, 0);

这篇关于如何在不跳过的情况下精确跟踪鼠标坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆