[英]SendInput always moves mouse pointer to left top corner
我想以编程方式将鼠标的运动合成到屏幕上的一个点(100,100),下面的代码将其移动到左上角。 有什么事吗
#include "stdafx.h"
#include<Windows.h>
int main() {
INPUT input;
input.type = INPUT_MOUSE;
input.mi.dx = 100;
input.mi.dy = 100;
input.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
input.mi.mouseData = 0;
input.mi.dwExtraInfo = NULL;
input.mi.time = 0;
SendInput(1, &input, sizeof(INPUT));
return 0;
}
PS。 我已经在Windows 10x64的VS2017中编译了它。 我也在Win7上运行了代码
PPS。 当我删除MOUSEEVENTF_ABSOLUTE标志时,它移至相对位置。
API调用遵循已记录的行为:
MOUSEEVENTF_ABSOLUTE : dx和dy成员包含标准化的绝对坐标。 [...]请参阅以下“备注”部分。
备注部分中确实描述了标准化坐标:
如果指定了
MOUSEEVENTF_ABSOLUTE
值,则dx和dy包含0到65,535之间的归一化绝对坐标。 事件过程将这些坐标映射到显示表面上。 坐标(0,0)映射到显示表面的左上角; 坐标(65535,65535)映射到右下角。 在多监视器系统中,坐标映射到主监视器。
要将鼠标移到绝对位置,您首先需要查询显示表面的大小(例如,通过调用GetMonitorInfor ),并适当缩放坐标。
给定该点并以设备单位显示的表面尺寸作为输入,以下函数可标准化该点:
POINT normalize(POINT const& pt_in_px, RECT const& display_size_in_px)
{
POINT pt_normalized{};
auto const width_in_px{ display_size_in_px.right - display_size_in_px.left };
auto const height_in_px{ display_size_in_px.bottom - display_size_in_px.top };
pt_normalized.x = ::MulDiv(pt_in_px.x, 65536, width_in_px);
pt_normalized.y = ::MulDiv(pt_in_px.y, 65536, height_in_px);
return pt_normalized;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.