I am trying to capture the coordinates of the cursor in a Windows application. While the coordinate for the position (0, 0) is displayed correctly in the upper left corner, the coordinates become more and more distorted the further I move away from it, so that instead of (600, 600) only (583, 560) is displayed in the lower right corner. Here is the code I’m using
#include <windows.h>
#include <stdio.h>
LRESULT MainWindowProcedure(HWND Window, UINT Message, WPARAM WParam, LPARAM LParam)
{
switch (Message)
{
case WM_MOUSEMOVE:
{
POINT Cursor;
GetCursorPos(&Cursor);
ScreenToClient(Window, &Cursor);
char Buffer[256];
snprintf(Buffer, sizeof(Buffer), "X: %d, Y: %d n", Cursor.x, Cursor.y);
OutputDebugStringA(Buffer);
} break;
case WM_DESTROY:
{
PostQuitMessage(0);
} break;
default:
{
return DefWindowProc(Window, Message, WParam, LParam);
} break;
}
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEXA WindowClass = {0};
WindowClass.cbSize = sizeof(WNDCLASSEXA);
WindowClass.style = CS_HREDRAW | CS_VREDRAW;
WindowClass.lpfnWndProc = MainWindowProcedure;
WindowClass.hInstance = hInstance;
WindowClass.lpszClassName = "WindowClass";
if (RegisterClassExA(&WindowClass) != 0)
{
HWND Window = CreateWindowExA(0, "WindowClass", "Window", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, 600, 600, 0, 0, hInstance, 0);
if (Window != 0)
{
int Running = 1;
while (Running) {
MSG Message;
while (PeekMessageA(&Message, 0, 0, 0, PM_REMOVE))
{
if (Message.message == WM_QUIT)
{
Running = false;
break;
}
TranslateMessage(&Message);
DispatchMessageA(&Message);
}
}
}
}
}
I also tried GET_X_LPARAM and GET_Y_LPARAM from the Windowsx.h. The exact same problem. Does somebody have a clue, what the problem could be?
user25234780 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.