Changeset 469
- Timestamp:
- Dec 4, 2014, 9:49:58 PM (10 years ago)
- Location:
- Common
- Files:
-
- 11 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Common/UScaleDPI.pas
r467 r469 8 8 9 9 uses 10 Classes, Forms, Graphics, Controls, ComCtrls, LCLType; 10 Classes, Forms, Graphics, Controls, ComCtrls, LCLType, SysUtils, StdCtrls, 11 Contnrs; 11 12 12 13 type 14 15 { TControlDimension } 16 17 TControlDimension = class 18 BoundsRect: TRect; 19 AuxSize: TPoint; 20 FontHeight: Integer; 21 Controls: TObjectList; // TList<TControlDimension> 22 constructor Create; 23 destructor Destroy; override; 24 end; 13 25 14 26 { TScaleDPI } … … 17 29 private 18 30 FAutoDetect: Boolean; 31 FDesignDPI: TPoint; 32 FDPI: TPoint; 19 33 procedure SetAutoDetect(AValue: Boolean); 34 procedure SetDesignDPI(AValue: TPoint); 35 procedure SetDPI(AValue: TPoint); 20 36 public 21 DPI: TPoint; 22 DesignDPI: TPoint; 37 procedure StoreDimensions(Control: TControl; Dimensions: TControlDimension); 38 procedure RestoreDimensions(Control: TControl; Dimensions: TControlDimension); 39 procedure ScaleDimensions(Control: TControl; Dimensions: TControlDimension); 23 40 procedure ApplyToAll(FromDPI: TPoint); 24 procedure Scale DPI(Control: TControl; FromDPI: TPoint);41 procedure ScaleControl(Control: TControl; FromDPI: TPoint); 25 42 procedure ScaleImageList(ImgList: TImageList; FromDPI: TPoint); 26 function ScaleXY(Size: TPoint; FromDPI: Integer): TPoint; 43 function ScalePoint(APoint: TPoint; FromDPI: TPoint): TPoint; 44 function ScaleRect(ARect: TRect; FromDPI: TPoint): TRect; 27 45 function ScaleX(Size: Integer; FromDPI: Integer): Integer; 28 46 function ScaleY(Size: Integer; FromDPI: Integer): Integer; 29 constructor Create(AOwner: TComponent); 47 constructor Create(AOwner: TComponent); override; 48 property DesignDPI: TPoint read FDesignDPI write SetDesignDPI; 49 property DPI: TPoint read FDPI write SetDPI; 30 50 published 31 51 property AutoDetect: Boolean read FAutoDetect write SetAutoDetect; … … 34 54 procedure Register; 35 55 56 36 57 implementation 37 58 59 resourcestring 60 SWrongDPI = 'Wrong DPI [%d,%d]'; 61 38 62 procedure Register; 39 63 begin 40 64 RegisterComponents('Common', [TScaleDPI]); 65 end; 66 67 { TControlDimension } 68 69 constructor TControlDimension.Create; 70 begin 71 Controls := TObjectList.Create; 72 end; 73 74 destructor TControlDimension.Destroy; 75 begin 76 Controls.Free; 77 inherited Destroy; 41 78 end; 42 79 … … 50 87 end; 51 88 89 procedure TScaleDPI.SetDesignDPI(AValue: TPoint); 90 begin 91 if (FDesignDPI.X = AValue.X) and (FDesignDPI.Y = AValue.Y) then Exit; 92 if (AValue.X <= 0) or (AValue.Y <= 0) then 93 raise Exception.Create(Format(SWrongDPI, [AValue.X, AValue.Y])); 94 FDesignDPI := AValue; 95 end; 96 97 procedure TScaleDPI.SetDPI(AValue: TPoint); 98 begin 99 if (FDPI.X = AValue.X) and (FDPI.Y = AValue.Y) then Exit; 100 if (AValue.X <= 0) or (AValue.Y <= 0) then 101 raise Exception.Create(Format(SWrongDPI, [AValue.X, AValue.Y])); 102 FDPI := AValue; 103 end; 104 105 procedure TScaleDPI.StoreDimensions(Control: TControl; 106 Dimensions: TControlDimension); 107 var 108 NewControl: TControlDimension; 109 I: Integer; 110 begin 111 Dimensions.BoundsRect := Control.BoundsRect; 112 Dimensions.FontHeight := Control.Font.GetTextHeight('Hg'); 113 Dimensions.Controls.Clear; 114 if Control is TToolBar then 115 Dimensions.AuxSize := Point(TToolBar(Control).ButtonWidth, TToolBar(Control).ButtonHeight); 116 117 if Control is TWinControl then 118 for I := 0 to TWinControl(Control).ControlCount - 1 do begin 119 if TWinControl(Control).Controls[I] is TControl then begin 120 NewControl := TControlDimension.Create; 121 Dimensions.Controls.Add(NewControl); 122 StoreDimensions(TWinControl(Control).Controls[I], NewControl); 123 end; 124 end; 125 end; 126 127 procedure TScaleDPI.RestoreDimensions(Control: TControl; 128 Dimensions: TControlDimension); 129 var 130 I: Integer; 131 begin 132 Control.BoundsRect := Dimensions.BoundsRect; 133 Control.Font.Height := Dimensions.FontHeight; 134 if Control is TToolBar then begin 135 TToolBar(Control).ButtonWidth := Dimensions.AuxSize.X; 136 TToolBar(Control).ButtonHeight := Dimensions.AuxSize.Y; 137 end; 138 if Control is TWinControl then 139 for I := 0 to TWinControl(Control).ControlCount - 1 do begin 140 if TWinControl(Control).Controls[I] is TControl then begin 141 RestoreDimensions(TWinControl(Control).Controls[I], TControlDimension(Dimensions.Controls[I])); 142 end; 143 end; 144 end; 145 146 procedure TScaleDPI.ScaleDimensions(Control: TControl; 147 Dimensions: TControlDimension); 148 var 149 I: Integer; 150 begin 151 Control.BoundsRect := ScaleRect(Dimensions.BoundsRect, DesignDPI); 152 Control.Font.Height := ScaleY(Dimensions.FontHeight, DesignDPI.Y); 153 if Control is TToolBar then begin 154 TToolBar(Control).ButtonWidth := ScaleX(Dimensions.AuxSize.X, DesignDPI.X); 155 TToolBar(Control).ButtonHeight := ScaleY(Dimensions.AuxSize.Y, DesignDPI.Y); 156 end; 157 if Control is TWinControl then 158 for I := 0 to TWinControl(Control).ControlCount - 1 do begin 159 if TWinControl(Control).Controls[I] is TControl then begin 160 ScaleDimensions(TWinControl(Control).Controls[I], TControlDimension(Dimensions.Controls[I])); 161 end; 162 end; 163 end; 164 52 165 procedure TScaleDPI.ApplyToAll(FromDPI: TPoint); 53 166 var … … 55 168 begin 56 169 for I := 0 to Screen.FormCount - 1 do begin 57 Scale DPI(Screen.Forms[I], FromDPI);170 ScaleControl(Screen.Forms[I], FromDPI); 58 171 end; 59 172 end; … … 110 223 end; 111 224 112 function TScaleDPI.ScaleXY(Size: TPoint; FromDPI: Integer): TPoint; 113 begin 114 Result.X := ScaleX(Size.X, FromDPI); 115 Result.Y := ScaleY(Size.Y, FromDPI); 225 function TScaleDPI.ScalePoint(APoint: TPoint; FromDPI: TPoint): TPoint; 226 begin 227 Result.X := ScaleX(APoint.X, FromDPI.X); 228 Result.Y := ScaleY(APoint.Y, FromDPI.Y); 229 end; 230 231 function TScaleDPI.ScaleRect(ARect: TRect; FromDPI: TPoint): TRect; 232 begin 233 Result.TopLeft := ScalePoint(ARect.TopLeft, FromDPI); 234 Result.BottomRight := ScalePoint(ARect.BottomRight, FromDPI); 116 235 end; 117 236 … … 123 242 end; 124 243 125 procedure TScaleDPI.Scale DPI(Control: TControl; FromDPI: TPoint);244 procedure TScaleDPI.ScaleControl(Control: TControl; FromDPI: TPoint); 126 245 var 127 246 I: Integer; 128 247 WinControl: TWinControl; 129 248 ToolBarControl: TToolBar; 130 begin 249 OldAnchors: TAnchors; 250 OldAutoSize: Boolean; 251 begin 252 //if Control is TMemo then Exit; 253 //if Control is TForm then 254 // Control.DisableAutoSizing; 131 255 with Control do begin 256 //OldAutoSize := AutoSize; 257 //AutoSize := False; 258 //Anchors := []; 132 259 Left := ScaleX(Left, FromDPI.X); 133 260 Top := ScaleY(Top, FromDPI.Y); 261 //if not (akRight in Anchors) then 134 262 Width := ScaleX(Width, FromDPI.X); 263 //if not (akBottom in Anchors) then 135 264 Height := ScaleY(Height, FromDPI.Y); 136 265 {$IFDEF LCL Qt} … … 139 268 Font.Height := ScaleY(Font.GetTextHeight('Hg'), FromDPI.Y); 140 269 {$ENDIF} 141 end; 270 //Anchors := OldAnchors; 271 //AutoSize := OldAutoSize; 272 end; 273 274 142 275 143 276 if Control is TToolBar then begin … … 149 282 end; 150 283 284 //if not (Control is TCustomPage) then 151 285 if Control is TWinControl then begin 152 286 WinControl := TWinControl(Control); … … 154 288 for I := 0 to WinControl.ControlCount - 1 do begin 155 289 if WinControl.Controls[I] is TControl then begin 156 Scale DPI(WinControl.Controls[I], FromDPI);290 ScaleControl(WinControl.Controls[I], FromDPI); 157 291 end; 158 292 end; 159 293 end; 160 294 end; 295 //if Control is TForm then 296 // Control.EnableAutoSizing; 161 297 end; 162 298
Note:
See TracChangeset
for help on using the changeset viewer.