Recent

Author Topic: [SOLVED] Form scaling  (Read 2365 times)

Pe3s

  • Hero Member
  • *****
  • Posts: 612
[SOLVED] Form scaling
« on: August 25, 2025, 10:19:52 am »
Hello, I have a question: is it possible to implement proportional scaling of a form, e.g. 16:9, but without unnecessary flickering of the form so that it scales nicely and smoothly?

Code: Pascal  [Select][+][-]
  1. private
  2.      FIgnoreResize: Boolean;
  3.  
  4.   public
  5.  
  6.   end;
  7.  
  8. var
  9.   Form1: TForm1;
  10.  
  11. implementation
  12.  
  13. {$R *.lfm}
  14.  
  15. { TForm1 }
  16.  
  17. procedure TForm1.FormCreate(Sender: TObject);
  18. begin
  19.   Width := 854;
  20.    Height := 480;
  21.    DoubleBuffered := True;
  22.    FIgnoreResize := False;
  23. end;
  24.  
  25. procedure TForm1.FormResize(Sender: TObject);
  26. const
  27. Ratio: Double = 16 / 9;
  28. var
  29. NewHeight: Integer;
  30. begin
  31. if FIgnoreResize then Exit;
  32.  
  33. FIgnoreResize := True;
  34. try
  35.  
  36.   NewHeight := Round(Width / Ratio);
  37.   if NewHeight <> Height then
  38.     Height := NewHeight;
  39. finally
  40.   FIgnoreResize := False;
  41. end;
  42. end;                              
  43.  
« Last Edit: September 07, 2025, 11:40:28 am by Pe3s »

Ally

  • Jr. Member
  • **
  • Posts: 80
Re: Form scaling
« Reply #1 on: August 25, 2025, 12:35:57 pm »
Hello Pe3s,

I have reduced the whole thing to the bare minimum.
The window changes size without flickering. (Win11, Lazarus 4.2)

Pe3s

  • Hero Member
  • *****
  • Posts: 612
Re: Form scaling
« Reply #2 on: August 25, 2025, 04:01:29 pm »
The code works, but you just need to add it to OnResize Form
and change the size, then it's a disco.

Ally

  • Jr. Member
  • **
  • Posts: 80
Re: Form scaling
« Reply #3 on: August 25, 2025, 04:32:21 pm »
That's not a problem for me on Windows either. Your code also runs without flickering.
However, the problem could be the following: FormResize is called when the size of Form changes.
If you change the size of Form within FormResize, FormResize is called again.
That doesn't normally make sense. That's why I put the size change of Form in a ButtonClick to prevent the recursive call.

Ally

  • Jr. Member
  • **
  • Posts: 80
Re: Form scaling
« Reply #4 on: August 25, 2025, 05:56:10 pm »
I must apologize.
I have now understood the problem.
Enclosed is a solution that prevents flickering when changing the size.

Pe3s

  • Hero Member
  • *****
  • Posts: 612
Re: Form scaling
« Reply #5 on: September 07, 2025, 11:40:08 am »
Thank you :)

 

TinyPortal © 2005-2018