Recent

Author Topic: [SOLVED] Components AutoSizing on Notebook  (Read 970 times)

d3crypti0n

  • New Member
  • *
  • Posts: 11
[SOLVED] Components AutoSizing on Notebook
« on: April 23, 2021, 12:04:22 am »
I found an interesting code snippet on the internet that adjusts all the components to the size of the tool. So when you stretch/shrink the tool, the components get bigger/smaller and the distance to each other and the edge of my form always stays equivalent to the size of my tool. If you shrink the window to a minimum no component disappears from the window. The font size changes as well.

Now when I add a Notebook with different pages, the components of my tool stay fixed. When you shrink the tool and move the side of the window, the components don't get bigger/smaller and they are also not accessible if you don't move the side back. I hope it get's clear what I mean

How would you change the code in order for the components to be stretched/shrunken on a Notebook-Page ?

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ComCtrls, StdCtrls,
  9.   ExtCtrls, Buttons, Menus;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Notebook1: TNotebook;
  17.     PaintBox1: TPaintBox;
  18.     PaintBox2: TPaintBox;
  19.     Button1: TButton;
  20.     Button2: TBUtton;
  21.     Page1: TPage;
  22.     Page2: TPage;
  23.  
  24.     procedure Button1Click(Sender: TObject);
  25.     procedure FormCreate(Sender: TObject);
  26.     procedure FormResize(Sender: TObject);
  27.  
  28.   private
  29.  
  30.   public
  31.  
  32.   end;
  33.  
  34. var
  35.   Form1: TForm1;
  36.   AutoZoom: array of array of real;
  37.   FormAspect: real;
  38.  
  39. implementation
  40.  
  41. {$R *.lfm}
  42.  
  43. { TForm1 }
  44.  
  45. procedure TForm1.Button1Click(Sender: TObject);
  46. begin
  47.    Close;
  48. end;
  49.  
  50. procedure TForm1.FormCreate(Sender: TObject);
  51. var
  52.   i: Integer;
  53. begin
  54.    FormAspect := Self.Width / Self.Height;
  55.    SetLength(AutoZoom, Self.ControlCount, 6);
  56.    for i := 0 to Self.ControlCount - 1 do
  57.    begin
  58.       AutoZoom[i][0] := Self.Controls[i].Top / Self.Height;
  59.       AutoZoom[i][1] := Self.Controls[i].Left / Self.Width;
  60.       AutoZoom[i][2] := Self.Controls[i].Width / Self.Width;
  61.       AutoZoom[i][3] := Self.Controls[i].Height / Self.Height;
  62.       if Self.Controls[i].Font.Height = 0 then
  63.          Self.Controls[i].Font.Height := 80;  //Font.Size
  64.          AutoZoom[i][4] := Self.Controls[i].Font.Height / Self.Width;
  65.          AutoZoom[i][5] := Self.Controls[i].Font.Height / Self.Height;
  66.    end;
  67. end;
  68.  
  69. procedure TForm1.FormResize(Sender: TObject);
  70. var
  71.    i: integer;
  72. begin
  73.    for i := 0 to Form1.ControlCount - 1 do
  74.       begin
  75.          Self.Controls[i].Top := Round(AutoZoom[i][0] * Self.Height);
  76.          Self.Controls[i].Left := Round(AutoZoom[i][1] * Self.Width);
  77.          Self.Controls[i].Width := Round(AutoZoom[i][2] * Self.Width);
  78.          Self.Controls[i].Height := Round(AutoZoom[i][3] * Self.Height);
  79.          if Self.Width / Self.Height < FormAspect then
  80.             Self.Controls[i].Font.Height := Round(AutoZoom[i][4] * Self.Width)
  81.          else
  82.             Self.Controls[i].Font.Height := Round(AutoZoom[i][4] * Self.Height);
  83.       end;
  84. end;
  85.  
  86. end.
  87.  
« Last Edit: April 24, 2021, 11:51:56 pm by d3crypti0n »

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: Components AutoSizing on Notebook
« Reply #1 on: April 23, 2021, 01:47:22 am »
Have you met Mr. Anchor Editor ?

Other than that, you may need to adjust your font sizes..
The only true wisdom is knowing you know nothing

alpine

  • Hero Member
  • *****
  • Posts: 1032
Re: Components AutoSizing on Notebook
« Reply #2 on: April 23, 2021, 01:49:52 am »
Now when I add a Notebook with different pages, the components of my tool stay fixed. When you shrink the tool and move the side of the window, the components don't get bigger/smaller and they are also not accessible if you don't move the side back. I hope it get's clear what I mean

That is because Form1.Controls doesn't include them. There is only the Notebook1 on which the Form1 is the immediate owner. Notebook1.Controls eventually includes Page1 and Page2. In turn, each of the pages includes some of the other controls.

You can't walk them in a single loop, you should go in depth.

How would you change the code in order for the components to be stretched/shrunken on a Notebook-Page ?

May be you should consider the Anchors property and Anchor Editor into the Object Inspector if you want to keep the edge offsets constant.
 
IMHO, (if we put aside the question of the rightness of this method) this code is incorrect for scaling, at least it keeps a lot of unnecessary information. You can keep only the X,Y ratios of the form itself.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

 

TinyPortal © 2005-2018