1 | unit fAutoSz;
|
---|
2 | { provides the basic mechanism to resize all the controls on a form when the form size is
|
---|
3 | changed. Differs from frmAResize in that this one descends directly from TForm, rather
|
---|
4 | than TPage }
|
---|
5 |
|
---|
6 | interface
|
---|
7 |
|
---|
8 | uses
|
---|
9 | Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
|
---|
10 |
|
---|
11 | type
|
---|
12 | TfrmAutoSz = class(TForm)
|
---|
13 | procedure FormResize(Sender: TObject);
|
---|
14 | procedure FormDestroy(Sender: TObject);
|
---|
15 | private
|
---|
16 | FSizes: TList;
|
---|
17 | FAutoSizeDisabled: Boolean;
|
---|
18 | protected
|
---|
19 | property AutoSizeDisabled: Boolean read FAutoSizeDisabled write FAutoSizeDisabled;
|
---|
20 | public
|
---|
21 | procedure Loaded; override;
|
---|
22 | end;
|
---|
23 |
|
---|
24 | var
|
---|
25 | frmAutoSz: TfrmAutoSz;
|
---|
26 |
|
---|
27 | implementation
|
---|
28 |
|
---|
29 | {$R *.DFM}
|
---|
30 |
|
---|
31 | uses
|
---|
32 | ORfn;
|
---|
33 | type
|
---|
34 | TSizeRatio = class // records relative sizes and positions for resizing logic
|
---|
35 | FControl: TControl;
|
---|
36 | FLeft: Extended;
|
---|
37 | FTop: Extended;
|
---|
38 | FWidth: Extended;
|
---|
39 | FHeight: Extended;
|
---|
40 | constructor Create(AControl: TControl; W, H: Integer);
|
---|
41 | end;
|
---|
42 |
|
---|
43 | { TSizeRatio methods }
|
---|
44 |
|
---|
45 | constructor TSizeRatio.Create(AControl: TControl; W, H: Integer);
|
---|
46 | begin
|
---|
47 | FControl := AControl;
|
---|
48 | with AControl do
|
---|
49 | begin
|
---|
50 | FLeft := Left / W;
|
---|
51 | FTop := Top / H;
|
---|
52 | FWidth := Width / W;
|
---|
53 | FHeight := Height / H;
|
---|
54 | end;
|
---|
55 | end;
|
---|
56 |
|
---|
57 | { TfrmAutoSz methods }
|
---|
58 |
|
---|
59 | procedure TfrmAutoSz.Loaded;
|
---|
60 | { record initial size & position info for resizing logic }
|
---|
61 | var
|
---|
62 | SizeRatio: TSizeRatio;
|
---|
63 | i,W,H: Integer;
|
---|
64 | Control: TControl;
|
---|
65 | begin
|
---|
66 | inherited Loaded;
|
---|
67 | FSizes := TList.Create;
|
---|
68 | if AutoSizeDisabled then Exit;
|
---|
69 | W := ClientWidth;
|
---|
70 | H := ClientHeight;
|
---|
71 | for i := 0 to ComponentCount - 1 do if Components[i] is TControl then
|
---|
72 | begin
|
---|
73 | Control := TControl(Components[i]);
|
---|
74 | W := HigherOf(W, Control.Left + Control.Width);
|
---|
75 | H := HigherOf(H, Control.Top + Control.Height);
|
---|
76 | end;
|
---|
77 | ClientHeight := H;
|
---|
78 | ClientWidth := W;
|
---|
79 | for i := 0 to ComponentCount - 1 do if Components[i] is TControl then
|
---|
80 | begin
|
---|
81 | SizeRatio := TSizeRatio.Create(TControl(Components[i]), W, H);
|
---|
82 | FSizes.Add(SizeRatio);
|
---|
83 | end;
|
---|
84 | end;
|
---|
85 |
|
---|
86 | procedure TfrmAutoSz.FormResize(Sender: TObject);
|
---|
87 | { resize child controls using their design time proportions }
|
---|
88 | var
|
---|
89 | SizeRatio: TSizeRatio;
|
---|
90 | i, W, H: Integer;
|
---|
91 | begin
|
---|
92 | inherited;
|
---|
93 | if AutoSizeDisabled then Exit;
|
---|
94 | W := HigherOf(ClientWidth, HorzScrollBar.Range);
|
---|
95 | H := HigherOf(ClientHeight, VertScrollBar.Range);
|
---|
96 | with FSizes do for i := 0 to Count - 1 do
|
---|
97 | begin
|
---|
98 | SizeRatio := Items[i];
|
---|
99 | with SizeRatio do
|
---|
100 | if ((FControl is TLabel) and TLabel(FControl).AutoSize) or ((FControl is TStaticText) and TStaticText(FControl).AutoSize) then
|
---|
101 | begin
|
---|
102 | FControl.Left := Round(FLeft*W);
|
---|
103 | FControl.Top := Round(FTop*H);
|
---|
104 | end
|
---|
105 | else FControl.SetBounds(Round(FLeft*W), Round(FTop*H), Round(FWidth*W), Round(FHeight*H));
|
---|
106 | end; {with FSizes}
|
---|
107 | end;
|
---|
108 |
|
---|
109 | procedure TfrmAutoSz.FormDestroy(Sender: TObject);
|
---|
110 | { destroy objects used to record size and position information for controls }
|
---|
111 | var
|
---|
112 | SizeRatio: TSizeRatio;
|
---|
113 | i: Integer;
|
---|
114 | begin
|
---|
115 | inherited;
|
---|
116 | if FSizes <> nil then with FSizes do for i := 0 to Count-1 do
|
---|
117 | begin
|
---|
118 | SizeRatio := Items[i];
|
---|
119 | SizeRatio.Free;
|
---|
120 | end;
|
---|
121 | FSizes.Free;
|
---|
122 | end;
|
---|
123 |
|
---|
124 | end.
|
---|