Handmade Hero»Forums
Jason
235 posts
Windows GetClientRect returning wrong size
Edited by Jason on
For my C++ program I'm trying to query my window's size through WINAPI's GetClientRect() function. However, it's returning a window that is slightly smaller than I originally setup. Here is the window setup stuff:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
//Creation of window
        WNDCLASS WindowProperties {};
        WindowProperties.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW; //TODO: Check if OWNDC/HREDRAW/VEDRAW matter
        WindowProperties.lpfnWndProc = Win32::ProgramWindowCallback;
        WindowProperties.hInstance = CurrentProgramInstance;
        WindowProperties.lpszClassName = "MemoWindowClass";
    
        if (RegisterClass(&WindowProperties))
        {
            HWND window= CreateWindowEx(0, WindowProperties.lpszClassName, "My Program", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                CW_USEDEFAULT, CW_USEDEFAULT, globalWindowWidth, globalWindowHeight, 0, 0, CurrentProgramInstance, 0);

        //more code


globalWindowWidth/Height are both set to 1280 and 720 respectively. Later on, in the WM_PAINT event, I get the window dimensions through this function which ultimately calls GetClientRect():

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
Window_Dimension GetWindowDimension(HWND window)
    {
        Window_Dimension Result;
    
        RECT ClientRect;
        GetClientRect(window, &ClientRect);
        Result.width = ClientRect.right - ClientRect.left;
        Result.height = ClientRect.bottom - ClientRect.top;

        return(Result);
    };


When called, GetClientRect returns a 'right' value of 1264 and 'bottom' value of 681, despite me wanting values of 1280 and 720. Why is this occurring?

EDIT:

After some research I found that this might have something to do with the CreateWindowEx call and the fact that is calculates total window size (includes window borders, title bar, etc.). So when I grab a client rect, it subtracts the window title bar and borders from the final pixel values. I find a function 'AdjustWindowRectEx' for which I'm trying to use:

1
2
3
 DWORD windowStyles = WS_VISIBLE;
        RECT rect = { 0, 0, (LONG)globalWindowWidth, (LONG)globalWindowHeight };
        BOOL success = AdjustWindowRectEx(&rect, windowStyles, false, 0);


However, my rect doesn't seem to be adding in additional border and title bar pixel values so I'm still stuck.
18 posts
Windows GetClientRect returning wrong size
Edited by William on
I think you just need to specify the rest of the styles you are using to create the window for AdjustWindowRectEx.

WS_OVERLAPPEDWINDOW includes
1
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX
, but the documentation says not to specify WS_OVERLAPPED.

Alternatively, I think you could use GetWindowRect.
309 posts / 1 project
None
Windows GetClientRect returning wrong size
Have you tried GetWindowRect instead?
Jason
235 posts
Windows GetClientRect returning wrong size
@William Ah, that was it. I wasn't specifying all the styles correctly. Thanks William!