0

背景:

在我使用 Windows API 进行开发的大部分时间里,我一直在使用WM_MOUSEMOVEWM_KEYDOWN/消息分别用于鼠标和键盘输入。WM_KEYUP但是,我最近开始实现一个输入类,它使用RAWINPUTWM_INPUT消息来处理输入。

多亏了这个网站,键盘实现完美无缺

但是,我现在将注意力转向实现鼠标移动。据我了解,WM_INPUT在处理鼠标时会生成鼠标移动增量,而不是客户端空间坐标。

问题是我还想在类中存储鼠标指针的客户端和屏幕空间坐标,理想情况下,我希望这样做而不需要捕获WM_MOUSEMOVE消息或在可能的情况下使用GetCursorPos()/ScreenToClient()函数,因为我的 (可能没有根据)对性能问题的恐惧。

我的研究把我带到RAWINPUT这里,它说明了鼠标的细节:

请注意,返回的值不是您从 WM_MOUSEMOVE 消息中获得的屏幕空间值。这是我们获得的原始数据,因此需要解释这些值。

这使我相信可以通过使用初始调用计算鼠标光标的初始位置来解释数据,GetCursorPos()然后对于消息检测到的每个鼠标移动事件WM_INPUT,将从消息中检索到的增量添加WM_INPUT到初始光标位置,采取措施确保该位置保持在屏幕边界内。例如像这样的东西(注意这不会编译,因为它包含我的课程的摘录,但它应该足以概述我目前正在做的事情)

// Somewhere in class header

RECT m_screenMouseBounds;
POINT m_mouseDelta;
POINT m_mouseScreenPosition;
POINT m_mouseScreenPositionGETCURSOR;
RAWMOUSE m_rawMouse;

// ... 

// Somewhere during class initialisation

GetCursorPos(&m_mouseScreenPosition); // Get initial mouse cursor position

// Determine maximum and minimum boundaires of current display
m_screenMouseBounds.right = GetSystemMetrics(SM_CXSCREEN);
m_screenMouseBounds.bottom = GetSystemMetrics(SM_CYSCREEN);
m_screenMouseBounds.top = 0;
m_screenMouseBounds.left = 0;

// Prepare the devices for registering
RAWINPUTDEVICE theInputDevices[2]; 

// 0 = keyboard
theInputDevices[0].usUsagePage = 0x01;
theInputDevices[0].usUsage = 0x06;
theInputDevices[0].dwFlags = RIDEV_NOLEGACY; 
theInputDevices[0].hwndTarget = theWindowToHandle;

// 1 = mouse
theInputDevices[1].usUsagePage = 0x01;
theInputDevices[1].usUsage = 0x02;
theInputDevices[1].dwFlags = 0; // RIDEV_NOLEGACY 
theInputDevices[1].hwndTarget = theWindowToHandle;

// Register each device with raw input
if(RegisterRawInputDevices(theInputDevices,2,sizeof(RAWINPUTDEVICE))==FALSE)
{
    // throw exception
    return false;
}

// ... 

// In the WM_INPUT processing function with parameters : (WPARAM wParam, LPARAM lParam)

char inputBuffer[sizeof(RAWINPUT)] = {};
UINT inputBufferSize = sizeof(RAWINPUT);
GetRawInputData(reinterpret_cast<HRAWINPUT>(lParam), RID_INPUT, inputBuffer, &inputBufferSize, sizeof(RAWINPUTHEADER));
RAWINPUT* rawData = reinterpret_cast<RAWINPUT*>(inputBuffer);

if(rawData->header.dwType == RIM_TYPEMOUSE)
{
    m_rawMouse = rawData->data.mouse;

    // Add the movement deltas acquired from the WM_INPUT message
    m_mouseDelta.x = m_rawMouse.lLastX;
    m_mouseDelta.y = m_rawMouse.lLastY;

    // Update the screen position using the delta values (Does not work as expected!)
    m_mouseScreenPosition.x += m_mouseDelta.x;
    m_mouseScreenPosition.y += m_mouseDelta.y;

    // Ensure mouse coords are within the screens boundaries
    if (m_mouseScreenPosition.x < m_screenMouseBounds.left)
    {
        m_mouseScreenPosition.x = m_screenMouseBounds.left;
    }
    if (m_mouseScreenPosition.x > m_screenMouseBounds.right)
    {
        m_mouseScreenPosition.x = m_screenMouseBounds.right;
    }
    if (m_mouseScreenPosition.y < m_screenMouseBounds.top)
    {
        m_mouseScreenPosition.y = m_screenMouseBounds.top;
    }
    if (m_mouseScreenPosition.y > m_screenMouseBounds.bottom)
    {
        m_mouseScreenPosition.y = m_screenMouseBounds.bottom;
    }        

    // Double check to make sure that the screen position coordinates calculated 
    // above match the screen coordinates as determined by Windows 
    // using GetCursorPos() (Hint: They don't! :( )
    GetCursorPos(&m_mouseScreenPositionGETCURSOR);

    TCHAR s[256];
    swprintf(s, L"MouseDX: %ld MouseDY: %ld\n", m_mouseDelta.x, m_mouseDelta.y);
    OutputDebugString(s);

    swprintf(s, L"MouseX(Screen(WM_INPUT)): %ld MouseY(Screen(WM_INPUT)): %ld\n", m_mouseScreenPosition.x, m_mouseScreenPosition.y);
    OutputDebugString(s);

    swprintf(s, L"MouseX(Screen(GetCursorPos)): %ld MouseY(Screen(GetCursorPos)): %ld\n", m_mouseScreenPositionGETCURSOR.x, m_mouseScreenPositionGETCURSOR.y);
    OutputDebugString(s);
}

问题是m_mouseScreenPosition通过此方法 获取的值与m_mouseScreenPositionGETCURSOR通过 WINAPI 函数获取的值不同GetCursorPos()

总之

我想我的整体问题是:

  1. 与什么相关的值lLastX和结构?它们是否与屏幕坐标/客户端坐标有关,即返回的值与屏幕上的像素成 1:1 关系,或者它们与显示器完全无关,而是与鼠标的细节(灵敏度等)有关,因此不是 1 :1 与像素的关系?lLastYRAWMOUSE

  2. 如果 1 的答案与鼠标而不是显示坐标有关:是否可以仅使用 来确定鼠标指针的实际屏幕坐标WM_INPUT?还是我必须捕获WM_MOUSEMOVE消息和/或使用 GetCursorPos() 以确定客户端空间和屏幕空间中的光标坐标?

  3. 如果 2 的答案是我将需要使用WM_MOUSEMOVE/ :如果我最终使用/ GetCursorPos(),是否有任何性能问题我应该注意?还是我对性能的担忧是没有根据的?RAWINPUTWM_MOUSEMOVEGetCursorPos()

如果你已经做到了这一点,我会感谢你,我很感激你能给我的任何帮助或信息。谢谢!

4

1 回答 1

0
  1. “请注意,返回的值不是您从 WM_MOUSEMOVE 消息中获得的屏幕空间值。这是我们得到的原始数据,因此需要解释这些值。这些值通常与最后一个位置相关,因此表示运动。请确保您可以检查 RAWMOUSE 结构中的 usFlags。” 从玩具制造商那里得到这个,我总是觉得很棒。这里

  2. RAWINPUT 仅用于获取相对输入。这就是为什么它是生的。如果您真的不想使用 GetCursorPos,只需使用 WM_MOUSEMOVE 并检查 l/w 参数。

  3. 我实际上从未对 GetCursorPos() 进行过性能测试,但我已经为我自己的项目做了一些阅读,而且我从未见过任何反对意见。调用 GetCursorPos,无论您是否使用 RAWINPUT,每帧一次或每次更新都肯定可以忽略不计。我用它来旋转相机,它一直工作得很好。

于 2013-07-16T20:16:26.467 回答