I am working on some OpenGL interface stuff and noticed that when I startup my application the WM_SIZE message gets send with width=1260 and height=677, even though I created the window with width=1280 and height=720.
Here is how I create the window:
MainWindow.handle = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, windowClass.lpszClassName,
"Some window", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 1280, 720,
NULL, NULL, instance, NULL);
This is how I handle WM_SIZE:
case WM_SIZE: {
Camera.width = LOWORD(lParam);
Camera.height = HIWORD(lParam);
MainWindow.width = LOWORD(lParam);
MainWindow.height = HIWORD(lParam);
glViewport(0, 0, LOWORD(lParam), HIWORD(lParam));
} break;
I suspect that those values (1260, 677) are wrong and not the actual window dimensions, since my interface ‘hitboxes’ are slightly off. If I resize the window manually, no matter how much, the correct WM_SIZE is send and the hitboxes are fine.
My question is why is this first WM_SIZE even send and why are those values slightly off? Can I maybe send an extra manual WM_SIZE to combat the initial WM_SIZE?
>Solution :
Per the WM_SIZE documentation:
lParam
The low-order word of lParam specifies the new width of the client area.
The high-order word of lParam specifies the new height of the client area.
You are expecting the values to give you the dimensions of the entire window, but it gives you the dimensions of only the client area.
Use GetWindowRect() to get the position and dimensions of the entire window.
Otherwise, if you want the client area to be a particular size, use AdjustWindowRect() or AdjustWindowRectEx() to calculate the necessary window size before calling CreateWindowEx(), eg:
RECT r;
r.left = 0;
r.top = 0;
r.right = 1280;
r.bottom = 720;
AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW, FALSE, WS_EX_OVERLAPPEDWINDOW);
MainWindow.handle = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, windowClass.lpszClassName,
"Some window", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, r.right-r.left, r.bottom-r.top,
NULL, NULL, instance, NULL);
