简体   繁体   English

为什么用Cursor.Position不能将鼠标光标移动到我告诉它的位置?

[英]Why won't my mouse cursor move where I tell it to go with Cursor.Position?

I'm doing some manipulation of the mouse cursor regarding clipping areas, and to this end I need to show a "fake" cursor on the screen. 我正在对有关裁剪区域的鼠标光标进行一些操作,为此,我需要在屏幕上显示一个“假”光标。 My real cursor will eventually be hidden, and just slightly off from the user's fake cursor to give me a buffer to preform clipping operations. 我的真实光标最终将被隐藏,并且与用户的假光标稍有不同,这为我提供了执行剪裁操作的缓冲区。 But that's not really important. 但这并不是很重要。

This is so weird. 太奇怪了 It seems like the program is blatantly ignoring my commands. 似乎该程序公然忽略了我的命令。 I have some debug code: 我有一些调试代码:

Debug.WriteLine("1fake: " + fakeMouse.X + " " + fakeMouse.Y);
Debug.WriteLine("1real: " + this.PointToClient(Cursor.Position).X + " " + this.PointToClient(Cursor.Position).Y);

int fmx = fakeMouse.X;
int fmy = fakeMouse.Y;

Cursor.Position = new Point(fmx, fmy);

Debug.WriteLine("2fake: " + fmx + " " + fmy);
Debug.WriteLine("2real: " + this.PointToClient(Cursor.Position).X + " " + this.PointToClient(Cursor.Position).Y);

And this results in debugger output like this: 这将导致调试器输出如下:

1fake: 489 497
1real: 490 500
2fake: 489 497
2real: 274 264 // I just set this to be EXACTLY The same as the value above it!?!

The cursor jumps way out of the way, into a completely different part of the screen. 光标会跳开,进入屏幕的完全不同的部分。 I did the fmx, fmy things to just reduce the problem to pure integer coordinates but it's still not taking the right parameters. 我做了fmx和fmy的事情,只是将问题简化为纯整数坐标,但仍然没有采用正确的参数。 Is it somehow being changed again somewhere else? 是否以某种方式在其他地方再次更改? I don't understand. 我不明白

Cursor.Position expects a point in screen coordinates. Cursor.Position期望屏幕坐标中的一个点。 If your point is in window or client coordinates it will be offset from where you expect. 如果您的点在窗口或客户坐标中,则它将与您期望的位置偏移。

You probably just need to call PointToScreen . 您可能只需要调用PointToScreen Something like: 就像是:

Cursor.Position = this.PointToScreen(new Point(fakeMouse.X, fakeMouse.Y));

http://msdn.microsoft.com/en-us/library/ms229598.aspx http://msdn.microsoft.com/en-us/library/ms229598.aspx

This is because you are using PointToClient before writing the output. 这是因为您在写入输出之前正在使用PointToClient。 The cursor position is relative to the screen and not to your form 光标位置是相对于屏幕而不是窗体的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM