Resizing a window while keeping the aspect ratio.

Posted on 2003-03-25
Medium Priority
Last Modified: 2013-12-03
Using the standard windows API, how can I make a window resize with a fixed aspect ratio (for example 1.3h x h) when dragging the resize border?
Question by:batboy2_
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 49

Accepted Solution

DanRollins earned 150 total points
ID: 8209266
Here's 50 points worth of answer:
Imagine that you want to enforce x=2y... Add this to you WindowProc:

          WINDOWPOS* rWP= (WINDOWPOS*)lParam;
          rWP->cx= (rWP->cy * 2);
This overrides the size settings.  You will need to add logic to make this more intelligent.  For instance, this works if you increase or decrease the height (drag the bottom), but not the width (drag the side).  You will need to decide which direction is changing (use GetWindowRect and compare to the values in the WINDOWPOS struct).  You will also need to initially create the window with a reasonably-proportioned size or when it first opens, it will change unexpectedly (in the above example the window immediately gets very wide).

-- Dan

Author Comment

ID: 8215993
Thank you. After a little tweaking that solved the problem.

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question