Public Group

# Trouble setting window size

This topic is 4724 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I want to create a window with a size based on the client area, not the size of the entire window. Pretty common, yeah :) And I've been doing this for years. But now, after a couple of months (or almost a year) away from C++ I cant seem to get it right anymore. I wrote a (quite) short program to demonstrate my problem. The problem lies in "AdjustWindowRectEx", that's for sure (the rect it gives me is not want I want at all). But I dont understand what's wrong! Feels like one's getting to old for this :) A little help would be great. GameDev.net still serves it's purpose I guess :)
#include <windows.h>
#include <iostream>
using namespace std;

int main()
{
// Settings for my window

const int width = 200;
const int height = 400;
const int style = WS_VISIBLE | WS_MINIMIZEBOX | WS_SYSMENU;
const int ex_style = WS_EX_TOPMOST;

// Register the window class

WNDCLASSEX wndcx;
wndcx.cbSize = sizeof(WNDCLASSEX);
wndcx.hInstance = GetModuleHandle(0);
wndcx.lpszClassName = "FooBar Window Class";
wndcx.style = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS | CS_OWNDC;
wndcx.lpfnWndProc = DefWindowProc;
wndcx.cbClsExtra = 0;
wndcx.cbWndExtra = 0;
wndcx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);

if (0 == RegisterClassEx(&wndcx))
cout << "Failed to register window class" << endl;

// Create the window
// Here is where the problem is. AdjustWindowRectEx wont
// give me the rect I'm looking for. Frustrating...

RECT rect = { 100, 100, width, height };
if (0 == AdjustWindowRectEx(&rect, style, 0, ex_style))
cout << "Failed to adjust window rect" << endl;

HWND hWnd = CreateWindowEx(
ex_style, "FooBar Window Class", "FooBar", style,
rect.left, rect.top, rect.right, rect.bottom,
0, 0, GetModuleHandle(0), 0);

if (0 == hWnd)
cout << "Failed to create window" << endl;

// And now, lets see if everything turned out ok...

RECT client;
RECT window;

GetClientRect(hWnd, &client);
GetWindowRect(hWnd, &window);

cout << "Window metrics: " << window.left << ", " << window.top << ", " << window.right << ", " << window.bottom << endl;
cout << "Client size: " << client.right-client.left << ", " << client.bottom-client.top << endl;
cout << "Press any key..." << endl;
cin.get();

// And no, it didn't. The sizes are all wrong!

return 0;
}
[\source]



##### Share on other sites
the client rect you pass into AdjustWindowRectEx needs to have left and top values of 0. then use the adjusted rect only for the window's width and height, e.g.,

CreateWindowEx(exStyle, "", "", style, winX, winY, rect.right-rect.left, rect.bottom-rect.top, ...);

##### Share on other sites
You shouldn't need to start the rect at (0,0). Probably your problem is that the input rect should be:

RECT rect = { 100, 100, 100 + width, 100 + height };

The code you have isn't using the real width and height because you're not adding the origin.

##### Share on other sites
Thanks for your replies, but believe me, I've already tried with both of your suggestions :) The "width + 100, height + 100" detail was of course intended to be expressed like you pointed out Anon Mike, but it's still not where the problem appears. Run the program if you'd like, it just wont do what it's supposed to do. AdjustWindowRectEx DOESN'T return the rect that the documentation says it should. Im pulling my hair over this one..

##### Share on other sites
Quote:
 Original post by JakobMThanks for your replies, but believe me, I've already tried with both of your suggestions :) The "width + 100, height + 100" detail was of course intended to be expressed like you pointed out Anon Mike, but it's still not where the problem appears. Run the program if you'd like, it just wont do what it's supposed to do. AdjustWindowRectEx DOESN'T return the rect that the documentation says it should. Im pulling my hair over this one..

Poor guy! I understand your pain. It's okay, there is an answer. Sakky will save you!

All that is wrong buddy is the way you are specifying that rect to CreateWindow
Look here at how I do it->
AdjustWindowRectEx( &rcClient, WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX, FALSE, WS_EX_CLIENTEDGE ) ;hWnd = CreateWindowEx( WS_EX_CLIENTEDGE,CLASS_NAME,TITLE_NAME,WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX,rcClient.left,rcClient.top,( rcClient.right - rcClient.left ),( rcClient.bottom - rcClient.top ),NULL,NULL,hInstance,NULL ) ;

Notice something different? You do have to specify the RECT cooridnates as Anon Mike said. You must add in the origin. Now, you have a rectangle that specifies screen coordinates after AdjustWindowRect, but when you use it you are getting crappy resutls. That is because CreateWindow accepts the x, y, width and height of the window. YOU are giving it x, y, another x and yet another y.

Hence, use this method:

rcClient.left,
rcClient.top,
( rcClient.right - rcClient.left ), // width that AdjustWindowRect returned
( rcClient.bottom - rcClient.top ), // height that AdjustWindowRect returned

rect.left, rect.top, rect.right, rect.bottom,

...

##### Share on other sites
Haha, Sakky, you just helped me, but probably not as you intended to :) Try to remove WS_CAPTION from your window style and you'll see what I mean. Without it, AdjustWindowRectEx does nothing (at least in my case). The details you guys have mentioned did their part as well, but it was the WS_CAPTION-part that did the trick. Thanks a bunch!

##### Share on other sites
Then you had two problems then...

1st ) You wern't specifying the coordinates to either AdjustWindowRectEx or CreateWindow

2nd ) You wern't using the correct style so you probebly got just a black rectable. Read the MSDN documentation on CreateWindow, you should see a list of window styles that can be using. There is a little description of each one.

• 10
• 16
• 14
• 18
• 15