-
Notifications
You must be signed in to change notification settings - Fork 84
/
unfuck.h
1618 lines (1447 loc) · 51.3 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 DWM_WINDOW_CORNER_PREFERENCE {
DWMWCP_DEFAULT = 0,
DWMWCP_DONOTROUND = 1,
DWMWCP_ROUND = 2,
DWMWCP_ROUNDSMALL = 3,
};
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 INVALID_FILE_ATTRIBUTES
#define INVALID_FILE_ATTRIBUTES 0xFFFFFFFF
#endif
#ifndef STACK_SIZE_PARAM_IS_A_RESERVATION
#define STACK_SIZE_PARAM_IS_A_RESERVATION 0x10000L
#endif
#ifndef WPF_ASYNCWINDOWPLACEMENT
#define WPF_ASYNCWINDOWPLACEMENT 0x0004
#endif
#ifndef WM_DPICHANGED
#define WM_DPICHANGED 0x02E0
#endif
#if defined(_MSC_VER) && _MSC_VER <= 1200
#define InterlockedIncrement(x) InterlockedIncrement((LONG*)(x))
#define InterlockedDecrement(x) InterlockedDecrement((LONG*)(x))
#endif
#ifndef SCANCODE_SIMULATED
#define SCANCODE_SIMULATED 0x0200
#endif
#ifndef EVENT_SYSTEM_MOVESIZESTART
#define EVENT_SYSTEM_MOVESIZESTART 0x000A
#define EVENT_SYSTEM_MOVESIZEEND 0x000B
#endif
#ifndef MSAA_MENU_SIG
#define MSAA_MENU_SIG 0xAA0DF00D
typedef struct tagMSAAMENUINFO {
DWORD dwMSAASignature;
DWORD cchWText;
LPWSTR pszWText;
} MSAAMENUINFO,*LPMSAAMENUINFO;
#endif
#ifndef GetWindowLongPtr
#define GetWindowLongPtr GetWindowLong
#define SetWindowLongPtr SetWindowLong
#define GetClassLongPtr GetClassLong
#define SetClassLongPtr SetClassLong
#define GWLP_WNDPROC (-4)
#define GWLP_HINSTANCE (-6)
#define GWLP_HWNDPARENT (-8)
#define GWLP_USERDATA (-21)
#define GWLP_ID (-12)
#endif
#ifndef PROCESS_SUSPEND_RESUME
#define PROCESS_SUSPEND_RESUME (0x0800)
#endif
#ifndef WS_EX_LAYERED
#define WS_EX_LAYERED 0x00080000
#define WS_EX_NOACTIVATE 0x08000000
#define LWA_COLORKEY 0x00000001
#define LWA_ALPHA 0x00000002
#endif
#ifndef WM_XBUTTONDOWN
#define GET_WHEEL_DELTA_WPARAM(wParam) ((short)HIWORD(wParam))
#define WM_XBUTTONDOWN 0x020B
#define WM_XBUTTONUP 0x020C
#define WM_XBUTTONDBLCLK 0x020D
#define WM_MOUSEHWHEEL 0x020e
#define VK_XBUTTON1 0x05
#define VK_XBUTTON2 0x06
#define VK_VOLUME_MUTE 0xAD
#define VK_VOLUME_DOWN 0xAE
#define VK_VOLUME_UP 0xAF
#define MOUSEEVENTF_XDOWN 0x0080
#define MOUSEEVENTF_XUP 0x0100
#define KEYEVENTF_UNICODE 0x0004
#define KEYEVENTF_SCANCODE 0x0008
#endif
#ifndef GCLP_HCURSOR
#define GCLP_HCURSOR (-12)
#define GCLP_HICON (-14)
#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];
HANDLE h;
va_start( arglist, fmt );
wvsprintfA( str, fmt, arglist );
va_end( arglist );
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];
DWORD dummy;
lstrcat_sA(str, ARR_SZ(str), " (");
lstrcat_sA(str, ARR_SZ(str), itostrA(lerr, lerrorstr, 10));
lstrcat_sA(str, ARR_SZ(str), ")\n");
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
#if !defined(__STDC_VERSION__) || __STDC_VERSION__ <= 202300L
#ifndef static_assert
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
// C11 cool _Static_assert
#define static_assert _Static_assert
#else
#define static_assert(x, y) enum assert_static__ { assert_static___ = 1/(x) };
#endif
#endif //static_assert
#endif // [C89 - C23[
/* 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 */
(TCHAR *)&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[AT_LEAST 512+40+2*8+4*12+1])
{
TCHAR klass[256]=TEXT(""), title[256]=TEXT("");
RECT rc;
GetWindowRect(hwnd, &rc);
GetClassName(hwnd, klass, ARR_SZ(klass));
GetWindowText(hwnd, title, ARR_SZ(title));
return wsprintf(buf
, TEXT("Hwnd=%x, %s|%s, style=%x, xstyle=%x, rect=%ld,%ld,%ld,%ld")
, (UINT)(UINT_PTR)hwnd
, title, klass
, (UINT)GetWindowLongPtr(hwnd, GWL_STYLE)
, (UINT)GetWindowLongPtr(hwnd, GWL_EXSTYLE)
, rc.left, rc.top, rc.right, rc.bottom
);
}
/* 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)
{
typedef HWND (WINAPI *funk_t)(HWND hwnd, UINT gaFlags);
static funk_t funk = (funk_t)IPTR;
HWND hlast, hprevious;
LONG wlong;
if(!hwnd) return NULL;
hprevious = hwnd;
if (funk == (funk_t)IPTR) {
funk = (funk_t)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;
}
#ifndef MSGFLTINFO_NONE
#define MSGFLTINFO_NONE 0
#define MSGFLTINFO_ALREADYALLOWED_FORWND 1
#define MSGFLTINFO_ALREADYDISALLOWED_FORWND 2
#define MSGFLTINFO_ALLOWED_HIGHER 3
typedef struct tagCHANGEFILTERSTRUCT {
DWORD cbSize;
DWORD ExtStatus;
} CHANGEFILTERSTRUCT, *PCHANGEFILTERSTRUCT;
#endif
// On Windows Vista we have to use this one that applies process wide.
static BOOL ChangeWindowMessageFilterL(UINT msg, DWORD ac)
{
typedef BOOL (WINAPI* funk_t)(UINT msg, DWORD ac);
static funk_t funk = (funk_t)IPTR;
if (funk == (funk_t)IPTR) {
funk = (funk_t)LoadDLLProc("USER32.DLL", "ChangeWindowMessageFilter");
}
if (funk) { /* We know we have the function */
return funk(msg, ac);
}
return FALSE;
}
// Available only on Windows 7+s
static BOOL ChangeWindowMessageFilterExL(HWND hwnd, UINT msg, DWORD ac, PCHANGEFILTERSTRUCT pC)
{
typedef BOOL (WINAPI *funk_t)(HWND hwnd, UINT msg, DWORD ac, PCHANGEFILTERSTRUCT pC);
static funk_t funk = (funk_t)IPTR;
if (funk == (funk_t)IPTR) {
funk = (funk_t)LoadDLLProc("USER32.DLL", "ChangeWindowMessageFilterEx");
}
if (funk) { /* We know we have the function */
return funk(hwnd, msg, ac, pC);
}
// Process-wide Fallback for Windows Vista!
return ChangeWindowMessageFilterL(msg, ac);
}
static BOOL GetLayeredWindowAttributesL(HWND hwnd, COLORREF *pcrKey, BYTE *pbAlpha, DWORD *pdwFlags)
{
typedef BOOL (WINAPI *funk_t)(HWND hwnd, COLORREF *pcrKey, BYTE *pbAlpha, DWORD *pdwFlags);
static funk_t funk = (funk_t)IPTR;
if (funk == (funk_t)IPTR) {
funk = (funk_t)LoadDLLProc("USER32.DLL", "GetLayeredWindowAttributes");
}
if (funk) { /* We know we have the function */
return funk(hwnd, pcrKey, pbAlpha, pdwFlags);
}
return FALSE;
}
static BOOL SetLayeredWindowAttributesL(HWND hwnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags)
{
typedef BOOL (WINAPI *funk_t)(HWND hwnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags);
static funk_t funk = (funk_t)IPTR;
if (funk == (funk_t)IPTR) {
funk= (funk_t)LoadDLLProc("USER32.DLL", "SetLayeredWindowAttributes");
}
if(funk) { /* We know we have the function */
return funk(hwnd, crKey, bAlpha, dwFlags);
}
return FALSE;
}
static BOOL GetMonitorInfoL(HMONITOR hMonitor, LPMONITORINFO lpmi)
{
typedef BOOL (WINAPI *funk_t)(HMONITOR hMonitor, LPMONITORINFO lpmi);
static funk_t funk = (funk_t)IPTR;
if (funk == (funk_t)IPTR) {
#ifdef _UNICODE
funk = (funk_t)LoadDLLProc("USER32.DLL", "GetMonitorInfoW");
#else
funk = (funk_t)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;
}
static BOOL EnumDisplayMonitorsL(HDC hdc, LPCRECT lprcClip, MONITORENUMPROC lpfnEnum, LPARAM dwData)
{
typedef BOOL (WINAPI *funk_t)(HDC hdc, LPCRECT lprcClip, MONITORENUMPROC lpfnEnum, LPARAM dwData);
static funk_t funk = (funk_t)IPTR;
MONITORINFO mi;
if (funk == (funk_t)IPTR) { /* First time */
funk= (funk_t)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;
}
static HMONITOR MonitorFromPointL(POINT pt, DWORD dwFlags)
{
typedef HMONITOR (WINAPI *funk_t)(POINT pt, DWORD dwFlags);
static funk_t funk=(funk_t)IPTR;
if (funk == (funk_t)IPTR) { /* First time */
funk = (funk_t)LoadDLLProc("USER32.DLL", "MonitorFromPoint");
}
if (funk) { /* We know we have the function */
return funk(pt, dwFlags);
}
return NULL;
}
static HMONITOR MonitorFromWindowL(HWND hwnd, DWORD dwFlags)
{
typedef HMONITOR (WINAPI *funk_t)(HWND hwnd, DWORD dwFlags);
static funk_t funk=(funk_t)IPTR;
if (funk == (funk_t)IPTR) { /* First time */
funk = (funk_t)LoadDLLProc("USER32.DLL", "MonitorFromWindow");
}
if (funk) { /* We know we have the function */
return funk(hwnd, dwFlags);
}
return NULL;
}
static BOOL GetGUIThreadInfoL(DWORD pid, GUITHREADINFO *lpgui)
{
typedef BOOL (WINAPI *funk_t)(DWORD pid, GUITHREADINFO *lpgui);
static funk_t funk=(funk_t)IPTR;
if (funk == (funk_t)IPTR) { /* First time */
funk = (funk_t)LoadDLLProc("USER32.DLL", "GetGUIThreadInfo");
}
if (funk) { /* We know we have the function */
return funk(pid, lpgui);
}
return FALSE;
}
static int GetSystemMetricsForDpiL(int nIndex, UINT dpi)
{
typedef int (WINAPI *funk_t)(int nIndex, UINT dpi);
static funk_t funk=(funk_t)IPTR;
if (dpi) {
if (funk == (funk_t)IPTR) { /* First time */
funk = (funk_t)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);
}
#define GetSystemMetricsForDpi GetSystemMetricsForDpiL
static HRESULT GetDpiForMonitorL(HMONITOR hmonitor, int dpiType, UINT *dpiX, UINT *dpiY)
{
typedef HRESULT (WINAPI *funk_t)(HMONITOR hmonitor, int dpiType, UINT *dpiX, UINT *dpiY);
static funk_t funk=(funk_t)IPTR;
if (funk == (funk_t)IPTR) { /* First time */
funk = (funk_t)LoadDLLProc("SHCORE.DLL", "GetDpiForMonitor");
}
if (funk) { /* We know we have the function */
return funk(hmonitor, dpiType, dpiX, dpiY);
}
return 666; /* Fail with 666 error */
}
/* Supported wince Windows 10, version 1607 [desktop apps only] */
static UINT GetDpiForWindowL(const HWND hwnd)
{
typedef UINT (WINAPI *funk_t)(const HWND hwnd);
static funk_t funk=(funk_t)IPTR;
if (funk == (funk_t)IPTR) { /* First time */
funk = (funk_t)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 */
}
#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 */
typedef BOOL (WINAPI *funk_t)(HWND hwnd);
static funk_t funk=(funk_t)IPTR;
if (funk == (funk_t)IPTR) { /* First time */
funk = (funk_t)LoadDLLProc("USER32.DLL", "EnableNonClientDpiScaling");
}
if (funk) { /* We know we have the function */
return funk(hwnd);
}
return FALSE;
}
/* Only applies to Windows NT for build number */
static xpure BOOL OredredWinVer()
{
DWORD oVer;
DWORD ver = GetVersion();
oVer = (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.
oVer |= 0xFFFF0000;
}
return oVer;
}
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)(UINT_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)
{
typedef BOOL (WINAPI *funk_t)(UINT uiAction, UINT uiParam, PVOID pvParam, UINT fWinIni, UINT dpi);
static funk_t funk=(funk_t)IPTR;
if (dpi) {
if (funk == (funk_t)IPTR) { /* First time */
funk = (funk_t)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);
}
#define SystemParametersInfoForDpi SystemParametersInfoForDpiL
static HWINEVENTHOOK SetWinEventHookL(
DWORD eventMin, DWORD eventMax
, HMODULE hmodWinEventProc
, WINEVENTPROC pfnWinEventProc
, DWORD idProcess, DWORD idThread, DWORD dwFlags)
{
typedef HWINEVENTHOOK (WINAPI *funk_t)(DWORD evm, DWORD evM, HMODULE hmod, WINEVENTPROC wevp, DWORD idProcess, DWORD idThread, DWORD dwFlags);
static funk_t funk=(funk_t)IPTR;
if (funk == (funk_t)IPTR) { /* First time */
funk = (funk_t)LoadDLLProc("USER32.DLL", "SetWinEventHook");
}
if (funk) { /* We know we have the function */
return funk(eventMin, eventMax, hmodWinEventProc
, pfnWinEventProc, idProcess, idThread, dwFlags);
}
/* Failed */
return NULL;
}
static BOOL UnhookWinEventL(HWINEVENTHOOK hWinEventHook)
{
typedef BOOL (WINAPI *funk_t)(HWINEVENTHOOK hWinEventHook);
static funk_t funk=(funk_t)IPTR;
if (funk == (funk_t)IPTR) { /* First time */
funk = (funk_t)LoadDLLProc("USER32.DLL", "UnhookWinEvent");
}
if (funk) { /* We know we have the function */
return funk(hWinEventHook);
}
/* Failed */
return FALSE;
}
#ifndef WIN64
static void NotifyWinEventL(DWORD event, HWND hwnd, LONG idObj, LONG idChild)
{
typedef void (WINAPI *funk_t)(DWORD, HWND, LONG, LONG);
static funk_t funk = (funk_t)1;
if (funk == (funk_t)1)
funk = (funk_t)LoadDLLProc("USER32.DLL", "NotifyWinEvent");
if (funk)
funk(event, hwnd, idObj, idChild);
}
#define NotifyWinEvent NotifyWinEventL
#endif
static HRESULT DwmGetWindowAttributeL(HWND hwnd, DWORD a, PVOID b, DWORD c)
{
typedef HRESULT (WINAPI *funk_t)(HWND hwnd, DWORD a, PVOID b, DWORD c);
static funk_t funk=(funk_t)IPTR;
if (funk == (funk_t)IPTR) { /* First time */
funk = (funk_t)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 */
}
static HRESULT DwmSetWindowAttributeL(HWND hwnd, DWORD a, PVOID b, DWORD c)
{
typedef HRESULT (WINAPI *funk_t)(HWND hwnd, DWORD a, PVOID b, DWORD c);
static funk_t funk=(funk_t)IPTR;
if (funk == (funk_t)IPTR) { /* First time */
funk = (funk_t)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 */
}
static HRESULT DwmGetColorizationColorL(DWORD *a, BOOL *b)
{
typedef HRESULT (WINAPI *funk_t)(DWORD *a, BOOL *b);
static funk_t funk=(funk_t)IPTR;
if (funk == (funk_t)IPTR) { /* First time */
funk = (funk_t)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 */
}
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;
}
static long xpure average(long a, long b)
{
return (a>>1) + (b>>1) + ( ((a&1) + (b&1))>>1 );
}
static long xpure averageX(const RECT *rc)
{
return average(rc->left, rc->right);
}
static long xpure averageY(const RECT *rc)
{
return average(rc->top, rc->bottom);
}
/* #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);
// if (IsZoomed(hwnd)) OffsetRect(bbb, 10, 10); // Test for Zoomed stuff
}
/* 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)
{