Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ &引用;鼠标“事件”;函数-将光标发送到(稍微)错误的坐标_C++_Windows_Mouse_Coordinates_Pixel - Fatal编程技术网

C++ &引用;鼠标“事件”;函数-将光标发送到(稍微)错误的坐标

C++ &引用;鼠标“事件”;函数-将光标发送到(稍微)错误的坐标,c++,windows,mouse,coordinates,pixel,C++,Windows,Mouse,Coordinates,Pixel,鼠标事件函数将光标发送到稍微错误的坐标(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 = (6

鼠标事件函数将光标发送到稍微错误的坐标(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';
}
intx,y;
int重复次数=1000;
int start=0;
点pt;

对于(int i=0;i我认为这是由于使用整数算术来计算像素,请尝试以下方法:

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

你为什么要做
65536/1920
的事情来计算x和y?你不应该把x和y设置为i的值吗?不,因为我使用的是MOUSEEVENTF_绝对标记,它要求x和y的绝对坐标都在0-65535之间。这是消除操作系统加速度被添加到运动中的唯一方法,这会导致甚至更多的错误在这种情况下,你应该做浮点运算,并将其转换为一个运行良好的intWow,我非常感激你。非常感谢,这显然是某种舍入错误,现在我必须稍微调整一下这种安排。它目前正好给我(-1,-1)每次尝试时都会出现错误,所以我确信修复这一错误很简单,这不是舍入错误,而是整数算术的工作方式。例如,使用
int
s,3/2=1,而如果使用
float
s,3/2=1.5。将
.0
添加到其中一个数字,编译器将使用浮点算术。