-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDelphiWindowStyle.Core.Win.pas
More file actions
418 lines (346 loc) · 13.2 KB
/
Copy pathDelphiWindowStyle.Core.Win.pas
File metadata and controls
418 lines (346 loc) · 13.2 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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
unit DelphiWindowStyle.Core.Win;
interface
uses
Winapi.Windows, Winapi.DwmApi, Winapi.UxTheme, System.UITypes,
DelphiWindowStyle.Types, WinApi.UI.Composition;
{$SCOPEDENUMS ON}
{$ALIGN ON}
{$MINENUMSIZE 4}
{$WARN SYMBOL_PLATFORM OFF}
function SetSystemBackdropType(Handle: THandle; const Value: TSystemBackdropType): Boolean;
function SetExtendFrameIntoClientArea(Handle: THandle; const Value: TRect): Boolean;
function SetWindowCaptionColor(Handle: THandle; const Value: TColor): Boolean;
function SetWindowTextColor(Handle: THandle; const Value: TColor): Boolean;
function SetWindowBorderColor(Handle: THandle; const Value: TColor): Boolean;
function SetWindowCorner(Handle: THandle; const Value: TWindowCornerPreference): Boolean;
function SetWindowColorModeAsSystem(Handle: THandle): Boolean;
function SetWindowColorMode(Handle: THandle; const IsDark: Boolean): Boolean;
procedure TestFuncs(Handle: THandle);
procedure AnimateWindow(Handle: THandle; Time: Cardinal; Animate: Cardinal);
//
procedure RefreshTitleBarThemeColor(Handle: THandle);
function IsHighContrast: Boolean;
function SystemIsDarkMode: Boolean;
function IsDarkModeAllowedForWindow(Handle: THandle): Boolean;
function GetIsImmersiveColorUsingHighContrast(Mode: TImmersiveHCCacheMode): Boolean;
function ShouldAppsUseDarkMode: Boolean;
function SetAccentPolicy(Handle: THandle; GradientColor: TColor): Boolean;
//
function ImmersiveDarkMode: TDwmWindowAttribute;
function AllowDarkModeForWindow(Handle: THandle; Allow: Boolean): Boolean;
procedure AllowDarkModeForApp(Allow: Boolean);
function GetAdjustWindowRect(Handle: THandle): TRect;
function GetAeroColor: TAlphaColor;
implementation
uses
System.Types, System.Math, System.Classes, Winapi.CommCtrl, Winapi.Messages,
System.SysUtils, System.Win.Registry;
function GetAeroColor: TAlphaColor;
var
OpaqueBlend: Bool;
AColor: DWord;
//A, R, G, B: Integer;
OSInfo: TOSVersionInfo;
begin
ZeroMemory(@OSInfo, SizeOf(OSInfo));
OSInfo.dwOSVersionInfoSize := SizeOf(TOSVERSIONINFO);
if (((not GetVersionEx(OSInfo)) and (OSInfo.dwPlatformId <> VER_PLATFORM_WIN32_NT) and (OSInfo.dwMajorVersion < 5))) or (Winapi.Dwmapi.DwmGetColorizationColor(AColor, OpaqueBlend) = S_FALSE) then
begin
Result := TColors.SysNone;
Exit;
end; {
A := (AColor and $FF000000) shr 24;
R := (AColor and $00FF0000) shr 16;
G := (AColor and $0000FF00) shr 8;
B := (AColor and $000000FF); }
Result := AColor;
end;
function CheckPerMonitorV2SupportForWindow(AHandle: HWnd): Boolean;
begin
Result := (AHandle <> 0) and (TOSVersion.Major >= 10) and (TOSVersion.Build >= 14393) and
AreDpiAwarenessContextsEqual(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2, GetWindowDpiAwarenessContext(AHandle));
end;
function AdjustWindowRectExForWindow(var lpRect: TRect; dwStyle: DWORD; bMenu: BOOL; dwExStyle: DWORD; AHandle: HWnd): Boolean;
begin
if CheckPerMonitorV2SupportForWindow(AHandle) then
Result := AdjustWindowRectExForDpi(lpRect, dwStyle, bMenu, dwExStyle, GetDPIForWindow(AHandle))
else
Result := AdjustWindowRectEx(lpRect, dwStyle, bMenu, dwExStyle);
end;
function GetAdjustWindowRect(Handle: THandle): TRect;
var
dwStyle, dwExStyle: DWORD;
begin
Result := TRect.Empty;
dwStyle := GetWindowLong(Handle, GWL_STYLE);
dwExStyle := GetWindowLong(Handle, GWL_EXSTYLE);
{$IF CompilerVersion < 34}
AdjustWindowRectEx(Result, dwStyle, False, dwExStyle);
{$ELSE}
AdjustWindowRectExForWindow(Result, dwStyle, False, dwExStyle, Handle);
{$ENDIF}
end;
procedure AnimateWindow(Handle: THandle; Time: Cardinal; Animate: Cardinal);
begin
Winapi.Windows.AnimateWindow(Handle, Time, Animate);
end;
function SetAccentPolicy(Handle: THandle; GradientColor: TColor): Boolean;
const
//
ACCENT_ENABLE_BLURBEHIND = 3;
ACCENT_ENABLE_ACRYLICBLURBEHIND = 4;
//
DrawLeftBorder = $20;
DrawTopBorder = $40;
DrawRightBorder = $80;
DrawBottomBorder = $100;
var
CompAttrData: TWindowCompositionAttribData;
Accent: TAccentPolicy;
var
SetWindowCompositionAttribute: function(Wnd: hWnd; const AttrData: TWindowCompositionAttribData): BOOL; stdcall;
begin
Result := False;
var DWM := LoadLibrary('user32.dll');
try
@SetWindowCompositionAttribute := GetProcAddress(DWM, 'SetWindowCompositionAttribute');
if @SetWindowCompositionAttribute <> nil then
begin
if GradientColor <> TColorRec.Null then
begin
Accent.GradientColor := GradientColor;
Accent.AccentState := ACCENT_ENABLE_ACRYLICBLURBEHIND;
end
else
begin
Accent.AccentState := ACCENT_ENABLE_BLURBEHIND;
end;
Accent.AccentFlags := DrawLeftBorder or DrawTopBorder or DrawRightBorder or DrawBottomBorder;
CompAttrData.Attrib := TWindowCompositionAttribute.WCA_ACCENT_POLICY;
CompAttrData.cbData := SizeOf(TAccentPolicy);
CompAttrData.pvData := @Accent;
Result := SetWindowCompositionAttribute(Handle, CompAttrData);
end;
finally
FreeLibrary(DWM);
end;
if Result then
SetWindowCorner(Handle, TWindowCornerPreference.DWMWCP_ROUND);
end;
procedure TestFuncs(Handle: THandle);
begin
// EnableBlur(Handle);
// COLORREF dwColorBorder = MenuManager::g_bIsDarkMode ? WIN11_POPUP_BORDER_DARK : WIN11_POPUP_BORDER_LIGHT;
// DwmSetWindowAttribute(hWnd, DWMWA_BORDER_COLOR, &dwColorBorder, sizeof(COLORREF));
end;
function SetSystemBackdropType(Handle: THandle; const Value: TSystemBackdropType): Boolean;
begin
Result := Succeeded(DwmSetWindowAttribute(Handle, Ord(TDwmWindowAttribute.DWMWA_SYSTEMBACKDROP_TYPE), @Value, SizeOf(Integer)));
end;
function SetExtendFrameIntoClientArea(Handle: THandle; const Value: TRect): Boolean;
begin
var Margins: TMargins;
Margins.cxLeftWidth := Value.Left;
Margins.cxRightWidth := Value.Right;
Margins.cyTopHeight := Value.Top;
Margins.cyBottomHeight := Value.Bottom;
Result := Succeeded(DwmExtendFrameIntoClientArea(Handle, Margins));
end;
function SetWindowCaptionColor(Handle: THandle; const Value: TColor): Boolean;
begin
var V: Int64 := Value;
if V = TColors.SysNone then
V := DWMWA_COLOR_NONE;
Result := Succeeded(DwmSetWindowAttribute(Handle, Ord(TDwmWindowAttribute.DWMWA_CAPTION_COLOR), @V, SizeOf(TColorRef)));
end;
function SetWindowTextColor(Handle: THandle; const Value: TColor): Boolean;
begin
Result := Succeeded(DwmSetWindowAttribute(Handle, Ord(TDwmWindowAttribute.DWMWA_TEXT_COLOR), @Value, SizeOf(TColorRef)));
end;
function SetWindowBorderColor(Handle: THandle; const Value: TColor): Boolean;
begin
Result := Succeeded(DwmSetWindowAttribute(Handle, Ord(TDwmWindowAttribute.DWMWA_BORDER_COLOR), @Value, SizeOf(TColorRef)));
end;
function SetWindowCorner(Handle: THandle; const Value: TWindowCornerPreference): Boolean;
begin
Result := Succeeded(DwmSetWindowAttribute(Handle, Ord(TDwmWindowAttribute.DWMWA_WINDOW_CORNER_PREFERENCE), @Value, SizeOf(Integer)));
end;
function SetWindowColorModeAsSystem(Handle: THandle): Boolean;
begin
Result := SetWindowColorMode(Handle, SystemIsDarkMode);
end;
function SetWindowColorMode(Handle: THandle; const IsDark: Boolean): Boolean;
begin
var Value: LongBool := IsDark;
Result := Succeeded(DwmSetWindowAttribute(Handle, Ord(ImmersiveDarkMode), @Value, SizeOf(Value)));
if Result then
begin
AllowDarkModeForWindow(Handle, IsDark);
AllowDarkModeForApp(IsDark);
end;
end;
function SystemIsDarkMode: Boolean;
var
LRegistry: TRegistry;
begin
Result := False;
try
LRegistry := TRegistry.Create;
try
LRegistry.RootKey := HKEY_CURRENT_USER;
if LRegistry.OpenKeyReadOnly('\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize') then
Result := not LRegistry.ReadBool('AppsUseLightTheme');
finally
LRegistry.Free;
end;
except
Result := False;
end;
end;
function IsHighContrast: Boolean;
var
Info: HIGHCONTRASTW;
begin
Info.cbSize := SizeOf(Info);
if SystemParametersInfo(SPI_GETHIGHCONTRAST, SizeOf(Info), @Info, Ord(False)) then
Result := Info.dwFlags and HCF_HIGHCONTRASTON <> 0
else
Result := False;
end;
//
var
WinAllowDarkModeForApp: function(Allow: BOOL): BOOL; stdcall;
WinAllowDarkModeForWindow: function(hWnd: hWnd; Allow: BOOL): BOOL; stdcall;
WinGetIsImmersiveColorUsingHighContrast: function(Mode: TImmersiveHCCacheMode): BOOL; stdcall;
WinIsDarkModeAllowedForWindow: function(hWnd: hWnd): BOOL; stdcall;
WinRefreshImmersiveColorPolicyState: procedure; stdcall;
WinSetPreferredAppMode: function(appMode: TPreferredAppMode): TPreferredAppMode; stdcall;
SetWindowCompositionAttribute: function(hWnd: hWnd; pData: PWindowCompositionAttribData): BOOL; stdcall;
WinShouldAppsUseDarkMode: function: BOOL; stdcall;
GDarkModeSupported: BOOL = False; // changed type to BOOL
GDarkModeEnabled: BOOL = False;
GUxTheme: HMODULE = 0;
const
LOAD_LIBRARY_SEARCH_SYSTEM32 = $00000800;
const
themelib = 'uxtheme.dll';
procedure RefreshTitleBarThemeColor(Handle: THandle);
var
LUseDark: BOOL;
LData: TWindowCompositionAttribData;
begin
if not Assigned(WinIsDarkModeAllowedForWindow) then
Exit;
if not Assigned(WinShouldAppsUseDarkMode) then
Exit;
LUseDark := WinIsDarkModeAllowedForWindow(Handle) and WinShouldAppsUseDarkMode and not IsHighContrast;
if TOSVersion.Build < 18362 then
SetProp(Handle, 'UseImmersiveDarkModeColors', THandle(LUseDark))
else if Assigned(SetWindowCompositionAttribute) then
begin
LData.Attrib := TWindowCompositionAttribute.WCA_USEDARKMODECOLORS;
LData.pvData := @LUseDark;
LData.cbData := SizeOf(LUseDark);
SetWindowCompositionAttribute(Handle, @LData);
end;
end;
function AllowDarkModeForWindow(Handle: THandle; Allow: Boolean): Boolean;
begin
Result := GDarkModeSupported and WinAllowDarkModeForWindow(Handle, Allow);
end;
function IsDarkModeAllowedForWindow(Handle: THandle): Boolean;
begin
Result := Assigned(WinIsDarkModeAllowedForWindow) and WinIsDarkModeAllowedForWindow(Handle);
end;
function GetIsImmersiveColorUsingHighContrast(Mode: TImmersiveHCCacheMode): Boolean;
begin
Result := Assigned(WinGetIsImmersiveColorUsingHighContrast) and WinGetIsImmersiveColorUsingHighContrast(Mode);
end;
//
procedure AllowDarkModeForApp(Allow: Boolean);
begin
if Assigned(WinAllowDarkModeForApp) then
WinAllowDarkModeForApp(Allow)
else if Assigned(WinSetPreferredAppMode) then
begin
if Allow then
WinSetPreferredAppMode(TPreferredAppMode.AllowDarkMode)
else
WinSetPreferredAppMode(TPreferredAppMode.DefaultMode);
end;
end;
function ShouldAppsUseDarkMode: Boolean;
begin
Result := Assigned(WinShouldAppsUseDarkMode) and WinShouldAppsUseDarkMode;
end;
function IsWindows10OrGreater(buildNumber: DWORD): Boolean;
begin
Result := (TOSVersion.Major > 10) or
((TOSVersion.Major = 10) and (TOSVersion.Minor = 0) and (DWORD(TOSVersion.Build) >= buildNumber));
end;
function IsWindows11OrGreater(buildNumber: DWORD): Boolean;
begin
Result := IsWindows10OrGreater(22000) or IsWindows10OrGreater(buildNumber);
end;
function CheckBuildNumber(buildNumber: DWORD): Boolean;
begin
Result := //
IsWindows10OrGreater(20348) or //
IsWindows10OrGreater(19045) or //
IsWindows10OrGreater(19044) or //
IsWindows10OrGreater(19043) or //
IsWindows10OrGreater(19042) or //
IsWindows10OrGreater(19041) or // 2004
IsWindows10OrGreater(18363) or // 1909
IsWindows10OrGreater(18362) or // 1903
IsWindows10OrGreater(17763); // 1809
end;
function ImmersiveDarkMode: TDwmWindowAttribute;
begin
if IsWindows10OrGreater(18985) then
Result := TDwmWindowAttribute.DWMWA_USE_IMMERSIVE_DARK_MODE
else
Result := TDwmWindowAttribute.DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1;
end;
procedure InitDarkMode;
begin
if (TOSVersion.Major = 10) and (TOSVersion.Minor = 0) and CheckBuildNumber(TOSVersion.Build) then
begin
GUxTheme := LoadLibraryEx(themelib, 0, LOAD_LIBRARY_SEARCH_SYSTEM32);
if GUxTheme <> 0 then
begin
@WinAllowDarkModeForWindow := GetProcAddress(GUxTheme, MAKEINTRESOURCEA(133));
@WinGetIsImmersiveColorUsingHighContrast := GetProcAddress(GUxTheme, MAKEINTRESOURCEA(106));
@WinIsDarkModeAllowedForWindow := GetProcAddress(GUxTheme, MAKEINTRESOURCEA(137));
@WinRefreshImmersiveColorPolicyState := GetProcAddress(GUxTheme, MAKEINTRESOURCEA(104));
@SetWindowCompositionAttribute := GetProcAddress(GetModuleHandle(user32), 'SetWindowCompositionAttribute');
@WinShouldAppsUseDarkMode := GetProcAddress(GUxTheme, MAKEINTRESOURCEA(132));
if TOSVersion.Build < 18362 then
@WinAllowDarkModeForApp := GetProcAddress(GUxTheme, MAKEINTRESOURCEA(135))
else
@WinSetPreferredAppMode := GetProcAddress(GUxTheme, MAKEINTRESOURCEA(135));
if Assigned(WinRefreshImmersiveColorPolicyState) and
Assigned(WinShouldAppsUseDarkMode) and
Assigned(WinAllowDarkModeForWindow) and
(Assigned(WinAllowDarkModeForApp) or Assigned(WinSetPreferredAppMode)) and
Assigned(WinIsDarkModeAllowedForWindow)
then
begin
GDarkModeSupported := True;
AllowDarkModeForApp(True);
WinRefreshImmersiveColorPolicyState;
GDarkModeEnabled := ShouldAppsUseDarkMode and not IsHighContrast;
end;
end;
end;
end;
procedure DoneDarkMode;
begin
if GUxTheme <> 0 then
FreeLibrary(GUxTheme);
end;
initialization
InitDarkMode;
finalization
DoneDarkMode;
end.