Handmade Hero » Forums » Code » Win32GetWindowDimension
svk151
mike
3 posts
#8558 Win32GetWindowDimension
1 year, 2 months ago

Question about this function from way back in the early episodes (episode 4 or 5).

As of the latest code base this is the function:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
internal win32_window_dimension
Win32GetWindowDimension(HWND Window)
{
    win32_window_dimension Result;

    RECT ClientRect;
    GetClientRect(Window, &ClientRect);
    Result.Width = ClientRect.right - ClientRect.left;
    Result.Height = ClientRect.bottom - ClientRect.top;

    return(Result);
}


However since the image is top down ClientRect.left & ClientRect.top are always 0 so why does the subtraction take place. Is there any reason that this cannot be:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
internal win32_window_dimension
Win32GetWindowDimension(HWND Window)
{
    win32_window_dimension Result;

    RECT ClientRect;
    GetClientRect(Window, &ClientRect);
    Result.Width = ClientRect.right;
    Result.Height = ClientRect.bottom;

    return(Result);
}
mmozeiko
Mārtiņš Možeiko
1503 posts
1 project
#8559 Win32GetWindowDimension
1 year, 2 months ago Edited by Mārtiņš Možeiko on Sept. 10, 2016, 8:12 p.m.

Yes, you can do that. GetClientRect always returns 0 in left and top members. It's documented in MSDN: https://msdn.microsoft.com/en-us/...ary/windows/desktop/ms633503.aspx

Typically when you deal with rectangles you are used to subtract left from right and top from bottom (or bottom from top) when you want to get width and height of rectangle. Sometimes you simply write code like this this to avoid silly mistakes when you forget to do subtraction. For example, when you use GetWindowRect function. So you simply always do this subtraction.
cmuratori
Casey Muratori
803 posts
1 project

Casey Muratori is a programmer at Molly Rocket on the game 1935 and is the host of the educational programming series Handmade Hero.

#8561 Win32GetWindowDimension
1 year, 2 months ago

Yes, I always do a subtraction by habit, since I don't really know to what extent various RECTs are guaranteed to have 0 corners. If MSDN says it's always the case, maybe it's always the case, but of course maybe not, because there's a lot of times MSDN says something but it turns out to not always be true :P

- Casey
brothir
Thomas Kingsvik
10 posts

None

#8569 Win32GetWindowDimension
1 year, 2 months ago

They're not guaranteed to be 0 when you are resizing a window. They seem to frequently be not 0 when resizing a window.

None
mmozeiko
Mārtiņš Možeiko
1503 posts
1 project
#8570 Win32GetWindowDimension
1 year, 2 months ago Edited by Mārtiņš Možeiko on Sept. 11, 2016, 7:03 p.m.

Are you sure about it? I tried resizing window in all the possible ways (left, right, top, bottom, diagonal) and never got it to put non-0 value in left or top.

The docs says that top and left will be 0, two times!
1)
Because client coordinates are relative to the upper-left corner of a window's client area, the coordinates of the upper-left corner are (0,0).
2)
The left and top members are zero.

Here's test code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <windows.h>
#include <stdio.h>

static LRESULT WINAPI wproc(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
{
    if (message == WM_SIZE)
    {
        RECT rect;
        GetClientRect(window, &rect);
        if (rect.left != 0 || rect.top != 0)
        {
            printf("left=%u, top=%u\n", rect.left, rect.top);
        }
        return 0;
    }
    else if (message == WM_DESTROY)
    {
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProcW(window, message, wparam, lparam);
}

int main()
{
    WNDCLASSW wc =
    {
        .lpfnWndProc = wproc,
        .lpszClassName = L"test",        
    };

    RegisterClassW(&wc);
    HWND window = CreateWindowW(wc.lpszClassName, L"test", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, NULL);

    BOOL ret;
    MSG msg;
    while (GetMessageW(&msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessageW(&msg);
    }
}

svk151
mike
3 posts
#8585 Win32GetWindowDimension
1 year, 2 months ago

Thanks for the replies.