Resize the shape while maintaining proportions

I have a window in which I display an image. I want the user to be able to resize this window, but keep it in the same aspect ratio as the image, so large blank areas do not appear in the window.

What I tried looks like this in the OnResize event:

DragWidth := Width; DragHeight := Height; //Calculate corresponding size with aspect ratio //... //Calculated values are now in CalcWidth and CalcHeight Width := CalcWidth; Height := CalcHeight; 

The problem is that while dragging the window, there is a drag between the original size and the calculated size, since the OnResize event is called afaik after the resizing has already been done (and drawn once).

Do you know any decision that you have a smooth change in the proportions?

Thanks for any help.

+7
source share
2 answers

Adding the following OnCanResize event handler seemed very useful to me:

 procedure TForm1.FormCanResize(Sender: TObject; var NewWidth, NewHeight: Integer; var Resize: Boolean); var AspectRatio:double; begin AspectRatio:=Height/Width; NewHeight:=round(AspectRatio*NewWidth); end; 

Of course, you can change the calculation method of NewHeight and NewWidth. The following method seems intuitively β€œcorrect” when I try to use it in an empty form:

  NewHeight:=round(0.5*(NewHeight+AspectRatio*NewWidth)); NewWidth:=round(NewHeight/AspectRatio); 
+7
source

Recent delphi have an OnCanResize event that allows you to do just that. You can simply return the calculated NewWidth and NewHeight directly from the event.

+2
source

All Articles