Recent

Author Topic: Proportional form resizing  (Read 2332 times)

hukka

  • New Member
  • *
  • Posts: 30
    • Github
Proportional form resizing
« on: December 18, 2017, 12:01:24 am »
In my application I have a small window showing the preview of an image. I would like to be able to resize this form proportionally so as to keep it in the image's aspect ratio. What would be the best way to accomplish this, keeping cross-platform compatibility in mind?

It seems like listening to the WM_SIZING message would be the way to go, but does this work on non-Windows platforms? Any code examples as to how to do this in FreePascal?

Thanks,
hukka

jamie

  • Hero Member
  • *****
  • Posts: 6128
Re: Proportional form resizing
« Reply #1 on: December 18, 2017, 12:41:55 am »
The WM_SIZING would be the best but the form does not handle it.  >:(

 I have an app that I do just that, I  keep the aspect while sizing the frame
so when I grab the sides I can adjust the height and the same for the bottom.
 
 processing the WM_SIZING works nicely because you can intercept the values
and change them before they get used, otherwise you'll get flickering issues doing
it with in the OnSize event.
 
  You can do it in the OnSize event but care must be taken to not have it re-enter
if you change the opposite sides to match, also you need to check which way its being sized. It gets tricky indeed.
The only true wisdom is knowing you know nothing

furious programming

  • Hero Member
  • *****
  • Posts: 858
Re: Proportional form resizing
« Reply #2 on: December 18, 2017, 01:25:30 am »
@hukka: You can override the ChangeBounds method and modify the AWidth and AHeight params. Example:

Code: Pascal  [Select][+][-]
  1. type
  2.   TForm1 = class(TForm)
  3.   protected
  4.     procedure ChangeBounds(ALeft, ATop, AWidth, AHeight: Integer; AKeepBase: Boolean); override;
  5.   end;
  6.  
  7. procedure TForm1.ChangeBounds(ALeft, ATop, AWidth, AHeight: Integer; AKeepBase: Boolean);
  8. begin
  9.   AWidth := AHeight; // form will always be square
  10.   inherited ChangeBounds(ALeft, ATop, AWidth, AHeight, AKeepBase);
  11. end;
  12.  
« Last Edit: December 18, 2017, 01:28:01 am by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

hukka

  • New Member
  • *
  • Posts: 30
    • Github
Re: Proportional form resizing
« Reply #3 on: December 18, 2017, 06:41:00 am »
@hukka: You can override the ChangeBounds method and modify the AWidth and AHeight params.

Perfect, thanks very much for your answer!

 

TinyPortal © 2005-2018