forked from RamonUnch/AltSnap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunfuck.h
1463 lines (1317 loc) · 48.4 KB
/
unfuck.h
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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Compatibility layer for old NT OSes *
* Written by Raymond Gillibert in 2021 *
* THIS FILE IS NOT UNDER GPL but under the WTFPL. *
* DO WHAT THE FUCK YOU WANT WITH THIS CODE! *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef _UNFUCK_NT_
#define _UNFUCK_NT_
#include <windows.h>
#include <oleacc.h>
#include <stdio.h>
#include "nanolibc.h"
/* #include <dwmapi.h> */
enum DWMWINDOWATTRIBUTE {
/* Windows Vista+ */
DWMWA_NCRENDERING_ENABLED=1,
DWMWA_NCRENDERING_POLICY,
DWMWA_TRANSITIONS_FORCEDISABLED,
DWMWA_ALLOW_NCPAINT,
DWMWA_CAPTION_BUTTON_BOUNDS,
DWMWA_NONCLIENT_RTL_LAYOUT,
DWMWA_FORCE_ICONIC_REPRESENTATION,
DWMWA_FLIP3D_POLICY,
DWMWA_EXTENDED_FRAME_BOUNDS,
DWMWA_HAS_ICONIC_BITMAP,
/* Windows 7+ */
DWMWA_DISALLOW_PEEK,
DWMWA_EXCLUDED_FROM_PEEK,
DWMWA_CLOAK,
/* Windows 8+ */
DWMWA_CLOAKED, /* 14 */
DWMWA_FREEZE_REPRESENTATION,
DWMWA_PASSIVE_UPDATE_MODE,
DWMWA_USE_HOSTBACKDROPBRUSH, /* Set, *pvAttribute=BOOL */
/* Windows 10 1809 + (17763 <= build < 18985), Undocumented */
DWMWA_USE_IMMERSIVE_DARK_MODE_PRE20H1=19, /* Set, *pvAttribute=BOOL */
/* Windows 10 21H1 + (build >= 18985) */
DWMWA_USE_IMMERSIVE_DARK_MODE = 20, /* Documented value since Win 11 build 22000 */
/* Windows 11 Build 22000 + */
DWMWA_WINDOW_CORNER_PREFERENCE = 33,
DWMWA_BORDER_COLOR,
DWMWA_CAPTION_COLOR,
DWMWA_TEXT_COLOR,
DWMWA_VISIBLE_FRAME_BORDER_THICKNESS,
/* Windows 11 Build 22621 + */
DWMWA_SYSTEMBACKDROP_TYPE,
DWMWA_LAST,
};
enum MONITOR_DPI_TYPE {
MDT_EFFECTIVE_DPI = 0,
MDT_ANGULAR_DPI = 1,
MDT_RAW_DPI = 2,
MDT_DEFAULT = MDT_EFFECTIVE_DPI
};
/* Invalid pointer with which we initialize
* all dynamically imported functions */
#define IPTR ((FARPROC)(1))
#define QWORD unsigned long long
#ifdef WIN64
#define CopyRect(x, y) (*(x) = *(y))
#define DorQWORD QWORD
#define HIWORDPTR(ll) ((DWORD) (((QWORD) (ll) >> 32) & 0xFFFFFFFF))
#define LOWORDPTR(ll) ((DWORD) (ll))
#define MAKELONGPTR(lo, hi) ((QWORD) (((DWORD) (lo)) | ((QWORD) ((DWORD) (hi))) << 32))
#else
#define DorQWORD unsigned long
#define HIWORDPTR(l) ((WORD) (((DWORD) (l) >> 16) & 0xFFFF))
#define LOWORDPTR(l) ((WORD) (l))
#define MAKELONGPTR(lo, hi) ((DWORD) (((WORD) (lo)) | ((DWORD) ((WORD) (hi))) << 16))
#endif
#ifndef LOBYTE
#define LOBYTE(w) ((BYTE)(w))
#endif
#ifndef HIBYTE
#define HIBYTE(w) ( (BYTE)(((WORD) (w) >> 8) & 0xFF) )
#endif
#ifndef GET_X_LPARAM
#define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
#define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
#endif
#define ARR_SZ(x) (sizeof(x) / sizeof((x)[0]))
#define IDAPPLY 0x3021
#ifndef IS_SURROGATE_PAIR
#define IS_HIGH_SURROGATE(wch) (((wch) >= 0xd800) && ((wch) <= 0xdbff))
#define IS_LOW_SURROGATE(wch) (((wch) >= 0xdc00) && ((wch) <= 0xdfff))
#define IS_SURROGATE_PAIR(hs, ls) (IS_HIGH_SURROGATE (hs) && IS_LOW_SURROGATE (ls))
#endif
#ifndef NIIF_USER
#define NIIF_USER 0x00000004
#endif
#ifndef WPF_ASYNCWINDOWPLACEMENT
#define WPF_ASYNCWINDOWPLACEMENT 0x0004
#endif
#ifndef WM_DPICHANGED
#define WM_DPICHANGED 0x02E0
#endif
#ifndef MSAA_MENU_SIG
#define MSAA_MENU_SIG 0xAA0DF00D
typedef struct tagMSAAMENUINFO {
DWORD dwMSAASignature;
DWORD cchWText;
LPWSTR pszWText;
} MSAAMENUINFO,*LPMSAAMENUINFO;
#endif
#ifndef SUBCLASSPROC
typedef LRESULT (CALLBACK *SUBCLASSPROC)
(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam
, UINT_PTR uIdSubclass, DWORD_PTR dwRefData);
#endif
//#define LOGA(X, ...) {DWORD err=GetLastError(); FILE *LOG=fopen("ad.log", "a"); fprintf(LOG, X, ##__VA_ARGS__); fprintf(LOG,", LastError=%lu\n",err); fclose(LOG); SetLastError(0); }
#define LOGA LOGfunk
/* Cool warpper for wvsprintf */
static void LOGfunk( const char *fmt, ... )
{
DWORD lerr = GetLastError();
va_list arglist;
char str[512];
va_start( arglist, fmt );
wvsprintfA( str, fmt, arglist );
va_end( arglist );
HANDLE h = CreateFileA( "ad.log",
FILE_APPEND_DATA, FILE_SHARE_READ, NULL, OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if( h == INVALID_HANDLE_VALUE )
return;
char lerrorstr[16];
lstrcat_sA(str, ARR_SZ(str), " (");
lstrcat_sA(str, ARR_SZ(str), itostrA(lerr, lerrorstr, 16));
lstrcat_sA(str, ARR_SZ(str), ")\n");
DWORD dummy;
WriteFile( h, str, lstrlenA(str), &dummy, NULL );
CloseHandle(h);
SetLastError(0);
}
#ifdef LOG_STUFF
#define LOG LOGfunk
#else
#define LOG if(0) LOGdummy
static void LOGdummy(const char *fmt, ...) {}
#endif
#ifdef DEBUG
#include <assert.h>
#else
#undef assert
#define assert(x)
#endif
/* Stuff missing in MinGW */
#ifndef WM_MOUSEHWHEEL
#define WM_MOUSEHWHEEL 0x020E
#endif
/* on both x64 and x32 */
#define NtSuspendProcess NtSuspendProcessL
#define NtResumeProcess NtResumeProcessL
#ifndef WIN64
#define GetLayeredWindowAttributes GetLayeredWindowAttributesL
#define SetLayeredWindowAttributes SetLayeredWindowAttributesL
#define GetAncestor GetAncestorL
#undef GetMonitorInfo
#define GetMonitorInfo GetMonitorInfoL
#define EnumDisplayMonitors EnumDisplayMonitorsL
#define MonitorFromPoint MonitorFromPointL
#define MonitorFromWindow MonitorFromWindowL
/* #define GetGUIThreadInfo GetGUIThreadInfoL (NT4 SP3+/Win98+) */
#endif
/* Helper function to pop a message bow with error code*/
static void ErrorBox(const TCHAR * const title)
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
(LPTSTR) &lpMsgBuf,
0, NULL
);
MessageBox( NULL, (LPCTSTR)lpMsgBuf, title, MB_OK | MB_ICONWARNING );
/* Free the buffer using LocalFree. */
LocalFree( lpMsgBuf );
}
/* Helper functiont to strcat variable amount of TCHAR*s */
static size_t lstrcatM_s(TCHAR *d, size_t dl, ...)
{
while( *d && dl-- ) d++;
va_list arglist;
va_start( arglist, dl );
while(1) {
if(dl == 0) break; /* End of string! */
const TCHAR *s = (const TCHAR *)va_arg(arglist, const TCHAR*);
if (s == NULL) break;
/* inline naive strcpy_s */
for (; dl && (*d=*s); ++s,++d,--dl);
}
*d = TEXT('\0'); /* Ensure NULL termination */
va_end( arglist );
return dl; /* Remaining TCHARs */
}
static int PrintHwndDetails(HWND hwnd, TCHAR *buf)
{
TCHAR klass[256], title[256];
GetClassName(hwnd, klass, ARR_SZ(klass));
GetWindowText(hwnd, title, ARR_SZ(title));
return wsprintf(buf
, TEXT("Hwnd=%x, %s|%s, style=%x, xstyle=%x")
, (UINT)(LONG_PTR)hwnd
, title, klass
, (UINT)GetWindowLongPtr(hwnd, GWL_STYLE)
, (UINT)GetWindowLongPtr(hwnd, GWL_EXSTYLE));
}
/* Helper to be able to enable/disable dialog items
* easily while ensuring we move keyboard focus to
* the next control if it was selected */
static BOOL EnableDlgItem(HWND hdlg, UINT id, BOOL enable)
{
HWND hwndControl = GetDlgItem(hdlg, id);
if (!enable && hwndControl == GetFocus()) {
SendMessage(hdlg, WM_NEXTDLGCTL, 0, FALSE);
}
return EnableWindow(hwndControl, enable);
}
static DWORD GetMsgPT(POINT *pt)
{
DWORD point = GetMessagePos();
pt->x = GET_X_LPARAM(point);
pt->y = GET_Y_LPARAM(point);
return point;
}
/* Removes the trailing file name from a path */
static BOOL PathRemoveFileSpecL(TCHAR *p)
{
int i=0;
if (!p) return FALSE;
while(p[++i] != '\0');
while(i > 0 && p[i] != '\\') i--;
p[i]='\0';
return TRUE;
}
/* Removes the path and keeps only the file name */
static void PathStripPathL(TCHAR *p)
{
int i=0, j;
if (!p) return;
while(p[++i] != '\0');
while(i >= 0 && p[i] != '\\') i--;
i++;
for(j=0; p[i+j] != '\0'; j++) p[j]=p[i+j];
p[j]= '\0';
}
static BOOL HaveProc(const char * const DLLname, const char * const PROCname)
{
HINSTANCE hdll = LoadLibraryA(DLLname);
BOOL ret = FALSE;
if (hdll) {
if(GetProcAddress(hdll, PROCname)) {
ret = TRUE ;
}
FreeLibrary(hdll);
}
return ret;
}
static FARPROC LoadDLLProc(const char *DLLname, const char *PROCname)
{
HINSTANCE hdll;
FARPROC ret = (FARPROC)NULL;
if((hdll = GetModuleHandleA(DLLname))) {
return GetProcAddress(hdll, PROCname);
}
hdll = LoadLibraryA(DLLname);
if (hdll) {
ret = GetProcAddress(hdll, PROCname);
if (!ret) FreeLibrary(hdll);
}
return ret;
}
/* Accurate Sleep, needs WINMM.DLL */
static void ASleep(DWORD duration_ms)
{
static MMRESULT (WINAPI *mtimeGetDevCaps)(LPTIMECAPS ptc, UINT cbtc) = (MMRESULT (WINAPI *)(LPTIMECAPS ptc, UINT cbtc))IPTR;
static MMRESULT (WINAPI *mtimeBeginPeriod)(UINT uPeriod);
static MMRESULT (WINAPI *mtimeEndPeriod)(UINT uPeriod);
if (duration_ms > 15) {
/* No need for accurate sleep... */
Sleep(duration_ms);
return;
}
if (mtimeGetDevCaps == (MMRESULT (WINAPI *)(LPTIMECAPS ptc, UINT cbtc))IPTR) {
HINSTANCE h=LoadLibraryA("WINMM.DLL");
if (h) {
mtimeGetDevCaps =(MMRESULT (WINAPI *)(LPTIMECAPS ptc, UINT cbtc))GetProcAddress(h, "timeGetDevCaps");
mtimeBeginPeriod=(MMRESULT (WINAPI *)(UINT uPeriod))GetProcAddress(h, "timeBeginPeriod");
mtimeEndPeriod =(MMRESULT (WINAPI *)(UINT uPeriod))GetProcAddress(h, "timeEndPeriod");
if(!mtimeGetDevCaps || !mtimeBeginPeriod || !mtimeEndPeriod) {
mtimeGetDevCaps=NULL;
FreeLibrary(h);
}
}
}
/* We have winmm functions */
/* This absurd code makes Sleep() more accurate
* - without it, Sleep() is not even +-10ms accurate
* - with it, Sleep is around +-1.5 ms accurate
*/
if(mtimeGetDevCaps) {
TIMECAPS tc;
mtimeGetDevCaps(&tc, sizeof(tc));
mtimeBeginPeriod(tc.wPeriodMin); /* begin accurate sleep */
Sleep(duration_ms); /* perform The SLEEP */
mtimeEndPeriod(tc.wPeriodMin); /* end accurate sleep*/
} else {
Sleep(duration_ms);
}
}
static BOOL FreeDLLByName(const char * const DLLname)
{
HINSTANCE hdll;
if((hdll = GetModuleHandleA(DLLname)))
return FreeLibrary(hdll);
return FALSE;
}
static HWND GetAncestorL(HWND hwnd, UINT gaFlags)
{
#define FUNK_TYPE ( HWND (WINAPI *)(HWND hwnd, UINT gaFlags) )
static HWND (WINAPI *funk)(HWND hwnd, UINT gaFlags) = FUNK_TYPE IPTR;
HWND hlast, hprevious;
LONG wlong;
if(!hwnd) return NULL;
hprevious = hwnd;
if (funk == FUNK_TYPE IPTR) {
funk = FUNK_TYPE LoadDLLProc("USER32.DLL", "GetAncestor");
}
if(funk) { /* We know we have the function */
return funk(hwnd, gaFlags);
}
/* Fallback */
while ( (hlast = GetParent(hprevious)) != NULL ){
wlong=GetWindowLong(hprevious, GWL_STYLE);
if(wlong&(WS_POPUP)) break;
hprevious=hlast;
}
return hprevious;
#undef FUNK_TYPE
}
static BOOL GetLayeredWindowAttributesL(HWND hwnd, COLORREF *pcrKey, BYTE *pbAlpha, DWORD *pdwFlags)
{
#define FUNK_TYPE ( BOOL (WINAPI *)(HWND hwnd, COLORREF *pcrKey, BYTE *pbAlpha, DWORD *pdwFlags) )
static BOOL (WINAPI *funk)(HWND hwnd, COLORREF *pcrKey, BYTE *pbAlpha, DWORD *pdwFlags) = FUNK_TYPE IPTR;
if (funk == FUNK_TYPE IPTR) {
funk = FUNK_TYPE LoadDLLProc("USER32.DLL", "GetLayeredWindowAttributes");
}
if (funk) { /* We know we have the function */
return funk(hwnd, pcrKey, pbAlpha, pdwFlags);
}
return FALSE;
#undef FUNK_TYPE
}
static BOOL SetLayeredWindowAttributesL(HWND hwnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags)
{
#define FUNK_TYPE ( BOOL (WINAPI *)(HWND hwnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags) )
static BOOL (WINAPI *funk)(HWND hwnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags) = FUNK_TYPE IPTR;
if (funk == FUNK_TYPE IPTR) { /* First time */
funk= FUNK_TYPE LoadDLLProc("USER32.DLL", "SetLayeredWindowAttributes");
}
if(funk) { /* We know we have the function */
return funk(hwnd, crKey, bAlpha, dwFlags);
}
return FALSE;
#undef FUNK_TYPE
}
static BOOL GetMonitorInfoL(HMONITOR hMonitor, LPMONITORINFO lpmi)
{
#define FUNK_TYPE ( BOOL (WINAPI *)(HMONITOR hMonitor, LPMONITORINFO lpmi) )
static BOOL (WINAPI *funk)(HMONITOR hMonitor, LPMONITORINFO lpmi) = FUNK_TYPE IPTR;
if (funk == FUNK_TYPE IPTR) { /* First time */
#ifdef _UNICODE
funk = FUNK_TYPE LoadDLLProc("USER32.DLL", "GetMonitorInfoW");
#else
funk = FUNK_TYPE LoadDLLProc("USER32.DLL", "GetMonitorInfoA");
#endif
}
if(funk) { /* We know we have the function */
if(hMonitor) return funk(hMonitor, lpmi);
}
/* Fallback for NT4 */
GetClientRect(GetDesktopWindow(), &lpmi->rcMonitor);
SystemParametersInfo(SPI_GETWORKAREA, 0, &lpmi->rcWork, 0);
lpmi->dwFlags = MONITORINFOF_PRIMARY;
return TRUE;
#undef FUNK_TYPE
}
static BOOL EnumDisplayMonitorsL(HDC hdc, LPCRECT lprcClip, MONITORENUMPROC lpfnEnum, LPARAM dwData)
{
#define FUNK_TYPE ( BOOL (WINAPI *)(HDC hdc, LPCRECT lprcClip, MONITORENUMPROC lpfnEnum, LPARAM dwData) )
static BOOL (WINAPI *funk)(HDC hdc, LPCRECT lprcClip, MONITORENUMPROC lpfnEnum, LPARAM dwData) = FUNK_TYPE IPTR;
MONITORINFO mi;
if (funk == FUNK_TYPE IPTR) { /* First time */
funk= FUNK_TYPE LoadDLLProc("USER32.DLL", "EnumDisplayMonitors");
}
if (funk) { /* We know we have the function */
return funk(hdc, lprcClip, lpfnEnum, dwData);
}
/* Fallbak */
GetMonitorInfoL(NULL, &mi);
lpfnEnum(NULL, NULL, &mi.rcMonitor, 0); /* Callback function */
return TRUE;
#undef FUNK_TYPE
}
static HMONITOR MonitorFromPointL(POINT pt, DWORD dwFlags)
{
#define FUNK_TYPE ( HMONITOR (WINAPI *)(POINT pt, DWORD dwFlags) )
static HMONITOR (WINAPI *funk)(POINT pt, DWORD dwFlags) = FUNK_TYPE IPTR;
if (funk == FUNK_TYPE IPTR) { /* First time */
funk = FUNK_TYPE LoadDLLProc("USER32.DLL", "MonitorFromPoint");
}
if (funk) { /* We know we have the function */
return funk(pt, dwFlags);
}
return NULL;
#undef FUNK_TYPE
}
static HMONITOR MonitorFromWindowL(HWND hwnd, DWORD dwFlags)
{
#define FUNK_TYPE ( HMONITOR (WINAPI *)(HWND hwnd, DWORD dwFlags) )
static HMONITOR (WINAPI *funk)(HWND hwnd, DWORD dwFlags) = FUNK_TYPE IPTR;
if (funk == FUNK_TYPE IPTR) { /* First time */
funk = FUNK_TYPE LoadDLLProc("USER32.DLL", "MonitorFromWindow");
}
if (funk) { /* We know we have the function */
return funk(hwnd, dwFlags);
}
return NULL;
#undef FUNK_TYPE
}
static BOOL GetGUIThreadInfoL(DWORD pid, LPGUITHREADINFO lpgui)
{
#define FUNK_TYPE ( BOOL (WINAPI *)(DWORD pid, LPGUITHREADINFO lpgui) )
static BOOL (WINAPI *funk)(DWORD pid, LPGUITHREADINFO lpgui) = FUNK_TYPE IPTR;
if (funk == FUNK_TYPE IPTR) { /* First time */
funk = FUNK_TYPE LoadDLLProc("USER32.DLL", "GetGUIThreadInfo");
}
if (funk) { /* We know we have the function */
return funk(pid, lpgui);
}
return FALSE;
#undef FUNK_TYPE
}
static int GetSystemMetricsForDpiL(int nIndex, UINT dpi)
{
#define FUNK_TYPE ( int (WINAPI *)(int nIndex, UINT dpi) )
static int (WINAPI *funk)(int nIndex, UINT dpi) = FUNK_TYPE IPTR;
if (dpi) {
if (funk == FUNK_TYPE IPTR) { /* First time */
funk = FUNK_TYPE LoadDLLProc("USER32.DLL", "GetSystemMetricsForDpi");
}
if (funk) { /* We know we have the function */
return funk(nIndex, dpi);
}
}
/* Use non dpi stuff if dpi == 0 or if it does not exist. */
return GetSystemMetrics(nIndex);
#undef FUNK_TYPE
}
#define GetSystemMetricsForDpi GetSystemMetricsForDpiL
static HRESULT GetDpiForMonitorL(HMONITOR hmonitor, int dpiType, UINT *dpiX, UINT *dpiY)
{
#define FUNK_TYPE ( HRESULT (WINAPI *)(HMONITOR hmonitor, int dpiType, UINT *dpiX, UINT *dpiY) )
static HRESULT (WINAPI *funk)(HMONITOR hmonitor, int dpiType, UINT *dpiX, UINT *dpiY) = FUNK_TYPE IPTR;
if (funk == FUNK_TYPE IPTR) { /* First time */
funk = FUNK_TYPE LoadDLLProc("SHCORE.DLL", "GetDpiForMonitor");
}
if (funk) { /* We know we have the function */
return funk(hmonitor, dpiType, dpiX, dpiY);
}
return 666; /* Fail with 666 error */
#undef FUNK_TYPE
}
/* Supported wince Windows 10, version 1607 [desktop apps only] */
static UINT GetDpiForWindowL(const HWND hwnd)
{
#define FUNK_TYPE ( UINT (WINAPI *)(const HWND hwnd) )
static UINT (WINAPI *funk)(const HWND hwnd) = FUNK_TYPE IPTR;
if (funk == FUNK_TYPE IPTR) { /* First time */
funk = FUNK_TYPE LoadDLLProc("USER32.DLL", "GetDpiForWindow");
}
if (funk) { /* We know we have the function */
return funk(hwnd);
}
/* Windows 8.1 / Server2012 R2 Fallback */
UINT dpiX=0;
UINT dpiY=0;
HMONITOR hmon;
if ((hmon = MonitorFromWindowL(hwnd, MONITOR_DEFAULTTONEAREST))
&& S_OK == GetDpiForMonitorL(hmon, MDT_DEFAULT, &dpiX, &dpiY)) {
return dpiX;
}
return 0; /* Not handled */
#undef FUNK_TYPE
}
#define GetDpiForWindow GetDpiForWindowL
static UINT ReallyGetDpiForWindow(const HWND hwnd)
{
UINT dpi = GetDpiForWindowL(hwnd);
if (!dpi) {
HDC hdc = GetDC(hwnd);
if(hdc) {
dpi = (UINT)GetDeviceCaps(hdc, LOGPIXELSY);
ReleaseDC(hwnd, hdc);
if (!dpi) dpi = 96; /* Default to 96 dpi*/
} else {
dpi = 96; /* cannot et a DC, default to 96 dpi */
}
}
return dpi;
}
/* https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enablenonclientdpiscaling
* Available since Windows 10.0.14342 (~1607) https://stackoverflow.com/questions/36864894
* Useless since Windows 10.0.15063 (1703)
* So it is only useful in builds frim 1607 to 1703
* To be called in the WM_NCCREATE message handler.
*/
static BOOL EnableNonClientDpiScalingL(HWND hwnd)
{
/* For Windows 10 below build 15063 */
#define FUNK_TYPE ( BOOL (WINAPI *)(HWND hwnd) )
static BOOL (WINAPI *funk)(HWND hwnd) = FUNK_TYPE IPTR;
if (funk == FUNK_TYPE IPTR) { /* First time */
funk = FUNK_TYPE LoadDLLProc("USER32.DLL", "EnableNonClientDpiScaling");
}
if (funk) { /* We know we have the function */
return funk(hwnd);
}
return FALSE;
#undef FUNK_TYPE
}
/* Only applies to Windows NT for build number */
static xpure BOOL OredredWinVer()
{
DWORD WinVer;
DWORD ver = GetVersion();
WinVer = (ver&0x000000FF) << 24 /* MAJOR */
| (ver&0x0000FF00) << 8 /* MINOR */
| (ver&0xFFFF0000) >> 16;/* BUILDID */
/* On Windows 9x, no buildID is available in GetVer */
if (ver & 0x80000000) {
// Only use minor/major ver.
WinVer |= 0xFFFF0000;
}
return WinVer;
}
static BOOL IsDarkModeEnabled(void)
{
DWORD value = 0;
if ( OredredWinVer() >= 0x0A004563 ) {
/* In case of Windows 10 build 10.0.17763 or 10.x (future) or 11+ */
DWORD len=sizeof(DWORD);
HKEY key;
if (RegOpenKeyEx(HKEY_CURRENT_USER
, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize")
, 0, KEY_QUERY_VALUE, &key) == ERROR_SUCCESS)
{
if (RegQueryValueEx(key, TEXT("AppsUseLightTheme"), NULL, NULL, (LPBYTE)&value, &len) == ERROR_SUCCESS) {
value = !value;
}
RegCloseKey(key);
}
}
return value;
}
static BOOL IsHighContrastEnabled()
{
HIGHCONTRAST hc = { sizeof(hc) };
if (SystemParametersInfo(SPI_GETHIGHCONTRAST, sizeof(hc), &hc, FALSE))
return hc.dwFlags & HCF_HIGHCONTRASTON;
return FALSE;
}
/* We can use the DWMWA_USE_IMMERSIVE_DARK_MODE=20 DWM style
* It is documented for Win11 build 22000 but it also appears to work for Win10
* However from build 1809 to 2004 it the value was =19 and it is =20
* Since Windows 10 build 20H1 and on Win11
* DWMWA_USE_IMMERSIVE_DARK_MODE=19 up to Win10 2004
* DWMWA_USE_IMMERSIVE_DARK_MODE=20 since Win10 20H1 */
static HRESULT DwmSetWindowAttributeL(HWND hwnd, DWORD a, PVOID b, DWORD c);
static BOOL AllowDarkTitlebar(HWND hwnd)
{
if( IsHighContrastEnabled() )
return FALSE; /* Nothing to do and ignore DarkMode */
BOOL DarkMode = IsDarkModeEnabled();
if ( OredredWinVer() >= 0x0A004A29 ) {
/* Windows 10 build 10.0.18985 ie 20H1 */
DwmSetWindowAttributeL(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
} else if ( OredredWinVer() >= 0x0A004563) {
/* Windows 10 build 10.0.17763 ie: 1809 or later */
if ( OredredWinVer() < 0x0A0047BA) {
/* Windows 10 build 10.0.18362 ie: before 1903 */
SetProp(hwnd, TEXT("UseImmersiveDarkModeColors"), (HANDLE)(LONG_PTR)DarkMode);
}
DwmSetWindowAttributeL(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE_PRE20H1, &DarkMode, sizeof(DarkMode));
}
/* We must return the actual state of the Dark mode */
return DarkMode;
}
/* Helper function */
static int GetSystemMetricsForWin(int nIndex, HWND hwnd)
{
return GetSystemMetricsForDpiL(nIndex, GetDpiForWindowL(hwnd));
}
static BOOL SystemParametersInfoForDpiL(UINT uiAction, UINT uiParam, PVOID pvParam, UINT fWinIni, UINT dpi)
{
#define FUNK_TYPE ( BOOL (WINAPI *)(UINT uiAction, UINT uiParam, PVOID pvParam, UINT fWinIni, UINT dpi) )
static BOOL (WINAPI *funk)(UINT uiAction, UINT uiParam, PVOID pvParam, UINT fWinIni, UINT dpi) = FUNK_TYPE IPTR;
if (dpi) {
if (funk == FUNK_TYPE IPTR) { /* First time */
funk = FUNK_TYPE LoadDLLProc("USER32.DLL", "SystemParametersInfoForDpi");
}
if (funk) { /* We know we have the function */
return funk(uiAction, uiParam, pvParam, fWinIni, dpi);
}
}
/* Not handeled */
return SystemParametersInfo(uiAction, uiParam, pvParam, fWinIni);
#undef FUNK_TYPE
}
#define SystemParametersInfoForDpi SystemParametersInfoForDpiL
static HWINEVENTHOOK SetWinEventHookL(
DWORD eventMin, DWORD eventMax
, HMODULE hmodWinEventProc
, WINEVENTPROC pfnWinEventProc
, DWORD idProcess, DWORD idThread, DWORD dwFlags)
{
#define FUNK_TYPE ( HWINEVENTHOOK (WINAPI *)(DWORD evm, DWORD evM, HMODULE hmod, WINEVENTPROC wevp, DWORD idProcess, DWORD idThread, DWORD dwFlags) )
static HWINEVENTHOOK (WINAPI *funk)(DWORD evm, DWORD evM, HMODULE hmod, WINEVENTPROC wevp, DWORD idProcess, DWORD idThread, DWORD dwFlags)= FUNK_TYPE IPTR;
if (funk == FUNK_TYPE IPTR) { /* First time */
funk = FUNK_TYPE LoadDLLProc("USER32.DLL", "SetWinEventHook");
}
if (funk) { /* We know we have the function */
return funk(eventMin, eventMax, hmodWinEventProc
, pfnWinEventProc, idProcess, idThread, dwFlags);
}
/* Failed */
return NULL;
#undef FUNK_TYPE
}
static BOOL UnhookWinEventL(HWINEVENTHOOK hWinEventHook)
{
#define FUNK_TYPE ( BOOL (WINAPI *)(HWINEVENTHOOK hWinEventHook) )
static BOOL (WINAPI *funk)(HWINEVENTHOOK hWinEventHook) = FUNK_TYPE IPTR;
if (funk == FUNK_TYPE IPTR) { /* First time */
funk = FUNK_TYPE LoadDLLProc("USER32.DLL", "UnhookWinEvent");
}
if (funk) { /* We know we have the function */
return funk(hWinEventHook);
}
/* Failed */
return FALSE;
#undef FUNK_TYPE
}
static HRESULT DwmGetWindowAttributeL(HWND hwnd, DWORD a, PVOID b, DWORD c)
{
#define FUNK_TYPE ( HRESULT (WINAPI *)(HWND hwnd, DWORD a, PVOID b, DWORD c) )
static HRESULT (WINAPI *funk)(HWND hwnd, DWORD a, PVOID b, DWORD c) = FUNK_TYPE IPTR;
if (funk == FUNK_TYPE IPTR) { /* First time */
funk = FUNK_TYPE LoadDLLProc("DWMAPI.DLL", "DwmGetWindowAttribute");
}
if (funk) { /* We know we have the function */
return funk(hwnd, a, b, c);
}
/* DwmGetWindowAttribute return 0 on sucess ! */
return 666; /* Here we FAIL with 666 error */
#undef FUNK_TYPE
}
static HRESULT DwmSetWindowAttributeL(HWND hwnd, DWORD a, PVOID b, DWORD c)
{
#define FUNK_TYPE ( HRESULT (WINAPI *)(HWND hwnd, DWORD a, PVOID b, DWORD c) )
static HRESULT (WINAPI *funk)(HWND hwnd, DWORD a, PVOID b, DWORD c) = FUNK_TYPE IPTR;
if (funk == FUNK_TYPE IPTR) { /* First time */
funk = FUNK_TYPE LoadDLLProc("DWMAPI.DLL", "DwmSetWindowAttribute");
}
if (funk) { /* We know we have the function */
return funk(hwnd, a, b, c);
}
/* myDwmSetWindowAttribute return 0 on sucess ! */
return 666; /* Here we FAIL with 666 error */
#undef FUNK_TYPE
}
static HRESULT DwmGetColorizationColorL(DWORD *a, BOOL *b)
{
#define FUNK_TYPE ( HRESULT (WINAPI *)(DWORD *a, BOOL *b) )
static HRESULT (WINAPI *funk)(DWORD *a, BOOL *b) = FUNK_TYPE IPTR;
if (funk == FUNK_TYPE IPTR) { /* First time */
funk = FUNK_TYPE LoadDLLProc("DWMAPI.DLL", "DwmGetColorizationColor");
}
if (funk) { /* We know we have the function */
return funk(a, b);
}
/* return 0 on sucess ! */
return 666; /* Here we FAIL with 666 error */
#undef FUNK_TYPE
}
static COLORREF GetSysColorizationColor()
{
DWORD color=0;
BOOL b=FALSE;
if(S_OK == DwmGetColorizationColorL(&color, &b)) {
/* Re orde- bytes because
* COLORREF: 0x00BBGGRR and not 0xAARRGGBB */
return (color&0x000000FF) << 16 /* blue */
| (color&0x0000FF00) /* green */
| (color&0x00FF0000) >> 16; /* red */
}
return 0;
}
/* #define DwmGetWindowAttribute DwmGetWindowAttributeL */
static void SubRect(RECT *__restrict__ frame, const RECT *rect)
{
frame->left -= rect->left;
frame->top -= rect->top;
frame->right = rect->right - frame->right;
frame->bottom = rect->bottom - frame->bottom;
}
static void InflateRectBorder(RECT *__restrict__ rc, const RECT *bd)
{
rc->left -= bd->left;
rc->top -= bd->top;
rc->right += bd->right;
rc->bottom += bd->bottom;
}
static void DeflateRectBorder(RECT *__restrict__ rc, const RECT *bd)
{
rc->left += bd->left;
rc->top += bd->top;
rc->right -= bd->right;
rc->bottom -= bd->bottom;
}
static void OffsetPoints(POINT *pts, long dx, long dy, unsigned count)
{
while(count--) {
pts->x += dx;
pts->y += dy;
pts++;
}
}
static void FixDWMRectLL(HWND hwnd, RECT *bbb, const int SnapGap)
{
RECT rect, frame;
if(S_OK == DwmGetWindowAttributeL(hwnd, DWMWA_EXTENDED_FRAME_BOUNDS, &frame, sizeof(RECT))
&& GetWindowRect(hwnd, &rect)){
SubRect(&frame, &rect);
CopyRect(bbb, &frame);
} else {
SetRectEmpty(bbb);
/*SetRect(bbb, 10, 10, 10, 10);*/
}
if (SnapGap) OffsetRect(bbb, -SnapGap, -SnapGap);
}
/* This function is here because under Windows 10, the GetWindowRect function
* includes invisible borders, if those borders were visible it would make
* sense, this is the case on Windows 7 and 8.x for example
* We use DWM api when available in order to get the REAL client area
*/
static BOOL GetWindowRectLL(HWND hwnd, RECT *rect, const int SnapGap)
{
HRESULT ret = DwmGetWindowAttributeL(hwnd, DWMWA_EXTENDED_FRAME_BOUNDS, rect, sizeof(RECT));
if( ret == S_OK) {
ret = TRUE;
} else {
ret = GetWindowRect(hwnd, rect); /* Fallback to normal */
}
if (SnapGap) InflateRect(rect, SnapGap, SnapGap);
return ret;
}
/* Under Win8 and later a window can be cloaked
* This falg can be obtained with this function
* 1 The window was cloaked by its owner application.
* 2 The window was cloaked by the Shell.
* 4 The cloak value was inherited from its owner window.
* For windows that are supposed to be logically "visible", in addition to WS_VISIBLE.
* EDIT: Now Raymond Chen did an article about this:
* https://devblogs.microsoft.com/oldnewthing/20200302-00/?p=103507
* I had to figure it out the Hard way...
*/
static int IsWindowCloaked(HWND hwnd)
{
int cloaked=0;
return S_OK == DwmGetWindowAttributeL(hwnd, DWMWA_CLOAKED, &cloaked, sizeof(cloaked))
&& cloaked;
}
static BOOL IsVisible(HWND hwnd)
{
return IsWindowVisible(hwnd) && !IsWindowCloaked(hwnd);
}
/* Gets the original owner of hwnd.
* stops going back the owner chain if invisible. */
static HWND GetRootOwner(HWND hwnd)
{
HWND parent;
int i=0;
while (( parent = (GetWindowLongPtr(hwnd, GWL_STYLE)&WS_CHILD)
? GetParent(hwnd) : GetWindow(hwnd, GW_OWNER)
)) {
RECT prc;
if (parent == hwnd || i++ > 2048 || !IsVisible(parent)
|| !GetWindowRect(parent, &prc) || IsRectEmpty(&prc)) {
/* Stop if in a loop or if parent is not visible
* or if the parent rect is empty */
break;
}
hwnd = parent;
}
return hwnd;
}
/* Use the DWM api to obtain the rectangel that *should* contain all
* caption buttons. This is usefull to ensure we are not in one of them.
*/
static BOOL GetCaptionButtonsRect(HWND hwnd, RECT *rc)
{
int ret = DwmGetWindowAttributeL(hwnd, DWMWA_CAPTION_BUTTON_BOUNDS, rc, sizeof(RECT));
/* Convert rectangle to to screen coordinate. */
if (ret == S_OK) {
RECT wrc;
GetWindowRect(hwnd, &wrc);
OffsetRect(rc, wrc.left, wrc.top);
return 1;
}
return 0;
}
static LONG NtSuspendProcessL(HANDLE ProcessHandle)
{
#define FUNK_TYPE ( HRESULT (NTAPI *)(HANDLE ProcessHandle) )
static HRESULT (NTAPI *funk)(HANDLE ProcessHandle) = FUNK_TYPE IPTR;
if (funk == FUNK_TYPE IPTR) { /* First time */
funk = FUNK_TYPE LoadDLLProc("NTDLL.DLL", "NtSuspendProcess");
}
if (funk) { /* We know we have the function */
return funk(ProcessHandle);
}
return 666; /* Here we FAIL with 666 error */
#undef FUNK_TYPE
}
static LONG NtResumeProcessL(HANDLE ProcessHandle)
{
#define FUNK_TYPE ( HRESULT (NTAPI *)(HANDLE ProcessHandle) )
static HRESULT (NTAPI *funk)(HANDLE ProcessHandle) = FUNK_TYPE IPTR;
if (funk == FUNK_TYPE IPTR) { /* First time */
funk = FUNK_TYPE LoadDLLProc("NTDLL.DLL", "NtResumeProcess");
}
if (funk) { /* We know we have the function */
return funk(ProcessHandle);
}
return 666; /* Here we FAIL with 666 error */
#undef FUNK_TYPE
}
static HRESULT DwmIsCompositionEnabledL(BOOL *pfEnabled)
{
#define FUNK_TYPE ( HRESULT (WINAPI *)(BOOL *pfEnabled) )
HRESULT (WINAPI *funk)(BOOL *pfEnabled) = FUNK_TYPE IPTR;
HINSTANCE hdll=NULL;
HRESULT ret ;
*pfEnabled = FALSE;
ret = 666;
hdll = LoadLibraryA("DWMAPI.DLL");
if(hdll) {
funk = FUNK_TYPE GetProcAddress(hdll, "DwmIsCompositionEnabled");
if(funk) {
ret = funk(pfEnabled);
} else {
*pfEnabled = FALSE;
ret = 666;
}
FreeLibrary(hdll);
}
return ret;
#undef FUNK_TYPE
}
static BOOL HaveDWM()
{
static int first=1;
static BOOL have_dwm = FALSE;
if(first)
DwmIsCompositionEnabledL(&have_dwm);
first = 0;
return have_dwm;
}
static BOOL DwmDefWindowProcL(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, LRESULT *plResult)
{
#define FUNK_TYPE ( BOOL (WINAPI *)(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, LRESULT *plResult) )
static BOOL (WINAPI *funk)(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, LRESULT *plResult) = FUNK_TYPE IPTR;
if (funk == FUNK_TYPE IPTR) { /* First time */
funk= FUNK_TYPE LoadDLLProc("DWMAPI.DLL", "DwmDefWindowProc");