有没有办法在 C++ 中获取当前鼠标 dpi 设置?
问题是向系统发送鼠标移动消息会导致不同的光标位置,具体取决于鼠标的 dpi 分辨率。
编辑:
我找到了一个不需要鼠标 dpi 设置的解决方案。我使用 SystemParametersInfo 获取鼠标速度并通过以下方式计算移动距离: moveDistance.x * 5.0/鼠标速度。 5.0/mouseSpeed 是保证移动距离始终正确的神奇数字。
// get mouse speed
int mouseSpeed;
mouseSpeed = 0;
SystemParametersInfo(SPI_GETMOUSESPEED, 0, &mouseSpeed, 0);
// calculate distance to gaze position
POINT moveDistance;
moveDistance.x = m_lastEyeX - m_centerOfScreen.x;
moveDistance.y = m_lastEyeY - m_centerOfScreen.y;
// 5.0 / mouseSpeed -> magic numbers, this will halve the movedistance if mouseSpeed = 10, which is the default setting
// no need to get the dpi of the mouse, but all mouse acceleration has to be turned off
double xMove = moveDistance.x * 5.0 / static_cast<double>(mouseSpeed);
double yMove = moveDistance.y * 5.0 / static_cast<double>(mouseSpeed);
INPUT mouse;
memset(&mouse, 0, sizeof(INPUT));
mouse.type = INPUT_MOUSE;
// flag for the mouse hook to tell that it's a synthetic event.
mouse.mi.dwExtraInfo = 0x200;
mouse->mi.dx = static_cast<int>(xMove);
mouse->mi.dy = static_cast<int>(yMove);
mouse->mi.dwFlags = mouse->mi.dwFlags | MOUSEEVENTF_MOVE;
SendInput(1, &mouse, sizeof(mouse));
我希望这对某人有帮助:)
最佳答案
关于检索鼠标 dpi 的问题之前在这里被问过:How I can get the "pointer resolution" (or mouse DPI) on Windows? - 那里的答案似乎表明这是不可能的,这是有道理的,因为它可能特定于正在使用的鼠标硬件/驱动程序。
就设置光标位置而言 - 如果您使用像 SetCursorPos()
这样的函数,并且正在处理 WM_MOUSEMOVE
消息,那么您正在使用的坐标是绝对的,而不是相对的,并且不应该依赖于鼠标的 dpi完全没有。
关于c++ - Windows - 在 C++ 中读取鼠标的 dpi 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14891709/