| 1 | unit UScaleDPI;
|
|---|
| 2 |
|
|---|
| 3 | { See: http://wiki.lazarus.freepascal.org/High_DPI }
|
|---|
| 4 |
|
|---|
| 5 | {$mode delphi}{$H+}
|
|---|
| 6 |
|
|---|
| 7 | interface
|
|---|
| 8 |
|
|---|
| 9 | uses
|
|---|
| 10 | Classes, Forms, Graphics, Controls, ComCtrls, LCLType, SysUtils, StdCtrls,
|
|---|
| 11 | Contnrs;
|
|---|
| 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;
|
|---|
| 25 |
|
|---|
| 26 | { TScaleDPI }
|
|---|
| 27 |
|
|---|
| 28 | TScaleDPI = class(TComponent)
|
|---|
| 29 | private
|
|---|
| 30 | FAutoDetect: Boolean;
|
|---|
| 31 | FDesignDPI: TPoint;
|
|---|
| 32 | FDPI: TPoint;
|
|---|
| 33 | procedure SetAutoDetect(AValue: Boolean);
|
|---|
| 34 | procedure SetDesignDPI(AValue: TPoint);
|
|---|
| 35 | procedure SetDPI(AValue: TPoint);
|
|---|
| 36 | public
|
|---|
| 37 | procedure StoreDimensions(Control: TControl; Dimensions: TControlDimension);
|
|---|
| 38 | procedure RestoreDimensions(Control: TControl; Dimensions: TControlDimension);
|
|---|
| 39 | procedure ScaleDimensions(Control: TControl; Dimensions: TControlDimension);
|
|---|
| 40 | procedure ApplyToAll(FromDPI: TPoint);
|
|---|
| 41 | procedure ScaleControl(Control: TControl; FromDPI: TPoint);
|
|---|
| 42 | procedure ScaleImageList(ImgList: TImageList; FromDPI: TPoint);
|
|---|
| 43 | function ScalePoint(APoint: TPoint; FromDPI: TPoint): TPoint;
|
|---|
| 44 | function ScaleRect(ARect: TRect; FromDPI: TPoint): TRect;
|
|---|
| 45 | function ScaleX(Size: Integer; FromDPI: Integer): Integer;
|
|---|
| 46 | function ScaleY(Size: Integer; FromDPI: Integer): Integer;
|
|---|
| 47 | constructor Create(AOwner: TComponent); override;
|
|---|
| 48 | property DesignDPI: TPoint read FDesignDPI write SetDesignDPI;
|
|---|
| 49 | property DPI: TPoint read FDPI write SetDPI;
|
|---|
| 50 | published
|
|---|
| 51 | property AutoDetect: Boolean read FAutoDetect write SetAutoDetect;
|
|---|
| 52 | end;
|
|---|
| 53 |
|
|---|
| 54 | procedure Register;
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 | implementation
|
|---|
| 58 |
|
|---|
| 59 | resourcestring
|
|---|
| 60 | SWrongDPI = 'Wrong DPI [%d,%d]';
|
|---|
| 61 |
|
|---|
| 62 | procedure Register;
|
|---|
| 63 | begin
|
|---|
| 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;
|
|---|
| 78 | end;
|
|---|
| 79 |
|
|---|
| 80 | procedure TScaleDPI.SetAutoDetect(AValue: Boolean);
|
|---|
| 81 | begin
|
|---|
| 82 | if FAutoDetect = AValue then Exit;
|
|---|
| 83 | FAutoDetect := AValue;
|
|---|
| 84 | if AValue then begin
|
|---|
| 85 | DPI := Point(ScreenInfo.PixelsPerInchX, ScreenInfo.PixelsPerInchY);
|
|---|
| 86 | end;
|
|---|
| 87 | end;
|
|---|
| 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 |
|
|---|
| 165 | procedure TScaleDPI.ApplyToAll(FromDPI: TPoint);
|
|---|
| 166 | var
|
|---|
| 167 | I: Integer;
|
|---|
| 168 | begin
|
|---|
| 169 | for I := 0 to Screen.FormCount - 1 do begin
|
|---|
| 170 | ScaleControl(Screen.Forms[I], FromDPI);
|
|---|
| 171 | end;
|
|---|
| 172 | end;
|
|---|
| 173 |
|
|---|
| 174 | procedure TScaleDPI.ScaleImageList(ImgList: TImageList; FromDPI: TPoint);
|
|---|
| 175 | var
|
|---|
| 176 | TempBmp: TBitmap;
|
|---|
| 177 | Temp: array of TBitmap;
|
|---|
| 178 | NewWidth, NewHeight: integer;
|
|---|
| 179 | I: Integer;
|
|---|
| 180 | begin
|
|---|
| 181 | NewWidth := ScaleX(ImgList.Width, FromDPI.X);
|
|---|
| 182 | NewHeight := ScaleY(ImgList.Height, FromDPI.Y);
|
|---|
| 183 |
|
|---|
| 184 | SetLength(Temp, ImgList.Count);
|
|---|
| 185 | TempBmp := TBitmap.Create;
|
|---|
| 186 | for I := 0 to ImgList.Count - 1 do
|
|---|
| 187 | begin
|
|---|
| 188 | ImgList.GetBitmap(I, TempBmp);
|
|---|
| 189 | //TempBmp.PixelFormat := pfDevice;
|
|---|
| 190 | Temp[I] := TBitmap.Create;
|
|---|
| 191 | Temp[I].SetSize(NewWidth, NewHeight);
|
|---|
| 192 | Temp[I].TransparentColor := TempBmp.TransparentColor;
|
|---|
| 193 | //Temp[I].TransparentMode := TempBmp.TransparentMode;
|
|---|
| 194 | Temp[I].Transparent := True;
|
|---|
| 195 | Temp[I].Canvas.Brush.Style := bsSolid;
|
|---|
| 196 | Temp[I].Canvas.Brush.Color := Temp[I].TransparentColor;
|
|---|
| 197 | Temp[I].Canvas.FillRect(0, 0, Temp[I].Width, Temp[I].Height);
|
|---|
| 198 |
|
|---|
| 199 | if (Temp[I].Width = 0) or (Temp[I].Height = 0) then Continue;
|
|---|
| 200 | Temp[I].Canvas.StretchDraw(Rect(0, 0, Temp[I].Width, Temp[I].Height), TempBmp);
|
|---|
| 201 | end;
|
|---|
| 202 | TempBmp.Free;
|
|---|
| 203 |
|
|---|
| 204 | ImgList.Clear;
|
|---|
| 205 | ImgList.Width := NewWidth;
|
|---|
| 206 | ImgList.Height := NewHeight;
|
|---|
| 207 |
|
|---|
| 208 | for I := 0 to High(Temp) do
|
|---|
| 209 | begin
|
|---|
| 210 | ImgList.Add(Temp[I], nil);
|
|---|
| 211 | Temp[i].Free;
|
|---|
| 212 | end;
|
|---|
| 213 | end;
|
|---|
| 214 |
|
|---|
| 215 | function TScaleDPI.ScaleX(Size: Integer; FromDPI: Integer): Integer;
|
|---|
| 216 | begin
|
|---|
| 217 | Result := MulDiv(Size, DPI.X, FromDPI);
|
|---|
| 218 | end;
|
|---|
| 219 |
|
|---|
| 220 | function TScaleDPI.ScaleY(Size: Integer; FromDPI: Integer): Integer;
|
|---|
| 221 | begin
|
|---|
| 222 | Result := MulDiv(Size, DPI.Y, FromDPI);
|
|---|
| 223 | end;
|
|---|
| 224 |
|
|---|
| 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);
|
|---|
| 235 | end;
|
|---|
| 236 |
|
|---|
| 237 | constructor TScaleDPI.Create(AOwner: TComponent);
|
|---|
| 238 | begin
|
|---|
| 239 | inherited;
|
|---|
| 240 | DPI := Point(96, 96);
|
|---|
| 241 | DesignDPI := Point(96, 96);
|
|---|
| 242 | end;
|
|---|
| 243 |
|
|---|
| 244 | procedure TScaleDPI.ScaleControl(Control: TControl; FromDPI: TPoint);
|
|---|
| 245 | var
|
|---|
| 246 | I: Integer;
|
|---|
| 247 | WinControl: TWinControl;
|
|---|
| 248 | ToolBarControl: TToolBar;
|
|---|
| 249 | OldAnchors: TAnchors;
|
|---|
| 250 | OldAutoSize: Boolean;
|
|---|
| 251 | begin
|
|---|
| 252 | //if Control is TMemo then Exit;
|
|---|
| 253 | //if Control is TForm then
|
|---|
| 254 | // Control.DisableAutoSizing;
|
|---|
| 255 | with Control do begin
|
|---|
| 256 | //OldAutoSize := AutoSize;
|
|---|
| 257 | //AutoSize := False;
|
|---|
| 258 | //Anchors := [];
|
|---|
| 259 | Left := ScaleX(Left, FromDPI.X);
|
|---|
| 260 | Top := ScaleY(Top, FromDPI.Y);
|
|---|
| 261 | //if not (akRight in Anchors) then
|
|---|
| 262 | Width := ScaleX(Width, FromDPI.X);
|
|---|
| 263 | //if not (akBottom in Anchors) then
|
|---|
| 264 | Height := ScaleY(Height, FromDPI.Y);
|
|---|
| 265 | {$IFDEF LCL Qt}
|
|---|
| 266 | Font.Size := 0;
|
|---|
| 267 | {$ELSE}
|
|---|
| 268 | Font.Height := ScaleY(Font.GetTextHeight('Hg'), FromDPI.Y);
|
|---|
| 269 | {$ENDIF}
|
|---|
| 270 | //Anchors := OldAnchors;
|
|---|
| 271 | //AutoSize := OldAutoSize;
|
|---|
| 272 | end;
|
|---|
| 273 |
|
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 | if Control is TToolBar then begin
|
|---|
| 277 | ToolBarControl := TToolBar(Control);
|
|---|
| 278 | with ToolBarControl do begin
|
|---|
| 279 | ButtonWidth := ScaleX(ButtonWidth, FromDPI.X);
|
|---|
| 280 | ButtonHeight := ScaleY(ButtonHeight, FromDPI.Y);
|
|---|
| 281 | end;
|
|---|
| 282 | end;
|
|---|
| 283 |
|
|---|
| 284 | //if not (Control is TCustomPage) then
|
|---|
| 285 | if Control is TWinControl then begin
|
|---|
| 286 | WinControl := TWinControl(Control);
|
|---|
| 287 | if WinControl.ControlCount > 0 then begin
|
|---|
| 288 | for I := 0 to WinControl.ControlCount - 1 do begin
|
|---|
| 289 | if WinControl.Controls[I] is TControl then begin
|
|---|
| 290 | ScaleControl(WinControl.Controls[I], FromDPI);
|
|---|
| 291 | end;
|
|---|
| 292 | end;
|
|---|
| 293 | end;
|
|---|
| 294 | end;
|
|---|
| 295 | //if Control is TForm then
|
|---|
| 296 | // Control.EnableAutoSizing;
|
|---|
| 297 | end;
|
|---|
| 298 |
|
|---|
| 299 | end.
|
|---|