Menu

[327ada]: / Source / VCL / vcl-styles-utils / Common / Vcl.Styles.DPIAware.pas  Maximize  Restore  History

Download this file

367 lines (313 with data), 13.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
{
Modified 20-Mar-2019 by Rickard Johansson (www.rj-texted.se).
Purpose: Add per-monitor DPI awareness
Usage: Add the unit to the interface uses statement of the main form and add code below:
TMyForm = class(TForm)
private
FStyleDPIAwareness : TStyleDPIAwareness;
procedure TFrmMain.FormCreate(Sender: TObject);
begin
FStyleDPIAwareness := TStyleDPIAwareness.Create(Self);
FStyleDPIAwareness.Parent := Self;
procedure TFrmMain.FormDestroy(Sender: TObject);
begin
FStyleDPIAwareness.Free;
procedure TFrmMain.FormAfterMonitorDpiChanged(Sender: TObject; OldDPI, NewDPI: Integer);
begin
FStyleDPIAwareness.AfterDPIChange(OldDPI, NewDPI);
end;
procedure TFrmMain.FormBeforeMonitorDpiChanged(Sender: TObject; OldDPI, NewDPI: Integer);
begin
FStyleDPIAwareness.BeforeDPIChange(OldDPI, NewDPI);
end;
}
{-----------------------------------------------------------------------------
Unit Name: VCL.Styles.DPIAware
Author: PyScripter (https://github.com/pyscripter)
Date: 13-Nov-2017
Purpose: Use VCL Styles in DPI Aware applications by scaling styles
History:
-----------------------------------------------------------------------------}
{
To use the unit just add it to the implementation uses statement of the main form and add
the following code to the FormCreate handler.
procedure TFrmMain.FormCreate(Sender: TObject);
Var
StyleDPIAwareness : TStyleDPIAwareness;
begin
StyleDPIAwareness := TStyleDPIAwareness.Create(Self);
StyleDPIAwareness.Parent := Self;
By default the component scales the styles at multiples of 100%. You can change that,
by adding the line:
StyleDPIAwareness.RoundScalingFactor := False;
With this statement styles are scaled to whatever scaling factor results for Screen.PixelsPerInch.
Most of the styles would work fine, but a few may show some visual defects.
Limitations:
Does not support perMonitor DPI Awareness.
You need to set DPI Awareness to System.
}
unit VCL.Styles.DPIAware;
interface
uses
Winapi.Windows, WinAPI.Messages, System.SysUtils, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Themes, Vcl.Styles;
Type
TStyleDPI = class(TObject)
private
FCurrentDPI: Integer;
public
property CurrentDPI: Integer read FCurrentDPI write FCurrentDPI;
end;
TStyleDPIAwareness = class(TControl)
private
FScaledStyles : TStringList;
FRoundScalingFactor : Boolean;
FUseCustomScalingFactor : Boolean;
FCustomPPI : integer;
FOldDPI: Integer;
protected
procedure CMStyleChanged(var Message: TMessage); message CM_STYLECHANGED;
procedure RecreateForms;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure AfterDPIChange(OldDPI, NewDPI: Integer);
procedure BeforeDPIChange(OldDPI, NewDPI: Integer);
procedure ScaleStyle(Style : TCustomStyleServices);
property OldDPI: Integer read FOldDPI write FOldDPI;
published
property RoundScalingFactor : Boolean read FRoundScalingFactor
write FRoundScalingFactor default True;
property UseCustomScalingFactor : Boolean read FUseCustomScalingFactor
write FUseCustomScalingFactor default False;
property CustomPPI : integer read FCustomPPI write FCustomPPI default 96;
end;
implementation
Uses
System.Rtti, DDetours, System.Math;
{ TStyleDPIAwareness }
procedure ResizeBitmap(Bitmap: TBitmap; const NewWidth, NewHeight: integer);
var
buffer: TBitmap;
begin
buffer := TBitmap.Create;
try
buffer.SetSize(NewWidth, NewHeight);
buffer.Canvas.StretchDraw(Rect(0, 0, NewWidth, NewHeight), Bitmap);
Bitmap.SetSize(NewWidth, NewHeight);
Bitmap.Canvas.Draw(0, 0, buffer);
finally
buffer.Free;
end;
end;
procedure TStyleDPIAwareness.CMStyleChanged(var Message: TMessage);
begin
ScaleStyle(TStyleManager.ActiveStyle);
end;
constructor TStyleDPIAwareness.Create(AOwner: TComponent);
begin
inherited;
FRoundScalingFactor := True;
FUseCustomScalingFactor := False;
FCustomPPI := 96;
FOldDPI := 96;
FScaledStyles := TStringList.Create;
FScaledStyles.Sorted := False;
ScaleStyle(TStyleManager.ActiveStyle);
end;
destructor TStyleDPIAwareness.Destroy;
var
i : Integer;
begin
for i := 0 to FScaledStyles.Count - 1 do
TStyleDPI(FScaledStyles.Objects[i]).Free;
FScaledStyles.Free;
inherited;
end;
procedure TStyleDPIAwareness.AfterDPIChange(OldDPI, NewDPI: Integer);
begin
ScaleStyle(TStyleManager.ActiveStyle);
end;
procedure TStyleDPIAwareness.BeforeDPIChange(OldDPI, NewDPI: Integer);
begin
FOldDPI := OldDPI;
end;
procedure TStyleDPIAwareness.RecreateForms;
Var
i : Integer;
begin
for i := 0 to Screen.FormCount - 1 do
begin
if Screen.Forms[i] <> TForm(Owner) then
Screen.Forms[i].Perform(CM_RECREATEWND, 0, 0);
end;
end;
procedure TStyleDPIAwareness.ScaleStyle(Style: TCustomStyleServices);
Var
NewDPI : integer;
SeStyle : TObject;
SeStyleSource : TObject;
BitmapList : TList;
BitMap : TBitmap;
StyleObjectList : Tlist;
i,n: integer;
StyleObject : TComponent;
obj: TStyleDPI;
procedure ProcessBitmapLink(BL : TObject);
Var
BLType : TRTTIType;
begin
BLType := TRttiContext.Create.GetType(BL.ClassType);
BLType.GetProperty('Bottom').SetValue(BL, Round((BLType.GetProperty('Bottom').GetValue(BL).AsInteger * NewDPI - 1) / OldDPI));
BLType.GetProperty('Right').SetValue(BL, Round(BLType.GetProperty('Right').GetValue(BL).AsInteger * NewDPI / OldDPI));
BLType.GetProperty('Left').SetValue(BL, Round(BLType.GetProperty('Left').GetValue(BL).AsInteger * NewDPI / OldDPI));
BLType.GetProperty('Top').SetValue(BL, Round(BLType.GetProperty('Top').GetValue(BL).AsInteger * NewDPI / OldDPI));
end;
procedure ProcessSO(aSO : TComponent; aSOType : TRTTIType);
begin
aSOType.GetProperty('Top').SetValue(aSO, Round(aSOType.GetProperty('Top').GetValue(aSO).AsInteger * NewDPI / OldDPI));
aSOType.GetProperty('Left').SetValue(aSO, Round(aSOType.GetProperty('Left').GetValue(aSO).AsInteger * NewDPI / OldDPI));
aSOType.GetProperty('Width').SetValue(aSO, Round(aSOType.GetProperty('Width').GetValue(aSO).AsInteger * NewDPI / OldDPI));
aSOType.GetProperty('Height').SetValue(aSO, Round(aSOType.GetProperty('Height').GetValue(aSO).AsInteger * NewDPI / OldDPI));
aSOType.GetProperty('MarginTop').SetValue(aSO, Round(aSOType.GetProperty('MarginTop').GetValue(aSO).AsInteger * NewDPI / OldDPI));
aSOType.GetProperty('MarginLeft').SetValue(aSO, Round(aSOType.GetProperty('MarginLeft').GetValue(aSO).AsInteger * NewDPI / OldDPI));
aSOType.GetProperty('MarginBottom').SetValue(aSO, Round(aSOType.GetProperty('MarginBottom').GetValue(aSO).AsInteger * NewDPI / OldDPI));
aSOType.GetProperty('MarginRight').SetValue(aSO, Round(aSOType.GetProperty('MarginRight').GetValue(aSO).AsInteger * NewDPI / OldDPI));
aSOType.GetProperty('TextMarginTop').SetValue(aSO, Round(aSOType.GetProperty('TextMarginTop').GetValue(aSO).AsInteger * NewDPI / OldDPI));
aSOType.GetProperty('TextMarginLeft').SetValue(aSO, Round(aSOType.GetProperty('TextMarginLeft').GetValue(aSO).AsInteger * NewDPI / OldDPI));
aSOType.GetProperty('TextMarginRight').SetValue(aSO, Round(aSOType.GetProperty('TextMarginRight').GetValue(aSO).AsInteger * NewDPI / OldDPI));
end;
procedure ProcessStyleObject(SO : TComponent);
var
i: integer;
ChildSo : TComponent;
SOType : TRTTIType;
BitmapLink : TObject;
begin
SOType := TRttiContext.Create.GetType(SO.ClassType);
ProcessSO(SO, SOType);
if So.ClassName = 'TSeBitmapObject' then begin
BitmapLink := TRttiContext.Create.GetType(SO.ClassType).GetField('FBitmap').GetValue(SO).AsObject;;
ProcessBitmapLink(BitmapLink);
end;
if So.ClassName = 'TSeActiveBitmap' then begin
BitmapLink := TRttiContext.Create.GetType(SO.ClassType).GetField('FBitmap').GetValue(SO).AsObject;;
ProcessBitmapLink(BitmapLink);
BitmapLink := TRttiContext.Create.GetType(SO.ClassType).GetField('FActiveBitmap').GetValue(SO).AsObject;;
ProcessBitmapLink(BitmapLink);
end;
if So.ClassName = 'TSeSystemButton' then begin
// Shift the form title to the right
if SO.Name = 'btnSysMenu' then
SOType.GetProperty('Width').SetValue(SO, MulDiv(28, NewDPI, OldDPI));
BitmapLink := TRttiContext.Create.GetType(SO.ClassType).GetField('FBitmap').GetValue(SO).AsObject;;
ProcessBitmapLink(BitmapLink);
BitmapLink := TRttiContext.Create.GetType(SO.ClassType).GetField('FActiveBitmap').GetValue(SO).AsObject;;
ProcessBitmapLink(BitmapLink);
BitmapLink := TRttiContext.Create.GetType(SO.ClassType).GetField('FBitmapPressed').GetValue(SO).AsObject;;
ProcessBitmapLink(BitmapLink);
BitmapLink := TRttiContext.Create.GetType(SO.ClassType).GetField('FBitmapHot').GetValue(SO).AsObject;;
ProcessBitmapLink(BitmapLink);
end;
if So.ClassName = 'TSeButtonObject' then begin
BitmapLink := TRttiContext.Create.GetType(SO.ClassType).GetField('FBitmap').GetValue(SO).AsObject;;
ProcessBitmapLink(BitmapLink);
BitmapLink := TRttiContext.Create.GetType(SO.ClassType).GetField('FBitmapFocused').GetValue(SO).AsObject;;
ProcessBitmapLink(BitmapLink);
BitmapLink := TRttiContext.Create.GetType(SO.ClassType).GetField('FBitmapHot').GetValue(SO).AsObject;;
ProcessBitmapLink(BitmapLink);
BitmapLink := TRttiContext.Create.GetType(SO.ClassType).GetField('FBitmapPressed').GetValue(SO).AsObject;;
ProcessBitmapLink(BitmapLink);
BitmapLink := TRttiContext.Create.GetType(SO.ClassType).GetField('FBitmapDisabled').GetValue(SO).AsObject;;
ProcessBitmapLink(BitmapLink);
end;
for i := 0 to SO.ComponentCount - 1 do begin
ChildSo := SO.Components[i];
ProcessStyleObject(ChildSo);
end;
end;
begin
n := FScaledStyles.IndexOf(TStyleManager.ActiveStyle.Name);
if n >= 0 then
begin
obj := TStyleDPI(FScaledStyles.Objects[n]);
OldDPI := obj.FCurrentDPI;
end;
if UseCustomScalingFactor then
NewDPI := CustomPPI
else
NewDPI := TForm(Owner).Monitor.PixelsPerInch;
if (Style = TStyleManager.SystemStyle) then
Exit;
SeStyle := TRttiContext.Create.GetType(Style.ClassType).GetField('FSource').GetValue(Style).AsObject;
SeStyleSource := TRttiContext.Create.GetType(SeStyle.ClassType).GetField('FCleanCopy').GetValue(SeStyle).AsObject;
BitMapList := TRttiContext.Create.GetType(SeStyleSource.ClassType).GetField('FBitmaps').GetValue(SeStyleSource).AsObject as TList;
if BitMapList.Count = 1 then
begin
Bitmap := TObject(BitmapList[0]) as TBitmap;
ResizeBitmap(Bitmap, Round(Bitmap.Width * NewDPI / OldDPI), Round(Bitmap.Height * NewDPI / OldDPI));
StyleObjectList := TRttiContext.Create.GetType(SeStyleSource.ClassType).GetField('FObjects').GetValue(SeStyleSource).AsObject as TList;
for i := 0 to StyleObjectList.Count -1 do begin
StyleObject := TObject(StyleObjectList[i]) as TComponent;
ProcessStyleObject(StyleObject);
end;
TRttiContext.Create.GetType(SeStyle.ClassType).GetMethod('ResetStyle').Invoke(SeStyle, []);
end;
n := FScaledStyles.IndexOf(Style.Name);
if n >= 0 then
begin
obj := TStyleDPI(FScaledStyles.Objects[n]);
obj.FCurrentDPI := NewDPI;
end
else
begin
obj := TStyleDPI.Create;
obj.FCurrentDPI := NewDPI;
FScaledStyles.AddObject(Style.Name, obj);
end;
if Style = TStyleManager.ActiveStyle then
RecreateForms;
end;
{$IFDEF VER330} // RAD Studio 10.3
type
TGetBorderSize = function: TRect of object;
TFormStyleHookFix = class helper for TFormStyleHook
procedure SetStretchedCaptionInc(Value : Integer);
function GetBorderSizeAddr: Pointer;
function Detour_GetBorderSize: TRect;
end;
var
Trampoline_TFormStyleHook_GetBorderSize : TGetBorderSize;
Detour_TFormStyleHook_GetBorderSize : TGetBorderSize;
{ TFormStyleHookFix }
function TFormStyleHookFix.GetBorderSizeAddr: Pointer;
var
MethodPtr: TGetBorderSize;
begin
with Self do MethodPtr := GetBorderSize;
Result := TMethod(MethodPtr).Code;
end;
procedure TFormStyleHookFix.SetStretchedCaptionInc(Value: Integer);
begin
with Self do FStretchedCaptionInc := Value;
end;
function TFormStyleHookFix.Detour_GetBorderSize: TRect;
var
MethodPtr: TGetBorderSize;
begin
TMethod(MethodPtr).Code := TMethod(Trampoline_TFormStyleHook_GetBorderSize).Code;
TMethod(MethodPtr).Data := Pointer(Self);
Result := MethodPtr;
Self.SetStretchedCaptionInc(1);
if (Form.Monitor.PixelsPerInch > 96) then
Result.Top := MulDiv(Result.Top, 96, Form.Monitor.PixelsPerInch);
end;
initialization
Detour_TFormStyleHook_GetBorderSize := TFormStyleHook(nil).Detour_GetBorderSize;
TMethod(Trampoline_TFormStyleHook_GetBorderSize).Code :=
InterceptCreate(TFormStyleHook(nil).GetBorderSizeAddr,
TMethod(Detour_TFormStyleHook_GetBorderSize).Code)
finalization
InterceptRemove(TMethod(Trampoline_TFormStyleHook_GetBorderSize).Code);
{$ENDIF VER330}
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.