2013-02-15 53 views
1

有沒有辦法在C++中獲得當前的鼠標dpi設置?Windows - 在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)); 

我希望這可以幫助別人:)

+0

您是否嘗試過使用Windows管理工具?在這裏試試這個:[wikipedia](http://en.wikipedia.org/wiki/Windows_Management_Instrumentation) – 2013-02-15 10:35:56

+0

非常感謝bash.d我會通讀。 :) – Flo 2013-02-15 12:23:29

回答

1

有關檢索鼠標的DPI有人問以前這裏:How I can get the "pointer resolution" (or mouse DPI) on Windows? - 似乎表明,它是不可能的答案,這是有道理的,因爲它會可能是特定於正在使用的鼠標硬件/驅動程序。

至於設置光標位置去,但 - 如果你使用的功能像SetCursorPos(),並與WM_MOUSEMOVE消息的工作,你正在使用的座標是絕對的,而不是相對的,不應該依賴於的新聞部鼠標。

+0

嗨邁克爾,謝謝你的回答我也讀過以前問過的問題,這是2011年問的問題,所以我希望現在可以有一個解決方案。 :)我不能使用SetCursorPos(),我必須使用SendInput和相對鼠標移動。 – Flo 2013-02-15 12:22:50

+0

你能提供更多關於你想要做什麼的信息嗎?即使使用'SendInput'的相對鼠標移動,我也看不出這會受到鼠標硬件dpi的影響。 – Michael 2013-02-15 12:47:05

+0

在第三人稱射擊遊戲中,我想將相對鼠標移動到當前注視位置(注視位置=你在看的屏幕上的位置,這些信息來自我的眼睛記者)。此鼠標移動受鼠標dpi設置的影響。我可以通過SystemParametersInfo獲得鼠標當前速度的定義,但我無法弄清楚如何獲得鼠標的dpi設置... – Flo 2013-02-15 12:59:18

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