Windows - 在c ++中读取鼠标的dpi设置

时间:2013-02-15 09:34:08

标签: c++ windows mouse dpi mousemove

有没有办法在c ++中获取当前的鼠标dpi设置?

问题是,向系统发送鼠标移动消息将导致光标位置不同,具体取决于鼠标的dpi分辨率。

修改

我找到了一个解决方案,我不需要鼠标的dpi设置。我使用SystemParametersInfo获取鼠标速度并通过以下方式计算移动距离: moveDistance.x * 5.0 / mouseSpeed。 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));

我希望这有助于某人:)

2 个答案:

答案 0 :(得分:1)

关于检索鼠标dpi的问题先前在这里被问到:How I can get the "pointer resolution" (or mouse DPI) on Windows? - 答案似乎表明这是不可能的,这是有道理的,因为它可能特定于使用中的鼠标硬件/驱动程序

就设置光标位置而言 - 如果你使用像SetCursorPos()这样的函数,并且正在使用WM_MOUSEMOVE消息,你正在使用的坐标是绝对的,而不是相对的,并且不应该t取决于鼠标的dpi。

答案 1 :(得分:0)

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));

而不是这个你可以使用它:

mouse_event(MOUSEEVENTF_MOVE, xMove , yMove , NULL, NULL);