-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
functions.pas
632 lines (578 loc) · 18.7 KB
/
functions.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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
(*
Functions that we will be using
Changelog:
19-09-13
- Detect Windows 10 versions
19-06-06
- Support for light mode Windows 10 May 2019 Update
18-09-03
- Added detection of Acrylic Glass support Windows 10 1803 17134
16-08-07
- Added function to detect a process elevation
15-12-01
- Added uses OleAcc, Variants and imported DLL AccessibleChildren
- Added function TaskbarTaskViewButtonClick
*)
unit functions;
interface
uses
Windows, Classes, TlHelp32, PsAPI, SysUtils, Registry, Graphics, DWMApi,
ActiveX, ComObj, ShlObj,
PNGImage{,
UXTHeme, Themes} {uxtheme and themes for rendering text on glass }, OleAcc, Variants, Forms;
const
BUILD_THRESHOLD1 = 10240; // N/A
BUILD_THRESHOLD2 = 10586; // November Update
BUILD_REDSTONE1 = 14393; // Anniversary Update
BUILD_REDSTONE2 = 15063; // Creators Update
BUILD_REDSTONE3 = 16299; // Fall Creators Update
BUILD_REDSTONE4 = 17134; // April 2018 Update
BUILD_REDSTONE5 = 17763; // October 2018 Update
BUILD_19H1 = 18362; // May 2019 Update
BUILD_19H2 = 18363;
BUILD_20H1 = 18980;
type
AccentPolicy = packed record
AccentState: Integer;
AccentFlags: Integer;
GradientColor: Integer;
AnimationId: Integer;
end;
TWinCompAttrData = packed record
attribute: THandle;
pData: Pointer;
dataSize: ULONG;
end;
TShellLinkInfo = record
PathName: String;
Arguments: String;
Description: String;
WorkingDirectory: String;
IconLocation: String;
IconIndex: Integer;
ShowCmd: Integer;
HotKey: Word;
end;
function ProcessIsElevated(Process: Cardinal): Boolean;
function GetProcessNameFromWnd(Wnd: HWND): string;
function isWindows10(var version: Integer): boolean;
function isAcrylicSupported:boolean;
function SystemUsesLightTheme:boolean;
procedure EnableBlur(Wnd: HWND; Enable: Boolean = True);
function GetAccentColor:TColor;
function TaskbarTranslucent:Boolean;
function TaskbarAccented:Boolean;
procedure SetAlphaColorPicture(const Col: TColor; const Alpha: Integer; Picture: TPicture);
function GetRectOfPrimaryMonitor(const WorkArea: Boolean): TRect;
function BlendColors(Col1, Col2: TColor; A: Byte): TColor;
function CreatePreMultipliedRGBQuad(Color: TColor; Alpha: Byte = $FF): TRGBQuad;
function CreateSolidBrushWithAlpha(Color: TColor; Alpha: Byte = $FF): HBRUSH;
function TaskbarTaskViewBtnClick: Boolean;
{procedure DrawGlassText(Canvas: TCanvas; GlowSize: Integer; var Rect: TRect;
var Text: UnicodeString; Format: DWORD); overload;}
function IsFullScreenWindow(Wnd: HWND): Boolean;
// from http://www.informit.com/articles/article.aspx?p=26940&seqNum=4
procedure GetShellLinkInfo(const LinkFile: WideString; var LnkInfo: TShellLinkInfo);
procedure SetShellLinkInfo(const LinkFile: WideString; const LnkInfo: TShellLinkInfo);
procedure SwitchToThisWindow(h1: hWnd; x: bool); stdcall;
external user32 Name 'SwitchToThisWindow';
function SetWindowCompositionAttribute(Wnd: HWND; const AttrData: TWinCompAttrData): BOOL; stdcall;
external user32 Name 'SetWindowCompositionAttribute';
function AccessibleChildren(paccContainer: Pointer; iChildStart: LONGINT;
cChildren: LONGINT; out rgvarChildren: OleVariant;
out pcObtained: LONGINT): HRESULT; stdcall;
external 'OLEACC.DLL' name 'AccessibleChildren';
implementation
//http://stackoverflow.com/questions/95912/how-can-i-detect-if-my-process-is-running-uac-elevated-or-not
function ProcessIsElevated(Process: Cardinal): Boolean;
var
hToken, hProcess : THandle;
pTokenInformation: Pointer;
ReturnLength: DWORD;
TokenInformation: TTokenElevation;
begin
//hProcess := GetCurrentProcess;
hProcess := Process;
try
if OpenProcessToken(hProcess, TOKEN_QUERY, hToken) then
try
TokenInformation.TokenIsElevated := 0;
pTokenInformation := @TokenInformation;
GetTokenInformation(hToken, TokenElevation, pTokenInformation, SizeOf(TokenInformation), ReturnLength);
Result := (TokenInformation.TokenIsElevated > 0);
finally
CloseHandle(hToken);
end;
except
Result := False;
end;
end;
// This procedure assumes WinXP or superior only : suorce http://www.delphitricks.com/source-code/windows/get_exe_path_from_window_handle.html
function GetProcessNameFromWnd(Wnd: HWND): string;
function RunningProcessesList(const List: TStrings; FullPath: Boolean): Boolean;
function ProcessFilename(PID: DWORD): string;
var
Handle: THandle;
begin
Result := '';
Handle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, PID);
if Handle <> 0 then
try
SetLength(Result, MAX_PATH);
if FullPath then
begin
if GetModuleFileNameEx(Handle, 0, PChar(Result), MAX_PATH) > 0 then
SetLength(Result, strlen(PChar(Result)))
else
Result := '';
end
else
begin
if GetModuleBaseName(Handle, 0, PChar(Result), MAX_PATH) > 0 then
SetLength(Result, StrLen(PChar(Result)))
else
Result := '';
end;
finally
CloseHandle(Handle);
end;
end;
var
SnapProcHandle: THandle;
ProcEntry: TProcessEntry32;
NextProc: Boolean;
Filename: string;
begin
SnapProcHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
Result := (SnapProcHandle <> INVALID_HANDLE_VALUE);
if Result then
try
ProcEntry.dwSize := SizeOf(ProcEntry);
NextProc := Process32First(SnapProcHandle, ProcEntry);
while NextProc do
begin
if ProcEntry.th32ProcessID = 0 then
begin
Filename := 'System Idle Process';
end
else begin
Filename := ProcessFilename(ProcEntry.th32ProcessID);
if Filename = '' then
Filename := ProcEntry.szExeFile;
end;
List.AddObject(Filename, Pointer(ProcEntry.th32ProcessID));
NextProc := Process32Next(SnapProcHandle, ProcEntry);
end;
finally
CloseHandle(SnapProcHandle);
end;
end;
var
List: TStringList;
PID: DWORD;
I: Integer;
begin
Result := '';
if IsWindow(Wnd) then
begin
PID := INVALID_HANDLE_VALUE;
GetWindowThreadProcessId(Wnd, @PID);
List := TStringList.Create;
try
if RunningProcessesList(List, True) then
begin
I := List.IndexOfObject(Pointer(PID));
if I > -1 then
Result := List[I];
end;
finally
List.Free;
end;
end;
end;
(* IsWindows10 function supports official RTM and above only *)
function isWindows10(var version: Integer):boolean;
var
Reg: TRegistry;
begin
Result := False;
Reg := TRegistry.Create;
try
Reg.RootKey := HKEY_LOCAL_MACHINE;
if Reg.OpenKeyReadOnly('SOFTWARE\Microsoft\Windows NT\CurrentVersion') then
begin
if Reg.ValueExists('CurrentVersion') then
begin
version := StrToInt(Reg.ReadString('CurrentBuildNumber'));
if (Reg.ReadString('CurrentVersion') = '6.3')
and (version >= 10240) then
begin
Result := True;
end;
end;
end;
finally
Reg.Free;
end;
end;
// Check Windows 10 RS4 version which onwards supports Acrylic Glass
function isAcrylicSupported:boolean;
var
Reg: TRegistry;
begin
Result := False;
Reg := TRegistry.Create;
try
Reg.RootKey := HKEY_LOCAL_MACHINE;
if Reg.OpenKeyReadOnly('SOFTWARE\Microsoft\Windows NT\CurrentVersion') then
begin
if Reg.ValueExists('CurrentVersion') then
if (Reg.ReadString('CurrentVersion') = '6.3')
and (StrToInt(Reg.ReadString('CurrentBuildNumber')) >= 17134) then
Result := True;
end;
finally
Reg.Free;
end;
end;
// Checks whether registry value which registers system's light mode is on
function SystemUsesLightTheme:boolean;
var
Reg: TRegistry;
begin
Result := False;
Reg := TRegistry.Create;
try
Reg.RootKey := HKEY_CURRENT_USER;
if Reg.OpenKeyReadOnly('Software\Microsoft\Windows\CurrentVersion\Themes\Personalize') then
begin
if Reg.ValueExists('SystemUsesLightTheme') then
if (Reg.ReadInteger('SystemUsesLightTheme') = 1) then
Result := True;
end;
finally
Reg.Free;
end;
end;
procedure EnableBlur(Wnd: HWND; Enable: Boolean = True);
const
WCA_ACCENT_POLICY = 19;
ACCENT_ENABLE_GRADIENT = 1;
ACCENT_ENABLE_TRANSPARENTGRADIENT = 2;
ACCENT_ENABLE_BLURBEHIND = 3;
ACCENT_ENABLE_ACRYLICBLURBEHIND = 4;
DRAW_LEFT_BORDER = $20;
DRAW_TOP_BORDER = $40;
DRAW_RIGHT_BORDER = $80;
DRAW_BOTTOM_BORDER = $100;
var
data: TWinCompAttrData;
accent: AccentPolicy;
begin
if Enable then
begin
if isAcrylicSupported then
accent.AccentState := ACCENT_ENABLE_ACRYLICBLURBEHIND
else
accent.AccentState := ACCENT_ENABLE_BLURBEHIND
end
else
accent.AccentState := ACCENT_ENABLE_TRANSPARENTGRADIENT;
accent.AccentFlags := DRAW_LEFT_BORDER or DRAW_TOP_BORDER or DRAW_RIGHT_BORDER or DRAW_BOTTOM_BORDER;
data.attribute := WCA_ACCENT_POLICY;
data.dataSize := SizeOf(accent);
data.pData := @accent;
SetWindowCompositionAttribute(Wnd, data);
end;
function GetAccentColor:TColor;
var
col: Cardinal;
opaque: LongBool;
newColor: TColor;
a,r,g,b: byte;
begin
DwmGetColorizationColor(col, opaque);
a := Byte(col shr 24);
r := Byte(col shr 16);
g := Byte(col shr 8);
b := Byte(col);
newcolor := RGB(
round(r*(a/255)+255-a),
round(g*(a/255)+255-a),
round(b*(a/255)+255-a)
);
Result := newcolor;
end;
function TaskbarTranslucent: Boolean;
var
reg: TRegistry;
begin
Result := False;
reg := TRegistry.Create;
try
reg.RootKey := HKEY_CURRENT_USER;
reg.OpenKeyReadOnly('SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize');
try
if reg.ValueExists('EnableTransparency') then
if reg.ReadInteger('EnableTransparency') = 1 then
Result := True;
except
Result := False;
end;
reg.CloseKey;
finally
reg.Free;
end;
end;
function TaskbarAccented:Boolean;
var
reg: TRegistry;
begin
Result := False;
reg := TRegistry.Create;
try
reg.RootKey := HKEY_CURRENT_USER;
reg.OpenKeyReadOnly('SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize');
try
if reg.ValueExists('ColorPrevalence') then
if reg.ReadInteger('ColorPrevalence') = 1 then
Result := True;
except
Result := False;
end;
reg.CloseKey;
finally
reg.Free;
end;
end;
procedure SetAlphaColorPicture(const Col: TColor; const Alpha: Integer; Picture: TPicture);
var
png: TPngImage;
J: Integer;
sl: pByteArray;
begin
png := TPNGImage.CreateBlank(COLOR_RGBALPHA, 8, 10, 10);
try
png.Canvas.Brush.Color := Col;
png.Canvas.FillRect(Rect(0,0,10,10));
for J := 0 to png.Height - 1 do
begin
sl := png.AlphaScanline[J];
FillChar(sl^, png.Width, Alpha);
end;
Picture.Assign(png);
finally
png.Free;
end;
end;
function GetRectOfPrimaryMonitor(const WorkArea: Boolean): TRect;
begin
if not WorkArea or not SystemParametersInfo(SPI_GETWORKAREA, 0, @Result, 0) then
Result := Rect(0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
end;
{Credits to Roy M Klever http://rmklever.com/?p=116}
function BlendColors(Col1, Col2: TColor; A: Byte): TColor;
var
c1,c2: LongInt;
r,g,b,v1,v2: byte;
begin
A := Round(2.55 * A);
c1 := ColorToRGB(Col1);
c2 := ColorToRGB(Col2);
v1 := Byte(c1);
v2 := Byte(c2);
r := A * (v1 - v2) shr 8 + v2;
v1 := Byte(c1 shr 8);
v2 := Byte(c2 shr 8);
g := A * (v1 - v2) shr 8 + v2;
v1 := Byte(c1 shr 16);
v2 := Byte(c2 shr 16);
b := A * (v1 - v2) shr 8 + v2;
Result := (b shl 16) + (g shl 8) + r;
end;
// Functions to create alpha channel aware brushes to paint on canvas
// from Delphi Haven https://delphihaven.wordpress.com/2010/09/06/custom-drawing-on-glass-2/
function CreatePreMultipliedRGBQuad(Color: TColor; Alpha: Byte = $FF): TRGBQuad;
begin
Color := ColorToRGB(Color);
Result.rgbBlue := MulDiv(GetBValue(Color), Alpha, $FF);
Result.rgbGreen := MulDiv(GetGValue(Color), Alpha, $FF);
Result.rgbRed := MulDiv(GetRValue(Color), Alpha, $FF);
Result.rgbReserved := Alpha;
end;
function CreateSolidBrushWithAlpha(Color: TColor; Alpha: Byte = $FF): HBRUSH;
var
Info: TBitmapInfo;
begin
FillChar(Info, SizeOf(Info), 0);
with Info.bmiHeader do
begin
biSize := SizeOf(Info.bmiHeader);
biWidth := 1;
biHeight := 1;
biPlanes := 1;
biBitCount := 32;
biCompression := BI_RGB;
end;
Info.bmiColors[0] := CreatePreMultipliedRGBQuad(Color, Alpha);
Result := CreateDIBPatternBrushPt(@Info, 0);
end;
// Using IAccessible we first capture the tasview button handle then trigger iaccessible default action
function TaskbarTaskViewBtnClick: Boolean;
var
btnHandle: HWND;
res : HRESULT;
Acc, ChildAccessible: IAccessible;
btnCaption, ChildName: WideString;
iChildCount, iObtained: Integer;
ChildArray: array of OleVariant;
ChildDispatch: IDispatch;
I: Integer;
begin
Result := False;
btnHandle := FindWindow('Shell_TrayWnd', nil);
if btnHandle > 0 then
begin
btnHandle := FindWindowEx(btnHandle, 0, 'TrayButton', nil);
if not IsWindowVisible(btnHandle) then
//ShowMessage('Task View Button is hidden');
Exit;
if (btnHandle > 0 ) then
begin
//PostMessage(btnHandle, WM_LBUTTONUP, 0, 0);
res := AccessibleObjectFromWindow(btnHandle, 0, IID_IAccessible, Acc);
if res = S_OK then
begin
if Acc.Get_accName(CHILDID_SELF, btnCaption) = S_OK then
begin
//memo1.Lines.Add('btnCaption: '+btnCaption);
end
else
Exit;
// Let's find the correct button whic is named as previous btnCaption
// i.e. previous name found was 'Vista de tareas', so the button child is named the same
// that is our trigger access
if (Acc.Get_accChildCount(iChildCount) = S_OK) and (iChildCount > 0) then
begin
//memo1.Lines.Add('Childs:'+IntToStr(iChildCount));
SetLength(ChildArray, iChildCount);
if AccessibleChildren(Pointer(Acc), 0, iChildCount, ChildArray[0], iObtained) = S_OK then
begin
for I := 0 to iObtained - 1 do
begin
ChildDispatch := nil;
if VarType(ChildArray[I]) = varDispatch then
begin
ChildDispatch := ChildArray[I];
if (ChildDispatch <> nil) and (ChildDispatch.QueryInterface(IAccessible, ChildAccessible) = S_OK) then
begin
if (ChildAccessible.Get_accName(CHILDID_SELF, ChildName) = S_OK) and (ChildName = btnCaption) then
begin
//Memo1.Lines.Add('ChildName: ' + ChildName);
if ChildAccessible.Get_accDefaultAction(CHILDID_SELF, btnCaption) = S_OK then
begin
//memo1.Lines.Add('Default Action: ' + btnCaption);
//ChildAccessible.accSelect(SELFLAG_TAKEFOCUS, CHILDID_SELF);
if ChildAccessible.accDoDefaultAction(CHILDID_SELF) = S_OK then
Result := True;
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
{ procedure DrawGlassText(Canvas: TCanvas; GlowSize: Integer; var Rect: TRect;
var Text: UnicodeString; Format: DWORD); overload;
var
DTTOpts: TDTTOpts;
begin
if Win32MajorVersion < 6 then
begin
DrawTextW(Canvas.Handle, PWideChar(Text), Length(Text), Rect, Format);
Exit;
end;
ZeroMemory(@DTTOpts, SizeOf(DTTOpts));
DTTOpts.dwSize := SizeOf(DTTOpts);
DTTOpts.dwFlags := DTT_COMPOSITED or DTT_TEXTCOLOR;
if Format and DT_CALCRECT = DT_CALCRECT then
DTTOpts.dwFlags := DTTOpts.dwFlags or DTT_CALCRECT;
DTTOpts.crText := ColorToRGB(Canvas.Font.Color);
if GlowSize > 0 then
begin
DTTOpts.dwFlags := DTTOpts.dwFlags or DTT_GLOWSIZE;
DTTOpts.iGlowSize := GlowSize;
end;
with ThemeServices.GetElementDetails(teEditTextNormal) do
DrawThemeTextEx(ThemeServices.Theme[teEdit], Canvas.Handle, Part, State,
PWideChar(Text), Length(Text), Format, @Rect, DTTOpts);
end;}
function IsFullScreenWindow(Wnd: HWND): Boolean;
var
plc: WINDOWPLACEMENT;
rc: TRECT;
mon: TMonitor;
begin
Result := False;
GetWindowPlacement(wnd, plc);
if (plc.showCmd and SW_SHOWMAXIMIZED) = SW_SHOWMAXIMIZED then
begin
GetWindowRect(wnd, rc);
mon := Screen.MonitorFromWindow(wnd);
if mon.BoundsRect = rc then
Result := True;
end;
end;
procedure GetShellLinkInfo(const LinkFile: WideString; var LnkInfo: TShellLinkInfo);
var
SL: IShellLink;
PF: IPersistFile;
FindData: TWin32FindData;
AStr: array[0..MAX_PATH] of char;
begin
OleCheck(CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER, IShellLink, SL));
PF := SL as IPersistFile;
OleCheck(PF.Load(PWideChar(LinkFile), STGM_READ));
OleCheck(SL.Resolve(0, SLR_ANY_MATCH or SLR_NO_UI));
with LnkInfo do
begin
OleCheck(SL.GetPath(AStr, MAX_PATH, FindData, SLGP_SHORTPATH));
PathName := AStr;
OleCheck(SL.GetArguments(AStr, MAX_PATH));
Arguments := AStr;
//OleCheck(SL.GetDescription(AStr, MAX_PATH));
//Description := AStr;
OleCheck(SL.GetWorkingDirectory(AStr, MAX_PATH));
WorkingDirectory := AStr;
OleCheck(SL.GetIconLocation(AStr, MAX_PATH, IconIndex));
IconLocation := AStr;
OleCheck(SL.GetShowCmd(ShowCmd));
OleCheck(SL.GetHotkey(HotKey));
end;
end;
procedure SetShellLinkInfo(const LinkFile: WideString; const LnkInfo: TShellLinkInfo);
var
SL: IShellLink;
PF: IPersistFile;
begin
OleCheck(CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER, IShellLink, SL));
PF := SL as IPersistFile;
OleCheck(PF.Load(PChar(LinkFile), STGM_SHARE_DENY_WRITE));
OleCheck(SL.Resolve(0, SLR_ANY_MATCH or SLR_UPDATE or SLR_NO_UI));
with LnkInfo, SL do
begin
OleCheck(SetPath(PChar(PathName)));
OleCheck(SetArguments(PChar(Arguments)));
//OleCheck(SetDescription(PChar(Description)));
OleCheck(SetWorkingDirectory(PChar(WorkingDirectory)));
OleCheck(SetIconLocation(PChar(IconLocation), IconIndex));
OleCheck(SetShowCmd(ShowCmd));
OleCheck(SetHotkey(HotKey));
end;
PF.Save(PChar(LinkFile), True);
end;
end.