1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | RECT rect; rect.left = 0; rect.right = 1280; rect.top = 0; rect.bottom = 720; AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW|WS_VISIBLE, 0); // The function return true HWND window = CreateWindowEx( 0, windowClass.lpszClassName, "Title", WS_OVERLAPPEDWINDOW|WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, rect.right, rect.bottom, 0, 0, NULL, 0); // Some place else RECT clientRect; GetClientRect(window, &clientRect); // (1272, 689) |
1 2 3 4 5 6 7 8 9 10 11 12 13 | HWND window = CreateWindowEx( 0, windowClass.lpszClassName, "Title", WS_OVERLAPPEDWINDOW|WS_VISIBLE, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, 0, 0, NULL, 0); |
1 | rect.right, rect.bottom |
1 | rect.right - rect.left, rect.bottom - rect.up |
Sometimes I want the window to have a specific width and a default height, or vise versa. But CreateWindowEx wouldn't work if only the height argument is CW_USEDEFAULT. It needs both the height and width to be CW_USEDEFAULT.
I don't think there is way to do that on creation. Best you can do is to create window with both width/height USEDEFAULT and then resize it to actual size you need.
How can I resize a window?
For some reason, the AdjustWindowRect returns a bigger rect than I want. Here's an image of 2 windows. Both of them were created with the same size and position, then I set the inner rect of window 3 to the outer rect size.
GetWindowRect(window3, &rect); // The size of rect matches the initial size I pass into CreateWindow if (AdjustWindowRect(&rect, style, 0)) SetWindowPos(window2, HWND_TOP, r.left, r.top, r.right - r.left, r.bottom - r.top, 0));
From the GetWindowRect doc
In Windows Vista and later, the Window Rect now includes the area occupied by the drop shadow.
Calling GetWindowRect will have different behavior depending on whether the window has ever been shown or not. If the window has not been shown before, GetWindowRect will not include the area of the drop shadow.
To get the window bounds excluding the drop shadow, use DwmGetWindowAttribute, specifying DWMWA_EXTENDED_FRAME_BOUNDS. Note that unlike the Window Rect, the DWM Extended Frame Bounds are not adjusted for DPI. Getting the extended frame bounds can only be done after the window has been shown at least once.