Uniform resize of a window

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

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 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 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 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 on other sites
According to this post WM_WINDOWPOSCHANGING should do what you need too.

Share on other sites
Quote:
 Original post by Adam_42According to this post WM_WINDOWPOSCHANGING should do what you need too.

It is not from "this" post.
It's a Usenet FAQ (for > 15 years !)

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

• 12
• 9
• 13
• 41
• 15