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