Recent

Author Topic: [SOLVED] Form scaling aspect ratio  (Read 721 times)

Pe3s

  • Hero Member
  • *****
  • Posts: 628
[SOLVED] Form scaling aspect ratio
« on: November 14, 2025, 10:48:03 am »
Hello, I wrote a code that scales the form while maintaining proportions and allows for maximizing the form. The problem is that the form flashes and jumps. How can I improve the code so that the scaling is smooth?

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormResize(Sender: TObject);
  2.   const
  3.   Aspect = 16 / 9;
  4. var
  5.   NewHeight: Integer;
  6. begin
  7.   // Prevent recursion
  8.   if csDestroying in ComponentState then Exit;
  9.  
  10.   // Calculate the matching height
  11.   NewHeight := Round(Width / Aspect);
  12.  
  13.   // Only adjust height if it actually needs changing
  14.   if Height <> NewHeight then
  15.     Height := NewHeight;
  16. end;      
  17.  
« Last Edit: November 17, 2025, 02:21:06 pm by Pe3s »

jamie

  • Hero Member
  • *****
  • Posts: 7405
Re: Form scaling aspect ratio
« Reply #1 on: November 14, 2025, 12:07:58 pm »
For that you need to handle the message lm_windowposchanging

With you can adjust the sizes coming before they take effect.
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 7405
Re: Form scaling aspect ratio
« Reply #2 on: November 14, 2025, 10:59:03 pm »
It looks like non-one is stepping up to the plate to help you. I may be able to provide an example soon if I don't get too busy.

Jamie
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 7405
Re: Form scaling aspect ratio
« Reply #3 on: November 14, 2025, 11:24:26 pm »
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs,LMessages,lclType;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.   private
  16.  
  17.   public
  18.     Procedure CatchSizing(var Mgs:TLMessage); Message LM_WindowPosChanging;
  19.   end;
  20.  
  21. var
  22.   Form1: TForm1;
  23.  
  24. implementation
  25.  
  26. {$R *.lfm}
  27.  
  28. { TForm1 }
  29.  
  30. procedure TForm1.CatchSizing(var Mgs: TLMessage);
  31. begin
  32.   With WindowPos(Pointer(mgs.Lparam)^) do
  33.    begin
  34.      cy := Trunc(cx * 9/16);
  35.    end;
  36. end;
  37.  
  38. end.
  39.  

I may have the ration backwards, but you can resize the width of this and see how smooth this is.
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 7405
Re: Form scaling aspect ratio
« Reply #4 on: November 15, 2025, 02:50:38 am »
here is your test app modified.

The only true wisdom is knowing you know nothing

Pe3s

  • Hero Member
  • *****
  • Posts: 628
Re: Form scaling aspect ratio
« Reply #5 on: November 15, 2025, 09:10:20 am »
@Jamie Thank you for taking the time to help. I have one more question: the form scales perfectly from the guides and the right and left sides, but it does not scale from the bottom. What is the reason for this?

Pe3s

  • Hero Member
  • *****
  • Posts: 628
Re: Form scaling aspect ratio
« Reply #6 on: November 15, 2025, 10:32:26 am »
I also noticed that when we place a component, e.g., Image (alClient), on the form, the code adds a margin on the right side, and the image does not cover the form.

jamie

  • Hero Member
  • *****
  • Posts: 7405
Re: Form scaling aspect ratio
« Reply #7 on: November 15, 2025, 05:01:41 pm »
I put a little time into this for you, it's not perfect but you can get some insight on it.

In code you will need to size the frame via the WIDTH and the aspect will take over.

With the mouse, you can grab the corners for a fast size or the middle bottom which presents a little problem and that
is you need to move slowly; I didn't have enough time to do it better.

 Maybe someone else can make the bottom drag enable faster move because I used a "hittest" and if you move the mouse too fast, it loses its hit position and snaps back.

  There are other ways to do this but, the math is there to properly calculate the sizes.

Also, I showed how to stretch the Timage for you.

Attached is the project



The only true wisdom is knowing you know nothing

Pe3s

  • Hero Member
  • *****
  • Posts: 628
Re: Form scaling aspect ratio
« Reply #8 on: November 17, 2025, 02:20:45 pm »
@Jamie Thank you for your help and examples.
Best regards

 

TinyPortal © 2005-2018