问题
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());
}
}
回答1:
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.
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;
Use an empty rectangle to release the mouse:
Cursor.Clip = new Rectangle(0, 0, 0, 0);
回答2:
Are you sure that the mouse covers all the coordinates that lie on the journey from A to B?
The mouse input from the OS is sampled, discreet data. This means that the mouse pointer can skip coordinates, just as you are seeing.
Consider interpolation over your data if it isn't hi-res enough.
IIRC, you can increase the sample-rate of the mouse. Can't remember how though. I'm sure the web can though.
回答3:
Iv been asked this several times over the years. You generally cant tell mouse event listeners to tell you every single pixel the mouse passes though. Even the OS may skip large areas of the screen if you are moving the mouse fast enough. The only reliable way would be to plot the points yourself from the history of mouse positions you are given. The simple method would be to draw a line between the current point and the last one. More complex solutions would involve storing the last few coordinates and using sine / tangent math to plot a smooth curve.
来源:https://stackoverflow.com/questions/13105366/how-to-precisely-track-the-mouse-cordinates-without-skips