grunge fightr
grunge fightr

Reputation: 1370

How to set exact client size for overlapped window? (winapi)

I want to set anexact client size (say 300 x 300) when I create a normal 'overlapped' window.

There is the AdjustWindowRectEx() function where I can pass the desired client area and get sizes that I can pass to CreateWindowEx but it says in the documentation that I should not use it for overlapped windows and I do not understand why.

When I pass 300 x 300, I got 306 x 326 from it, and when I receive WM_SIZE, the client size is 298 x 298 so it is 2 pixels too small.

How to create overlapped window with exact given client size?

Upvotes: 4

Views: 2976

Answers (2)

grunge fightr
grunge fightr

Reputation: 1370

My mistake in my case was the following:

AdjustWindowRectEx(&rect, WS_OVERLAPPEDWINDOW, WS_CLIPSIBLINGS, FALSE, WS_OVERLAPPEDWINDOW);

Changing to this fixes it:

AdjustWindowRectEx(&rect, WS_OVERLAPPEDWINDOW, WS_CLIPSIBLINGS, FALSE, 0);

Upvotes: 2

arx
arx

Reputation: 16904

I don't know why AdjustWindowRect isn't working (unless your client area contains a child window with a one-pixel border).

Note that you can use this function for overlapped windows. The documentation says that you can't use the WS_OVERLAPPED style (I guess because it's value is zero), but you could use WS_OVERLAPPEDWINDOW.

As an alternative you can call GetWindowRect and GetClientRect, calculate the width/height of the borders (the difference between the width/height of the two rectangles), add these to the desired client size and set the window size. I believe you can do this before the window is shown.

Upvotes: 1

Related Questions