Windows-在C ++中读取鼠标的dpi设置 [英] Windows - read dpi setting of mouse in c++
问题描述
有没有办法在c ++中获取当前的鼠标dpi设置?
Is there a way to get the current mouse dpi setting in c++?
问题在于,将鼠标移动消息发送到系统会导致不同的光标位置,具体取决于鼠标的dpi分辨率.
The problem is that sending a mouse move message to the system will result in a different cursor position depending on the dpi resolution of the mouse.
我找到了不需要鼠标设置dpi的解决方案.我使用SystemParametersInfo获得鼠标速度,并通过以下方式计算移动距离:moveDistance.x * 5.0/mouseSpeed.5.0/mouseSpeed是保证移动距离永远正确的魔幻数字.
I found a solution where I don't need the dpi setting from the mouse. I get the mouse speed with SystemParametersInfo and calculate the move distance by: moveDistance.x * 5.0 / mouseSpeed. The 5.0 / mouseSpeed is magic number which garantees the move distance will always be correct.
// 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));
我希望这对某人有帮助:)
I hope this helps someone :)
推荐答案
先前在此处提出了有关获取鼠标dpi的问题:-答案似乎暗示这是不可能的,这很有意义,因为它可能特定于所使用的鼠标硬件/驱动程序.
The question about retrieving mouse dpi was asked previously here: How I can get the "pointer resolution" (or mouse DPI) on Windows? - the answer there seems to suggest that it isn't possible, which makes sense as it would likely be specific to the mouse hardware/driver in use.
就设置光标位置而言,如果使用 WM_MOUSEMOVE
消息是您正在使用的坐标是绝对坐标,不是相对坐标,并且完全不应该依赖于鼠标的dpi.
As far as setting a cursor position goes though - if you use a function like SetCursorPos()
, and are working with WM_MOUSEMOVE
messages the coordinates you are working with are absolute, not relative, and shouldn't depend on the dpi of the mouse at all.
这篇关于Windows-在C ++中读取鼠标的dpi设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!