“mouse_event”函数 – 将光标发送到(稍微)错误的坐标

mouse_event函数将光标发送到略微错误的坐标(1-20像素closures)。 “关”的程度是基于我无法想象的模式。

这是我的代码

int x, y; int repeats = 1000; int start = 0; POINT pt; for(int i=0; i<repeats; i+=10) //first loop, down right { x = (65536 / 1920) * i - 1; //convert to absolute coordinates y = (65536 / 1080) * i - 1; mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, x, y, 0, 0); //move GetCursorPos(&pt); //get cursor position if(pt.x != i){mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, x, y, 0, 0);} //check if the position is wrong, and if so fix it. if(pt.y != i){mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, x, y, 0, 0);} cout << "Try: " << i << ", " << i << "\tReal: " << pt.x << ", " << pt.y << "\tDiff: " << pt.x - i << ", " << pt.y - i << '\n'; } for(int i=repeats; i>0; i-=10) //second loop, up left { x = (65536 / 1920) * i - 1; y = (65536 / 1080) * i - 1; mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, x, y, 0, 0); GetCursorPos(&pt); if(pt.x != i){mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, x, y, 0, 0);} if(pt.y != i){mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, x, y, 0, 0);} cout << "Try: " << i << ", " << i << "\tReal: " << pt.x << ", " << pt.y << "\tDiff: " << pt.x - i << ", " << pt.y - i << '\n'; } 

如果运行,则鼠标从屏幕的左上angular向下移动,然后再次返回。 但是越往下,“mouse_event”创build的鼠标移动结果越不正确。

我移动它,然后logging当前的坐标,然后计算差异。 差异(运动中的错误)会增加屏幕的下方。 我甚至试图添加一个额外的检查,testing如果坐标是closures的,然后尝试再次将鼠标移动到正确的位置,但它不工作

任何想法,为什么这可能是?

这个程序的输出logging是为了方便。

Output_Log.txt

它清楚地表明,在第一个循环(将鼠标向下和向右移动)时,错误增加,然后在第二个循环(将其移回并再次离开)时,错误以相同的方式减小。

任何想法,为什么这可能会发生? 它发生在更复杂的实现,以及我无法量化的方式,而不是这个,所以我认为它必须在mouse_event函数本身或某些我不明白的function

在此先感谢您的帮助

我会说这是由于使用整数算术来计算像素,试试这个:

 x = (int)(65536.0 / 1920 * i - 1); //convert to absolute coordinates y = (int)(65536.0 / 1080 * i - 1);