如何使用 Mouse_Event 函数实现绝对精确的鼠标移动
如果你想要保证绝对正确的位置,可以使用 GetCursorPos 函数获取鼠标的绝对坐标,然后将其转换为相对坐标。具体做法是先获取当前窗口的句柄,然后使用 ScreenToClient 函数将鼠标的绝对坐标转换为相对于窗口的坐标。然后再使用 Mouse_Event 函数来模拟鼠标事件。
代码示例:
POINT pt;
HWND hwnd = GetForegroundWindow(); // 获取当前窗口句柄
GetCursorPos(&pt); // 获取鼠标的绝对坐标
ScreenToClient(hwnd, &pt); // 将绝对坐标转换为相对坐标
// 模拟鼠标事件
MOUSEINPUT mouseInput = { 0 };
mouseInput.dx = pt.x;
mouseInput.dy = pt.y;
mouseInput.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
mouseInput.time = 0;
mouseInput.dwExtraInfo = 0;
INPUT input = { 0 };
input.type = INPUT_MOUSE;
input.mi = mouseInput;
SendInput(1, &input, sizeof(INPUT)); // 发送鼠标事件
这样就可以保证鼠标事件的位置是绝对正确的了。
原文地址: https://www.cveoy.top/t/topic/nglY 著作权归作者所有。请勿转载和采集!