mouse_event函数会将光标发送到略微偏离的坐标(偏移1-20像素)。它偏移的程度基于一个我无法完全理解的模式。
以下是我的代码:
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';
}
如果运行此程序,鼠标会从屏幕左上角向下和向右移动,然后再返回。但是,它向下移动得越远,“mouse_event”创建的鼠标移动就越不正确。
我先将其移动,然后记录当前坐标,然后计算差异。随着我向下滚动屏幕,差异(运动中的误差)会增加。我甚至尝试添加一个额外的检查,测试坐标是否偏离,然后尝试将鼠标移动到正确的位置,但它并没有起作用。
您有任何想法为什么会这样吗?以下是此程序输出的日志以供参考。
很明显,在第一个循环中(移动鼠标向下和向右),误差增加,然后在第二个循环中(将其向上和向左移动)以相同的方式减少误差。
您有任何想法为什么会发生这种情况吗? 它也会在更复杂的实现中以我无法量化且不同于此的方式发生,因此我认为它必须是在mouse_event函数本身或一些我不理解的功能中。
提前感谢任何帮助。
65536/1920
来计算x和y呢?难道你不应该将x和y设置为i的值吗? - joshuahealy