Outside of the main
, I define a mouse event handler as
int xPosAbsolute = 0;
int yPosAbsolute = 0;
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch (msg)
{
case WM_INPUT:
{
UINT dwSize = sizeof(RAWINPUT);
static BYTE lpb[sizeof(RAWINPUT)];
GetRawInputData((HRAWINPUT)lparam, RID_INPUT, lpb, &dwSize, sizeof(RAWINPUTHEADER));
RAWINPUT* raw = (RAWINPUT*)lpb;
if (raw->header.dwType == RIM_TYPEMOUSE)
{
xPosAbsolute += raw->data.mouse.lLastX;
yPosAbsolute += raw->data.mouse.lLastY;
}
return 0;
}
}
return DefWindowProc(hwnd, msg, wparam, lparam);
}
inside of the main
I then register and bind the mouse readings as
POINT p;
if (GetCursorPos(&p))
{
xPosAbsolute = p.x;
yPosAbsolute = p.y;
}
RAWINPUTDEVICE Rid[1];
Rid[0].usUsagePage = HID_USAGE_PAGE_GENERIC;
Rid[0].usUsage = HID_USAGE_GENERIC_MOUSE;
Rid[0].dwFlags = RIDEV_INPUTSINK;
Rid[0].hwndTarget = hWnd;
RegisterRawInputDevices(Rid, 1, sizeof(Rid[0]));
Unfortunately, even if I move the mouse, the variables xPosAbsolute
and yPosAbsolute
never change their values from the initial GetCursorPos
setting. Am I missing something in order to make the WndProc
work?
EDIT:
Borrowing some of the ingredients from this link, I've included the following in the main
HMODULE hInstance = GetModuleHandle(NULL);
static TCHAR szWindowClass[] = _T("win32app");
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance,
MAKEINTRESOURCE(IDI_APPLICATION));
RegisterClassEx(&wcex);
to attempt to register WndProc
with the window. Unfortunately, this did not give any improvement.