Sign in to follow this  

Uniform resize of a window

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Greetings, I was wondering if anyone here knew how to forcefully constrict a window to a certain aspect ratio. For example, if a user tries to horizontally scale a window, the vertical would compensate and resize (in real time) so that the aspect ratio is unchanged. I know some games do this when in windowed mode, for example World of Warcraft. I did a bit of research and Here are my findings (maybe you guys can offer other suggestions). First, is a hook to my own window. It would intercept the WM_SIZE event and modify the RECT that governs the window size. It would then uniformly adjust the size. This method is a bit ugly! Secondly, is to use the WM_NCHITTEST message. I would then capture the mouse movements and manually send them back into the Message queue so that WM_SIZE would be handled later on. Its cleaner then the previous way but still a bit nutty. Any suggestions??

Share this post


Link to post
Share on other sites
When you get a WM_SIZE just resize the window yourself to fit? You don't need to hook your own window, you already get all the messages it receives.

Share this post


Link to post
Share on other sites
The problem with WM_SIZE is its called AFTER the window has been resized. It is an event that is fired when the user completes one tick of the resize. And its called per Tick. So the problem is that when the user starts resizing the window using the very corner of the window as its handle, the window will resize as its supposed to and once it does, it will notify my program with a WM_SIZE event.

Hence WM_SIZE is not what I'm looking for.

WM_SIZING on the other hand, might be worth looking into.

Share this post


Link to post
Share on other sites
WM_SIZING is indeed the message you want to handle, since you can modify the RECT passed in in the lParam (I've done this myself to prevent the window being sized too large or small [And yes, I know there's a better way to do that now [smile]])

Share this post


Link to post
Share on other sites
Looks like WM_SIZING did the trick. Thank you all :)!
Just for reference, of if anyone else is curious, here is the code:


LRESULT Window::onSizing(WPARAM wParam, LPARAM lParam)
{
RECT sz;
int w, h;

// If the aspect is locked, resize the window equally
if (lockAspect)
{
// Copy over the new size that was determined by windows
memcpy(&sz, (const Void *) lParam, sizeof(RECT));

// Calculate the width and height of the window
w = sz.right - sz.left;
h = sz.bottom - sz.top;

switch (wParam)
{
case WMSZ_LEFT:
case WMSZ_RIGHT:
// Modify the Height of the window
sz.bottom = LONG(w * aspectRatio) + sz.top;
break;

case WMSZ_TOP:
case WMSZ_BOTTOM:
// Modify the Width of the window
sz.right = LONG(h * (1 / aspectRatio)) + sz.left;
break;

case WMSZ_TOPRIGHT:
case WMSZ_TOPLEFT:
case WMSZ_BOTTOMRIGHT:
case WMSZ_BOTTOMLEFT:
// Adjust the width and height of the window to match aspect ratio
if (float(h) / float(w) > aspectRatio)
{
w = int(float(h) / aspectRatio);
}else{
h = int(float(w) * aspectRatio);
}

// Adjust Height
if (wParam == WMSZ_TOPLEFT || wParam == WMSZ_TOPRIGHT)
{
sz.top = sz.bottom - h;
}else{
sz.bottom = sz.top + h;
}

// Adjust Width
if (wParam == WMSZ_TOPLEFT || wParam == WMSZ_BOTTOMLEFT)
{
sz.left = sz.right - w;
}else{
sz.right = sz.left + w;
}
break;
}

// Copy back the size of the window
memcpy((void *) lParam, &sz, sizeof(RECT));

return TRUE;
}

return FALSE;
}



Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this