This repository has been archived by the owner on Mar 5, 2024. It is now read-only.
forked from cheat-engine/cheat-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDPIHelper.pas
executable file
·249 lines (204 loc) · 6.14 KB
/
DPIHelper.pas
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
unit DPIHelper;
{$mode objfpc}{$H+}
interface
uses
{$ifdef darwin}
macport, math,
{$endif}
{$ifdef windows}
Windows,
{$endif}Classes, controls, comctrls, SysUtils, Buttons, Graphics, forms, StdCtrls, betterControls;
procedure AdjustSpeedButtonSize(sb: TCustomSpeedButton);
procedure AdjustToolbar(tb: TToolbar);
procedure AdjustImageList(il: TImageList);
procedure AdjustComboboxSize(cb: TComboBox; canvas: TCanvas);
procedure AdjustEditBoxSize(editbox: TCustomEdit; mintextwidth: integer);
function GetEditBoxMargins(editbox: TCustomEdit): integer;
function getDPIScaleFactor: single;
implementation
uses globals{$ifdef windows}, win32proc{$endif};
const
designtimedpi=96;
function getDPIScaleFactor: single;
begin
result:=screen.PixelsPerInch/designtimedpi;
end;
function GetEditBoxMargins(editbox: TCustomEdit): integer;
var m: dword;
begin
{$ifdef windows}
if WindowsVersion>=wvVista then
m:=sendmessage(editbox.Handle, EM_GETMARGINS, 0,0)
else
{$endif}
m:=10;
result:=(m shr 16)+(m and $ffff);
end;
procedure AdjustEditBoxSize(editbox: TCustomEdit; mintextwidth: integer);
var marginsize: integer;
begin
marginsize:=GetEditBoxMargins(editbox);
editbox.clientwidth:=mintextwidth+marginsize;
end;
procedure AdjustComboboxSize(cb: TComboBox; canvas: TCanvas);
var
{$ifdef windows}
cbi: TComboboxInfo;
{$endif}
i: integer;
s: string;
maxwidth: integer;
w: integer;
begin
maxwidth:=0;
for i:=0 to cb.Items.Count-1 do
begin
s:=cb.Items[i];
maxwidth:=max(maxwidth, Canvas.TextWidth(s));
end;
{$ifdef windows}
cbi.cbSize:=sizeof(cbi);
if GetComboBoxInfo(cb.Handle, @cbi) then
begin
i:=maxwidth-(cbi.rcItem.Right-cbi.rcItem.Left)+4;
w:=cb.width+i;
end
else
{$endif}
w:=maxwidth+16;
cb.width:=w;
cb.Constraints.MinWidth:=w;
end;
procedure ScaleImageList(ImgList: TImageList; NewWidth, NewHeight: Integer); //from http://wiki.freepascal.org/TImageList
var
TempImgList: TImageList;
TempBmp1: TBitmap;
TempBmp2: TBitmap;
I: Integer;
begin
TempImgList := TImageList.Create(nil);
TempBmp1 := TBitmap.Create;
TempBmp1.PixelFormat := pf32bit;
TempBmp2 := TBitmap.Create;
TempBmp2.PixelFormat := pf32bit;
TempBmp2.SetSize(NewWidth, NewHeight);
try
TempImgList.Width := NewWidth;
TempImgList.Height := NewHeight;
for I := 0 to ImgList.Count - 1 do begin
// Load image for given index to temporary bitmap
ImgList.GetBitmap(I, TempBmp1);
// Clear transparent image background
TempBmp2.Canvas.Brush.Style := bsSolid;
TempBmp2.Canvas.Brush.Color := TempBmp2.TransparentColor;
TempBmp2.Canvas.FillRect(0, 0, TempBmp2.Width, TempBmp2.Height);
// Stretch image to new size
TempBmp2.Canvas.StretchDraw(Rect(0, 0, TempBmp2.Width, TempBmp2.Height), TempBmp1);
TempImgList.Add(TempBmp2, nil);
end;
ImgList.Assign(TempImgList);
finally
TempImgList.Free;
TempBmp1.Free;
TempBmp2.Free;
end;
end;
procedure AdjustImageList(il: TImageList);
begin
if (screen.PixelsPerInch<>designtimedpi) then
begin
ScaleImageList(il, scalex(il.Height, designtimedpi), scaley(il.width,designtimedpi));
end
else
begin
ScaleImageList(il, trunc(il.Height*fontmultiplication), trunc(il.Width*fontmultiplication));
end;
end;
procedure AdjustToolbar(tb: TToolbar);
var
i: integer;
originalbm: TBitmap;
bm: Tbitmap;
begin
if (fontmultiplication>1.0) or (screen.PixelsPerInch<>designtimedpi) then
begin
if (screen.PixelsPerInch<>designtimedpi) then
begin
tb.ButtonHeight:=scalex(tb.ButtonHeight, designtimedpi);
ScaleImageList(timagelist(tb.Images), scalex(tb.Images.Height, designtimedpi), scaley(tb.images.Width, designtimedpi));
end
else
begin
tb.ButtonHeight:=trunc(tb.ButtonHeight*fontmultiplication);
ScaleImageList(timagelist(tb.Images), trunc(tb.Images.Height*fontmultiplication), trunc(tb.images.Width*fontmultiplication));
end;
{
for i:=0 to tb.images.Count-1 do
begin
originalbm:=TBitmap.Create;
tb.images.GetBitmap(i,originalbm);
originalbm.Transparent:=false;
originalbm.SaveToFile('d:\bla.bmp');
bm:=Tbitmap.create;
bm.Assign(originalbm);
if (screen.PixelsPerInch<>designtimedpi) then
begin
bm.width:=scalex(originalbm.Width, designtimedpi);
bm.height:=scaley(originalbm.Height, designtimedpi);
end
else
begin
bm.width:=trunc(originalbm.width*fontmultiplication);
bm.height:=trunc(originalbm.height*fontmultiplication);
end;
bm.SaveToFile('d:\bla1.5.bmp');
bm.Canvas.StretchDraw(rect(0,0, bm.width, bm.height),originalbm);
bm.SaveToFile('d:\bla2.bmp');
tb.Images.Replace(i,bm,nil);
end; }
end;
end;
procedure AdjustSpeedButtonSize(sb: TCustomSpeedButton);
// designtimedpi=50;
var
bm: TBitmap;
ng: integer;
begin
if (fontmultiplication>1.0) or (screen.PixelsPerInch<>designtimedpi) then
begin
sb.Transparent:=false;
sb.Glyph.Transparent:=false;
ng:=sb.NumGlyphs;
bm:=TBitmap.Create;
bm.Assign(sb.Glyph);
if (screen.PixelsPerInch<>designtimedpi) then
begin
bm.width:=scalex(sb.Glyph.Width, designtimedpi);
bm.height:=scaley(sb.Glyph.Height, designtimedpi);
end
else
begin
bm.width:=trunc(bm.width*fontmultiplication);
bm.height:=trunc(bm.height*fontmultiplication);
end;
bm.Canvas.StretchDraw(rect(0,0, bm.width, bm.height),sb.Glyph);
if (screen.PixelsPerInch<>designtimedpi) then
begin
sb.Width:=scalex(sb.Width, designtimedpi);
sb.Height:=scaley(sb.Height, designtimedpi);
end
else
begin
sb.width:=trunc(sb.width*fontmultiplication);
sb.height:=trunc(sb.height*fontmultiplication);
end;
bm.TransparentColor:=0;
bm.TransparentMode:=tmAuto;
sb.Glyph:=bm;
sb.Glyph.Transparent:=true;
sb.NumGlyphs:=ng;
sb.Transparent:=true;
bm.free;
end;
end;
end.