1 | unit ScaleDPI;
|
---|
2 |
|
---|
3 | { See: http://wiki.lazarus.freepascal.org/High_DPI }
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, Forms, Graphics, Controls, ComCtrls, LCLType, SysUtils,
|
---|
9 | Generics.Collections;
|
---|
10 |
|
---|
11 | type
|
---|
12 | TControlDimensions = class;
|
---|
13 |
|
---|
14 | { TControlDimension }
|
---|
15 |
|
---|
16 | TControlDimension = class
|
---|
17 | BoundsRect: TRect;
|
---|
18 | FontHeight: Integer;
|
---|
19 | Controls: TControlDimensions;
|
---|
20 | // Class specifics
|
---|
21 | ButtonSize: TPoint; // TToolBar
|
---|
22 | CoolBandWidth: Integer;
|
---|
23 | ConstraintsMin: TPoint; // TForm
|
---|
24 | ConstraintsMax: TPoint; // TForm
|
---|
25 | constructor Create;
|
---|
26 | destructor Destroy; override;
|
---|
27 | end;
|
---|
28 |
|
---|
29 | TControlDimensions = class(TObjectList<TControlDimension>)
|
---|
30 | end;
|
---|
31 |
|
---|
32 | { TScaleDPI }
|
---|
33 |
|
---|
34 | TScaleDPI = class(TComponent)
|
---|
35 | private
|
---|
36 | FAutoDetect: Boolean;
|
---|
37 | FDesignDPI: TPoint;
|
---|
38 | FDPI: TPoint;
|
---|
39 | procedure SetAutoDetect(AValue: Boolean);
|
---|
40 | procedure SetDesignDPI(AValue: TPoint);
|
---|
41 | procedure SetDPI(AValue: TPoint);
|
---|
42 | public
|
---|
43 | procedure StoreDimensions(Control: TControl; Dimensions: TControlDimension);
|
---|
44 | procedure RestoreDimensions(Control: TControl; Dimensions: TControlDimension);
|
---|
45 | procedure ScaleDimensions(Control: TControl; Dimensions: TControlDimension);
|
---|
46 | procedure ApplyToAll(FromDPI: TPoint);
|
---|
47 | procedure ScaleControl(Control: TControl; FromDPI: TPoint);
|
---|
48 | procedure ScaleImageList(ImgList: TImageList; FromDPI: TPoint);
|
---|
49 | function ScalePoint(APoint: TPoint; FromDPI: TPoint): TPoint;
|
---|
50 | function ScaleRect(ARect: TRect; FromDPI: TPoint): TRect;
|
---|
51 | function ScaleX(Size: Integer; FromDPI: Integer): Integer;
|
---|
52 | function ScaleY(Size: Integer; FromDPI: Integer): Integer;
|
---|
53 | constructor Create(AOwner: TComponent); override;
|
---|
54 | property DesignDPI: TPoint read FDesignDPI write SetDesignDPI;
|
---|
55 | property DPI: TPoint read FDPI write SetDPI;
|
---|
56 | published
|
---|
57 | property AutoDetect: Boolean read FAutoDetect write SetAutoDetect;
|
---|
58 | end;
|
---|
59 |
|
---|
60 | procedure Register;
|
---|
61 |
|
---|
62 |
|
---|
63 | implementation
|
---|
64 |
|
---|
65 | resourcestring
|
---|
66 | SWrongDPI = 'Wrong DPI [%d,%d]';
|
---|
67 |
|
---|
68 | procedure Register;
|
---|
69 | begin
|
---|
70 | RegisterComponents('Common', [TScaleDPI]);
|
---|
71 | end;
|
---|
72 |
|
---|
73 | { TControlDimension }
|
---|
74 |
|
---|
75 | constructor TControlDimension.Create;
|
---|
76 | begin
|
---|
77 | Controls := TControlDimensions.Create;
|
---|
78 | end;
|
---|
79 |
|
---|
80 | destructor TControlDimension.Destroy;
|
---|
81 | begin
|
---|
82 | FreeAndNil(Controls);
|
---|
83 | inherited;
|
---|
84 | end;
|
---|
85 |
|
---|
86 | procedure TScaleDPI.SetAutoDetect(AValue: Boolean);
|
---|
87 | begin
|
---|
88 | if FAutoDetect = AValue then Exit;
|
---|
89 | FAutoDetect := AValue;
|
---|
90 | if AValue then begin
|
---|
91 | DPI := Point(ScreenInfo.PixelsPerInchX, ScreenInfo.PixelsPerInchY);
|
---|
92 | end;
|
---|
93 | end;
|
---|
94 |
|
---|
95 | procedure TScaleDPI.SetDesignDPI(AValue: TPoint);
|
---|
96 | begin
|
---|
97 | if (FDesignDPI.X = AValue.X) and (FDesignDPI.Y = AValue.Y) then Exit;
|
---|
98 | if (AValue.X <= 0) or (AValue.Y <= 0) then
|
---|
99 | raise Exception.Create(Format(SWrongDPI, [AValue.X, AValue.Y]));
|
---|
100 | FDesignDPI := AValue;
|
---|
101 | end;
|
---|
102 |
|
---|
103 | procedure TScaleDPI.SetDPI(AValue: TPoint);
|
---|
104 | begin
|
---|
105 | if (FDPI.X = AValue.X) and (FDPI.Y = AValue.Y) then Exit;
|
---|
106 | if (AValue.X <= 0) or (AValue.Y <= 0) then
|
---|
107 | raise Exception.Create(Format(SWrongDPI, [AValue.X, AValue.Y]));
|
---|
108 | FDPI := AValue;
|
---|
109 | end;
|
---|
110 |
|
---|
111 | procedure TScaleDPI.StoreDimensions(Control: TControl;
|
---|
112 | Dimensions: TControlDimension);
|
---|
113 | var
|
---|
114 | NewControl: TControlDimension;
|
---|
115 | I: Integer;
|
---|
116 | begin
|
---|
117 | Dimensions.BoundsRect := Control.BoundsRect;
|
---|
118 | Dimensions.FontHeight := Control.Font.GetTextHeight('Hg');
|
---|
119 | Dimensions.Controls.Clear;
|
---|
120 | if Control is TToolBar then
|
---|
121 | Dimensions.ButtonSize := Point(TToolBar(Control).ButtonWidth, TToolBar(Control).ButtonHeight);
|
---|
122 | if Control is TForm then begin
|
---|
123 | Dimensions.ConstraintsMin := Point(TForm(Control).Constraints.MinWidth,
|
---|
124 | TForm(Control).Constraints.MinHeight);
|
---|
125 | Dimensions.ConstraintsMax := Point(TForm(Control).Constraints.MaxWidth,
|
---|
126 | TForm(Control).Constraints.MaxHeight);
|
---|
127 | end;
|
---|
128 | if Control is TWinControl then
|
---|
129 | for I := 0 to TWinControl(Control).ControlCount - 1 do begin
|
---|
130 | if TWinControl(Control).Controls[I] is TControl then
|
---|
131 | // Do not scale docked forms twice
|
---|
132 | if not (TWinControl(Control).Controls[I] is TForm) then begin
|
---|
133 | NewControl := TControlDimension.Create;
|
---|
134 | Dimensions.Controls.Add(NewControl);
|
---|
135 | StoreDimensions(TWinControl(Control).Controls[I], NewControl);
|
---|
136 | end;
|
---|
137 | end;
|
---|
138 | end;
|
---|
139 |
|
---|
140 | procedure TScaleDPI.RestoreDimensions(Control: TControl;
|
---|
141 | Dimensions: TControlDimension);
|
---|
142 | var
|
---|
143 | I: Integer;
|
---|
144 | begin
|
---|
145 | Control.BoundsRect := Dimensions.BoundsRect;
|
---|
146 | Control.Font.Height := Dimensions.FontHeight;
|
---|
147 | if Control is TToolBar then begin
|
---|
148 | TToolBar(Control).ButtonWidth := Dimensions.ButtonSize.X;
|
---|
149 | TToolBar(Control).ButtonHeight := Dimensions.ButtonSize.Y;
|
---|
150 | end;
|
---|
151 | if Control is TForm then begin
|
---|
152 | TForm(Control).Constraints.MinWidth := Dimensions.ConstraintsMin.X;
|
---|
153 | TForm(Control).Constraints.MinHeight := Dimensions.ConstraintsMin.Y;
|
---|
154 | TForm(Control).Constraints.MaxWidth := Dimensions.ConstraintsMax.X;
|
---|
155 | TForm(Control).Constraints.MaxHeight := Dimensions.ConstraintsMax.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
|
---|
160 | // Do not scale docked forms twice
|
---|
161 | if not (TWinControl(Control).Controls[I] is TForm) then begin
|
---|
162 | RestoreDimensions(TWinControl(Control).Controls[I], TControlDimension(Dimensions.Controls[I]));
|
---|
163 | end;
|
---|
164 | end;
|
---|
165 | end;
|
---|
166 |
|
---|
167 | procedure TScaleDPI.ScaleDimensions(Control: TControl;
|
---|
168 | Dimensions: TControlDimension);
|
---|
169 | var
|
---|
170 | I: Integer;
|
---|
171 | begin
|
---|
172 | Control.BoundsRect := ScaleRect(Dimensions.BoundsRect, DesignDPI);
|
---|
173 | Control.Font.Height := ScaleY(Dimensions.FontHeight, DesignDPI.Y);
|
---|
174 | if Control is TToolBar then begin
|
---|
175 | TToolBar(Control).ButtonWidth := ScaleX(Dimensions.ButtonSize.X, DesignDPI.X);
|
---|
176 | TToolBar(Control).ButtonHeight := ScaleY(Dimensions.ButtonSize.Y, DesignDPI.Y);
|
---|
177 | end;
|
---|
178 | if Control is TCoolBar then begin
|
---|
179 | with TCoolBar(Control) do
|
---|
180 | for I := 0 to Bands.Count - 1 do
|
---|
181 | with TCoolBand(Bands[I]) do begin
|
---|
182 | MinWidth := ScaleX(Dimensions.ButtonSize.X, DesignDPI.X);
|
---|
183 | MinHeight := ScaleY(Dimensions.ButtonSize.Y, DesignDPI.Y);
|
---|
184 | //Width := ScaleX(Dimensions.BoundsRect.Left -
|
---|
185 | end;
|
---|
186 | end;
|
---|
187 | if Control is TForm then begin
|
---|
188 | TForm(Control).Constraints.MinWidth := ScaleX(Dimensions.ConstraintsMin.X, DesignDPI.X);
|
---|
189 | TForm(Control).Constraints.MaxWidth := ScaleX(Dimensions.ConstraintsMax.X, DesignDPI.X);
|
---|
190 | TForm(Control).Constraints.MinHeight := ScaleY(Dimensions.ConstraintsMin.Y, DesignDPI.Y);
|
---|
191 | TForm(Control).Constraints.MaxHeight := ScaleY(Dimensions.ConstraintsMax.Y, DesignDPI.Y);
|
---|
192 | end;
|
---|
193 | if Control is TWinControl then
|
---|
194 | for I := 0 to TWinControl(Control).ControlCount - 1 do begin
|
---|
195 | if TWinControl(Control).Controls[I] is TControl then
|
---|
196 | // Do not scale docked forms twice
|
---|
197 | if not (TWinControl(Control).Controls[I] is TForm) then begin
|
---|
198 | ScaleDimensions(TWinControl(Control).Controls[I], TControlDimension(Dimensions.Controls[I]));
|
---|
199 | end;
|
---|
200 | end;
|
---|
201 | end;
|
---|
202 |
|
---|
203 | procedure TScaleDPI.ApplyToAll(FromDPI: TPoint);
|
---|
204 | var
|
---|
205 | I: Integer;
|
---|
206 | begin
|
---|
207 | for I := 0 to Screen.FormCount - 1 do begin
|
---|
208 | ScaleControl(Screen.Forms[I], FromDPI);
|
---|
209 | end;
|
---|
210 | end;
|
---|
211 |
|
---|
212 | procedure TScaleDPI.ScaleImageList(ImgList: TImageList; FromDPI: TPoint);
|
---|
213 | var
|
---|
214 | TempBmp: TBitmap;
|
---|
215 | Temp: array of TBitmap;
|
---|
216 | NewWidth: Integer;
|
---|
217 | NewHeight: Integer;
|
---|
218 | I: Integer;
|
---|
219 | begin
|
---|
220 | ImgList.BeginUpdate;
|
---|
221 | try
|
---|
222 | NewWidth := ScaleX(ImgList.Width, FromDPI.X);
|
---|
223 | NewHeight := ScaleY(ImgList.Height, FromDPI.Y);
|
---|
224 |
|
---|
225 | Temp := nil;
|
---|
226 | SetLength(Temp, ImgList.Count);
|
---|
227 | for I := 0 to ImgList.Count - 1 do
|
---|
228 | begin
|
---|
229 | TempBmp := TBitmap.Create;
|
---|
230 | try
|
---|
231 | TempBmp.PixelFormat := pf32bit;
|
---|
232 | ImgList.GetBitmap(I, TempBmp);
|
---|
233 | Temp[I] := TBitmap.Create;
|
---|
234 | Temp[I].SetSize(NewWidth, NewHeight);
|
---|
235 | {$IFDEF UNIX}
|
---|
236 | Temp[I].PixelFormat := pf24bit;
|
---|
237 | {$ELSE}
|
---|
238 | Temp[I].PixelFormat := pf32bit;
|
---|
239 | {$ENDIF}
|
---|
240 | Temp[I].TransparentColor := TempBmp.TransparentColor;
|
---|
241 | //Temp[I].TransparentMode := TempBmp.TransparentMode;
|
---|
242 | Temp[I].Transparent := True;
|
---|
243 | Temp[I].Canvas.Brush.Style := bsSolid;
|
---|
244 | Temp[I].Canvas.Brush.Color := Temp[I].TransparentColor;
|
---|
245 | Temp[I].Canvas.FillRect(0, 0, Temp[I].Width, Temp[I].Height);
|
---|
246 |
|
---|
247 | if (Temp[I].Width = 0) or (Temp[I].Height = 0) then Continue;
|
---|
248 | Temp[I].Canvas.StretchDraw(Rect(0, 0, Temp[I].Width, Temp[I].Height), TempBmp);
|
---|
249 | finally
|
---|
250 | TempBmp.Free;
|
---|
251 | end;
|
---|
252 | end;
|
---|
253 |
|
---|
254 | ImgList.Clear;
|
---|
255 | ImgList.Width := NewWidth;
|
---|
256 | ImgList.Height := NewHeight;
|
---|
257 |
|
---|
258 | for I := 0 to High(Temp) do
|
---|
259 | begin
|
---|
260 | ImgList.Add(Temp[I], nil);
|
---|
261 | Temp[i].Free;
|
---|
262 | end;
|
---|
263 | finally
|
---|
264 | ImgList.EndUpdate;
|
---|
265 | end;
|
---|
266 | end;
|
---|
267 |
|
---|
268 | function TScaleDPI.ScaleX(Size: Integer; FromDPI: Integer): Integer;
|
---|
269 | begin
|
---|
270 | Result := MulDiv(Size, DPI.X, FromDPI);
|
---|
271 | end;
|
---|
272 |
|
---|
273 | function TScaleDPI.ScaleY(Size: Integer; FromDPI: Integer): Integer;
|
---|
274 | begin
|
---|
275 | Result := MulDiv(Size, DPI.Y, FromDPI);
|
---|
276 | end;
|
---|
277 |
|
---|
278 | function TScaleDPI.ScalePoint(APoint: TPoint; FromDPI: TPoint): TPoint;
|
---|
279 | begin
|
---|
280 | Result.X := ScaleX(APoint.X, FromDPI.X);
|
---|
281 | Result.Y := ScaleY(APoint.Y, FromDPI.Y);
|
---|
282 | end;
|
---|
283 |
|
---|
284 | function TScaleDPI.ScaleRect(ARect: TRect; FromDPI: TPoint): TRect;
|
---|
285 | begin
|
---|
286 | Result.TopLeft := ScalePoint(ARect.TopLeft, FromDPI);
|
---|
287 | Result.BottomRight := ScalePoint(ARect.BottomRight, FromDPI);
|
---|
288 | end;
|
---|
289 |
|
---|
290 | constructor TScaleDPI.Create(AOwner: TComponent);
|
---|
291 | begin
|
---|
292 | inherited;
|
---|
293 | DPI := Point(96, 96);
|
---|
294 | DesignDPI := Point(96, 96);
|
---|
295 | end;
|
---|
296 |
|
---|
297 | procedure TScaleDPI.ScaleControl(Control: TControl; FromDPI: TPoint);
|
---|
298 | var
|
---|
299 | I: Integer;
|
---|
300 | WinControl: TWinControl;
|
---|
301 | ToolBarControl: TToolBar;
|
---|
302 | //OldAnchors: TAnchors;
|
---|
303 | //OldAutoSize: Boolean;
|
---|
304 | begin
|
---|
305 | //if not (Control is TCustomPage) then
|
---|
306 | // Resize childs first
|
---|
307 | if Control is TWinControl then begin
|
---|
308 | WinControl := TWinControl(Control);
|
---|
309 | if WinControl.ControlCount > 0 then begin
|
---|
310 | for I := 0 to WinControl.ControlCount - 1 do begin
|
---|
311 | if WinControl.Controls[I] is TControl then begin
|
---|
312 | ScaleControl(WinControl.Controls[I], FromDPI);
|
---|
313 | end;
|
---|
314 | end;
|
---|
315 | end;
|
---|
316 | end;
|
---|
317 |
|
---|
318 | //if Control is TMemo then Exit;
|
---|
319 | //if Control is TForm then
|
---|
320 | // Control.DisableAutoSizing;
|
---|
321 | with Control do begin
|
---|
322 | //OldAutoSize := AutoSize;
|
---|
323 | //AutoSize := False;
|
---|
324 | //Anchors := [];
|
---|
325 | Left := ScaleX(Left, FromDPI.X);
|
---|
326 | Top := ScaleY(Top, FromDPI.Y);
|
---|
327 | //if not (akRight in Anchors) then
|
---|
328 | Width := ScaleX(Width, FromDPI.X);
|
---|
329 | //if not (akBottom in Anchors) then
|
---|
330 | Height := ScaleY(Height, FromDPI.Y);
|
---|
331 | {$IFDEF LCL Qt}
|
---|
332 | Font.Size := 0;
|
---|
333 | {$ELSE}
|
---|
334 | Font.Height := ScaleY(Font.GetTextHeight('Hg'), FromDPI.Y);
|
---|
335 | {$ENDIF}
|
---|
336 | //Anchors := OldAnchors;
|
---|
337 | //AutoSize := OldAutoSize;
|
---|
338 | end;
|
---|
339 |
|
---|
340 | if Control is TCoolBar then
|
---|
341 | with TCoolBar(Control) do begin
|
---|
342 | BeginUpdate;
|
---|
343 | try
|
---|
344 | for I := 0 to Bands.Count - 1 do
|
---|
345 | with Bands[I] do begin
|
---|
346 | MinWidth := ScaleX(MinWidth, FromDPI.X);
|
---|
347 | MinHeight := ScaleY(MinHeight, FromDPI.Y);
|
---|
348 | // Workaround to bad band width auto sizing
|
---|
349 | //Width := ScaleX(Width, FromDPI.X);
|
---|
350 | Width := ScaleX(Control.Width + 28, FromDPI.X);
|
---|
351 | //Control.Invalidate;
|
---|
352 | end;
|
---|
353 | // Workaround for bad autosizing of coolbar
|
---|
354 | if AutoSize then begin
|
---|
355 | AutoSize := False;
|
---|
356 | Height := ScaleY(Height, FromDPI.Y);
|
---|
357 | AutoSize := True;
|
---|
358 | end;
|
---|
359 | finally
|
---|
360 | EndUpdate;
|
---|
361 | end;
|
---|
362 | end;
|
---|
363 |
|
---|
364 | if Control is TToolBar then begin
|
---|
365 | ToolBarControl := TToolBar(Control);
|
---|
366 | with ToolBarControl do begin
|
---|
367 | ButtonWidth := ScaleX(ButtonWidth, FromDPI.X);
|
---|
368 | ButtonHeight := ScaleY(ButtonHeight, FromDPI.Y);
|
---|
369 | end;
|
---|
370 | end;
|
---|
371 |
|
---|
372 | //if Control is TForm then
|
---|
373 | // Control.EnableAutoSizing;
|
---|
374 | end;
|
---|
375 |
|
---|
376 | end.
|
---|