Changeset 73 for trunk/Packages/Common/UScaleDPI.pas
- Timestamp:
- Oct 27, 2016, 3:00:47 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Packages/Common/UScaleDPI.pas
r72 r73 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 FontHeight: Integer; 20 Controls: TObjectList; // TList<TControlDimension> 21 // Class specifics 22 ButtonSize: TPoint; // TToolBar 23 CoolBandWidth: Integer; 24 ConstraintsMin: TPoint; // TForm 25 ConstraintsMax: TPoint; // TForm 26 constructor Create; 27 destructor Destroy; override; 28 end; 13 29 14 30 { TScaleDPI } … … 17 33 private 18 34 FAutoDetect: Boolean; 35 FDesignDPI: TPoint; 36 FDPI: TPoint; 19 37 procedure SetAutoDetect(AValue: Boolean); 38 procedure SetDesignDPI(AValue: TPoint); 39 procedure SetDPI(AValue: TPoint); 20 40 public 21 DPI: TPoint; 22 DesignDPI: TPoint; 41 procedure StoreDimensions(Control: TControl; Dimensions: TControlDimension); 42 procedure RestoreDimensions(Control: TControl; Dimensions: TControlDimension); 43 procedure ScaleDimensions(Control: TControl; Dimensions: TControlDimension); 23 44 procedure ApplyToAll(FromDPI: TPoint); 24 procedure Scale DPI(Control: TControl; FromDPI: TPoint);45 procedure ScaleControl(Control: TControl; FromDPI: TPoint); 25 46 procedure ScaleImageList(ImgList: TImageList; FromDPI: TPoint); 26 function ScaleXY(Size: TPoint; FromDPI: Integer): TPoint; 47 function ScalePoint(APoint: TPoint; FromDPI: TPoint): TPoint; 48 function ScaleRect(ARect: TRect; FromDPI: TPoint): TRect; 27 49 function ScaleX(Size: Integer; FromDPI: Integer): Integer; 28 50 function ScaleY(Size: Integer; FromDPI: Integer): Integer; 29 51 constructor Create(AOwner: TComponent); override; 52 property DesignDPI: TPoint read FDesignDPI write SetDesignDPI; 53 property DPI: TPoint read FDPI write SetDPI; 30 54 published 31 55 property AutoDetect: Boolean read FAutoDetect write SetAutoDetect; … … 34 58 procedure Register; 35 59 60 36 61 implementation 37 62 63 resourcestring 64 SWrongDPI = 'Wrong DPI [%d,%d]'; 65 38 66 procedure Register; 39 67 begin 40 68 RegisterComponents('Common', [TScaleDPI]); 69 end; 70 71 { TControlDimension } 72 73 constructor TControlDimension.Create; 74 begin 75 Controls := TObjectList.Create; 76 end; 77 78 destructor TControlDimension.Destroy; 79 begin 80 FreeAndNil(Controls); 81 inherited Destroy; 41 82 end; 42 83 … … 50 91 end; 51 92 93 procedure TScaleDPI.SetDesignDPI(AValue: TPoint); 94 begin 95 if (FDesignDPI.X = AValue.X) and (FDesignDPI.Y = AValue.Y) then Exit; 96 if (AValue.X <= 0) or (AValue.Y <= 0) then 97 raise Exception.Create(Format(SWrongDPI, [AValue.X, AValue.Y])); 98 FDesignDPI := AValue; 99 end; 100 101 procedure TScaleDPI.SetDPI(AValue: TPoint); 102 begin 103 if (FDPI.X = AValue.X) and (FDPI.Y = AValue.Y) then Exit; 104 if (AValue.X <= 0) or (AValue.Y <= 0) then 105 raise Exception.Create(Format(SWrongDPI, [AValue.X, AValue.Y])); 106 FDPI := AValue; 107 end; 108 109 procedure TScaleDPI.StoreDimensions(Control: TControl; 110 Dimensions: TControlDimension); 111 var 112 NewControl: TControlDimension; 113 I: Integer; 114 begin 115 Dimensions.BoundsRect := Control.BoundsRect; 116 Dimensions.FontHeight := Control.Font.GetTextHeight('Hg'); 117 Dimensions.Controls.Clear; 118 if Control is TToolBar then 119 Dimensions.ButtonSize := Point(TToolBar(Control).ButtonWidth, TToolBar(Control).ButtonHeight); 120 if Control is TForm then begin 121 Dimensions.ConstraintsMin := Point(TForm(Control).Constraints.MinWidth, 122 TForm(Control).Constraints.MinHeight); 123 Dimensions.ConstraintsMax := Point(TForm(Control).Constraints.MaxWidth, 124 TForm(Control).Constraints.MaxHeight); 125 end; 126 if Control is TWinControl then 127 for I := 0 to TWinControl(Control).ControlCount - 1 do begin 128 if TWinControl(Control).Controls[I] is TControl then 129 // Do not scale docked forms twice 130 if not (TWinControl(Control).Controls[I] is TForm) then begin 131 NewControl := TControlDimension.Create; 132 Dimensions.Controls.Add(NewControl); 133 StoreDimensions(TWinControl(Control).Controls[I], NewControl); 134 end; 135 end; 136 end; 137 138 procedure TScaleDPI.RestoreDimensions(Control: TControl; 139 Dimensions: TControlDimension); 140 var 141 I: Integer; 142 begin 143 Control.BoundsRect := Dimensions.BoundsRect; 144 Control.Font.Height := Dimensions.FontHeight; 145 if Control is TToolBar then begin 146 TToolBar(Control).ButtonWidth := Dimensions.ButtonSize.X; 147 TToolBar(Control).ButtonHeight := Dimensions.ButtonSize.Y; 148 end; 149 if Control is TForm then begin 150 TForm(Control).Constraints.MinWidth := Dimensions.ConstraintsMin.X; 151 TForm(Control).Constraints.MinHeight := Dimensions.ConstraintsMin.Y; 152 TForm(Control).Constraints.MaxWidth := Dimensions.ConstraintsMax.X; 153 TForm(Control).Constraints.MaxHeight := Dimensions.ConstraintsMax.Y; 154 end; 155 if Control is TWinControl then 156 for I := 0 to TWinControl(Control).ControlCount - 1 do begin 157 if TWinControl(Control).Controls[I] is TControl then 158 // Do not scale docked forms twice 159 if not (TWinControl(Control).Controls[I] is TForm) then begin 160 RestoreDimensions(TWinControl(Control).Controls[I], TControlDimension(Dimensions.Controls[I])); 161 end; 162 end; 163 end; 164 165 procedure TScaleDPI.ScaleDimensions(Control: TControl; 166 Dimensions: TControlDimension); 167 var 168 I: Integer; 169 begin 170 Control.BoundsRect := ScaleRect(Dimensions.BoundsRect, DesignDPI); 171 Control.Font.Height := ScaleY(Dimensions.FontHeight, DesignDPI.Y); 172 if Control is TToolBar then begin 173 TToolBar(Control).ButtonWidth := ScaleX(Dimensions.ButtonSize.X, DesignDPI.X); 174 TToolBar(Control).ButtonHeight := ScaleY(Dimensions.ButtonSize.Y, DesignDPI.Y); 175 end; 176 if Control is TCoolBar then begin 177 with TCoolBar(Control) do 178 for I := 0 to Bands.Count - 1 do 179 with TCoolBand(Bands[I]) do begin 180 MinWidth := ScaleX(Dimensions.ButtonSize.X, DesignDPI.X); 181 MinHeight := ScaleY(Dimensions.ButtonSize.Y, DesignDPI.Y); 182 //Width := ScaleX(Dimensions.BoundsRect.Left - 183 end; 184 end; 185 if Control is TForm then begin 186 TForm(Control).Constraints.MinWidth := ScaleX(Dimensions.ConstraintsMin.X, DesignDPI.X); 187 TForm(Control).Constraints.MaxWidth := ScaleX(Dimensions.ConstraintsMax.X, DesignDPI.X); 188 TForm(Control).Constraints.MinHeight := ScaleY(Dimensions.ConstraintsMin.Y, DesignDPI.Y); 189 TForm(Control).Constraints.MaxHeight := ScaleY(Dimensions.ConstraintsMax.Y, DesignDPI.Y); 190 end; 191 if Control is TWinControl then 192 for I := 0 to TWinControl(Control).ControlCount - 1 do begin 193 if TWinControl(Control).Controls[I] is TControl then 194 // Do not scale docked forms twice 195 if not (TWinControl(Control).Controls[I] is TForm) then begin 196 ScaleDimensions(TWinControl(Control).Controls[I], TControlDimension(Dimensions.Controls[I])); 197 end; 198 end; 199 end; 200 52 201 procedure TScaleDPI.ApplyToAll(FromDPI: TPoint); 53 202 var … … 55 204 begin 56 205 for I := 0 to Screen.FormCount - 1 do begin 57 Scale DPI(Screen.Forms[I], FromDPI);206 ScaleControl(Screen.Forms[I], FromDPI); 58 207 end; 59 208 end; … … 70 219 71 220 SetLength(Temp, ImgList.Count); 72 TempBmp := TBitmap.Create;73 221 for I := 0 to ImgList.Count - 1 do 74 222 begin 223 TempBmp := TBitmap.Create; 224 TempBmp.PixelFormat := pf32bit; 75 225 ImgList.GetBitmap(I, TempBmp); 76 //TempBmp.PixelFormat := pfDevice;77 226 Temp[I] := TBitmap.Create; 78 227 Temp[I].SetSize(NewWidth, NewHeight); 228 Temp[I].PixelFormat := pf32bit; 79 229 Temp[I].TransparentColor := TempBmp.TransparentColor; 80 230 //Temp[I].TransparentMode := TempBmp.TransparentMode; … … 86 236 if (Temp[I].Width = 0) or (Temp[I].Height = 0) then Continue; 87 237 Temp[I].Canvas.StretchDraw(Rect(0, 0, Temp[I].Width, Temp[I].Height), TempBmp); 88 end;89 TempBmp.Free;238 TempBmp.Free; 239 end; 90 240 91 241 ImgList.Clear; … … 110 260 end; 111 261 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); 262 function TScaleDPI.ScalePoint(APoint: TPoint; FromDPI: TPoint): TPoint; 263 begin 264 Result.X := ScaleX(APoint.X, FromDPI.X); 265 Result.Y := ScaleY(APoint.Y, FromDPI.Y); 266 end; 267 268 function TScaleDPI.ScaleRect(ARect: TRect; FromDPI: TPoint): TRect; 269 begin 270 Result.TopLeft := ScalePoint(ARect.TopLeft, FromDPI); 271 Result.BottomRight := ScalePoint(ARect.BottomRight, FromDPI); 116 272 end; 117 273 … … 123 279 end; 124 280 125 procedure TScaleDPI.Scale DPI(Control: TControl; FromDPI: TPoint);281 procedure TScaleDPI.ScaleControl(Control: TControl; FromDPI: TPoint); 126 282 var 127 283 I: Integer; 128 284 WinControl: TWinControl; 129 285 ToolBarControl: TToolBar; 130 begin 286 OldAnchors: TAnchors; 287 OldAutoSize: Boolean; 288 begin 289 //if Control is TMemo then Exit; 290 //if Control is TForm then 291 // Control.DisableAutoSizing; 131 292 with Control do begin 293 //OldAutoSize := AutoSize; 294 //AutoSize := False; 295 //Anchors := []; 132 296 Left := ScaleX(Left, FromDPI.X); 133 297 Top := ScaleY(Top, FromDPI.Y); 298 //if not (akRight in Anchors) then 134 299 Width := ScaleX(Width, FromDPI.X); 300 //if not (akBottom in Anchors) then 135 301 Height := ScaleY(Height, FromDPI.Y); 136 302 {$IFDEF LCL Qt} … … 139 305 Font.Height := ScaleY(Font.GetTextHeight('Hg'), FromDPI.Y); 140 306 {$ENDIF} 307 //Anchors := OldAnchors; 308 //AutoSize := OldAutoSize; 141 309 end; 142 310 … … 149 317 end; 150 318 319 //if not (Control is TCustomPage) then 151 320 if Control is TWinControl then begin 152 321 WinControl := TWinControl(Control); … … 154 323 for I := 0 to WinControl.ControlCount - 1 do begin 155 324 if WinControl.Controls[I] is TControl then begin 156 Scale DPI(WinControl.Controls[I], FromDPI);325 ScaleControl(WinControl.Controls[I], FromDPI); 157 326 end; 158 327 end; 159 328 end; 160 329 end; 330 //if Control is TForm then 331 // Control.EnableAutoSizing; 161 332 end; 162 333
Note:
See TracChangeset
for help on using the changeset viewer.