2

有没有办法在 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));

我希望这可以帮助别人 :)

4

2 回答 2

1

关于检索鼠标 dpi 的问题之前在这里被问过:如何在 Windows 上获得“指针分辨率”(或鼠标 DPI)?- 那里的答案似乎表明这是不可能的,这是有道理的,因为它可能特定于正在使用的鼠标硬件/驱动程序。

就设置光标位置而言 - 如果您使用类似的函数SetCursorPos(),并且正在处理WM_MOUSEMOVE消息,那么您正在使用的坐标是绝对的,而不是相对的,并且根本不应该依赖于鼠标的 dpi。

于 2013-02-15T11:59:45.673 回答
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);
于 2016-02-22T15:17:23.437 回答