Handmade Hero » Forums » Windows GetClientRect returning wrong size
boagz57
Jason
105 posts
#21006 Windows GetClientRect returning wrong size
3 months, 2 weeks ago Edited by Jason on May 4, 2019, 4:12 p.m.

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.
William
8 posts
#21007 Windows GetClientRect returning wrong size
3 months, 2 weeks ago Edited by William on May 4, 2019, 4:36 p.m.

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.
x13pixels
129 posts / 1 project

None

#21008 Windows GetClientRect returning wrong size
3 months, 2 weeks ago

Have you tried GetWindowRect instead?
boagz57
Jason
105 posts
#21009 Windows GetClientRect returning wrong size
3 months, 2 weeks ago

@William Ah, that was it. I wasn't specifying all the styles correctly. Thanks William!