Win32GetWindowDimension

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);
}
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.

Edited by Mārtiņš Možeiko on
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
They're not guaranteed to be 0 when you are resizing a window. They seem to frequently be not 0 when resizing a window.
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);
    }
}


Edited by Mārtiņš Možeiko on
Thanks for the replies.