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

标签 c++ windows mouse dpi mousemove

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

相关文章:

c++ - 仅在使用间接时调用虚函数——经典的早期绑定(bind)问题?

c++ - 使用两个 vector ,一个带有 x、y、z 等变量,一个带有 1、2、3 等值,来构建像 x=1、y=2、z=3 这样的等式?

c++ - 如何将消息从 DLL 传递到应用程序

java - 将 Eclipse 安装移动到用户配置文件之外的文件夹?

python - 在 Linux 上获取鼠标位置,纯 Python

c# - 鼠标移动 : Click the sprite "walks" to point clicked

c++ - 是否有用于 gtest 的开源 VS2012 测试适配器?

c++ - strVar.at(index) 与 strVar[index]

windows - "Calculator bug"是代码问题吗?

c++ - 如何在 linux 中读取低级鼠标点击位置。