-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdark-window-titlebar.cpp
More file actions
309 lines (257 loc) · 9.84 KB
/
dark-window-titlebar.cpp
File metadata and controls
309 lines (257 loc) · 9.84 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
// ==WindhawkMod==
// @id dark-window-titlebar
// @name Dark Window Titlebar for Win32 App
// @description Hack to enable dark titlebar for win32 app when system darkmode is on.
// @version 1.0.0
// @author Fifv
// @github https://github.com/fifv
// @include dopus.exe
// @include dopusrt.exe
// @include GoldenDict.exe
// @include SyncTrayzor.exe
// @include Proxifier.exe
// @include javaw.exe
// @include tabby.exe
// @include EXE64.exe
// @include Notion.exe
// @include Bandizip.exe
// @compilerOptions -ldwmapi
// ==/WindhawkMod==
// ==WindhawkModReadme==
/*
Hack to enable dark titlebar for win32 app when system darkmode is on.
### Usage
Add any processes you want to enable dark titlebar in **Advanced** > **Custom
process inclusion list**
Only hook on apps creating window, so apps may needed to be restarted.
### Credit
I learnt how to do this from
https://learn.microsoft.com/en-us/windows/apps/desktop/modernize/apply-windows-themes#enable-a-dark-mode-title-bar-for-win32-applications
*/
// ==/WindhawkModReadme==
// ==WindhawkModSettings==
// ==/WindhawkModSettings==
#include <dwmapi.h>
#include <libloaderapi.h>
#include <minwindef.h>
#include <processthreadsapi.h>
#include <windhawk_api.h>
#include <windows.h>
#include <chrono>
#include <string>
#include <thread>
#include <vector>
#ifndef DWMWA_USE_IMMERSIVE_DARK_MODE
#define DWMWA_USE_IMMERSIVE_DARK_MODE 20
#endif
auto hWnds = std::vector<HWND>();
DWORD g_pid;
BOOL shouldHackDwmDarkTitlebar(HWND hWnd) {
if (!IsWindow(hWnd)) {
return FALSE;
}
BOOL oldDwmValue;
::DwmGetWindowAttribute(hWnd, DWMWA_USE_IMMERSIVE_DARK_MODE, &oldDwmValue,
sizeof(oldDwmValue));
auto isLightTitlebar = oldDwmValue == 0;
// BOOL isTopLevel = !(GetWindowLong(hWnd, GWL_STYLE) & WS_CHILD);
WCHAR _buffer_moduleName[MAX_PATH] = {0};
GetWindowModuleFileNameW(hWnd, _buffer_moduleName, MAX_PATH);
auto moduleName = std::wstring(_buffer_moduleName);
WCHAR _buffer_className[MAX_PATH] = {0};
GetClassNameW(hWnd, _buffer_className, MAX_PATH);
auto className = std::wstring(_buffer_className);
// BOOL isIME = moduleName.ends_with(L"ime") ||
// moduleName.ends_with(L"MSCTF.dll") ||
// className.find(L"IME") != std::string::npos;
// return isTopLevel && !isIME && isLightTitlebar;
auto style = GetWindowLongPtrW(hWnd, GWL_STYLE);
auto hasBorder = (style & WS_CAPTION) != 0;
// Wh_Log(
// L"hWnd %#08x, hasBorder %d, isLightTitlebar %d, ClassName: %s, "
// L"moduleName: %s",
// hWnd, hasBorder, isLightTitlebar, _buffer_className,
// _buffer_moduleName);
// auto isVisible = (style & WS_VISIBLE) != 0; /* Don't check this, some
// windows will be invisible first... */
return hasBorder && isLightTitlebar;
}
void hackDwmDarkTitlebar(HWND hWnd) {
// Wh_Log(L"oldDwmValue: %d, hWnd: %#08x, top: %d, module: %s, isIME:
// %d",
// oldDwmValue, hWnd, isTopLevel, moduleName, isIME);
hWnds.push_back(hWnd);
BOOL dwmValue = TRUE;
::DwmSetWindowAttribute(hWnd, DWMWA_USE_IMMERSIVE_DARK_MODE, &dwmValue,
sizeof(dwmValue));
}
void unhackDwmDarkTitlebar(HWND hWnd) {
/*
This must be checked, otherwise apps will crash
*/
if (!IsWindow(hWnd)) {
return;
}
BOOL dwmValue = FALSE;
::DwmSetWindowAttribute(hWnd, DWMWA_USE_IMMERSIVE_DARK_MODE, &dwmValue,
sizeof(dwmValue));
}
BOOL CALLBACK InitEnumWindowsCallback(HWND hWnd, LPARAM lParam) {
// Wh_Log(L"InitEnumWindowsCallback");
DWORD processId;
GetWindowThreadProcessId(hWnd, &processId);
if (processId == g_pid) {
// Wh_Log(L"PID: %llu", processId);
if (shouldHackDwmDarkTitlebar(hWnd)) {
WCHAR _buffer_moduleName[MAX_PATH] = {0};
GetWindowModuleFileNameW(hWnd, _buffer_moduleName, MAX_PATH);
WCHAR _buffer_className[MAX_PATH] = {0};
GetClassNameW(hWnd, _buffer_className, MAX_PATH);
Wh_Log(L"HACK hWnd: %#08x, ClassName: %s, moduleName: %s", hWnd,
_buffer_className, _buffer_moduleName);
hackDwmDarkTitlebar(hWnd);
/*
same
*/
// auto hInst = (HMODULE)GetWindowLongPtrW(hWnd, GWLP_HINSTANCE);
// Wh_Log(L"hinst: %#08x", hInst);
// WCHAR _buffer[MAX_PATH] = {0};
// GetModuleFileNameW(hInst, _buffer, MAX_PATH);
// Wh_Log(L"%s", _buffer);
}
}
return TRUE;
}
using CreateWindowExW_t = decltype(&CreateWindowExW);
CreateWindowExW_t CreateWindowExW_Original;
HWND WINAPI CreateWindowExW_Hook(DWORD dwExStyle,
LPCWSTR lpClassName,
LPCWSTR lpWindowName,
DWORD dwStyle,
int X,
int Y,
int nWidth,
int nHeight,
HWND hWndParent,
HMENU hMenu,
HINSTANCE hInstance,
LPVOID lpParam) {
HWND hWnd = CreateWindowExW_Original(dwExStyle, lpClassName, lpWindowName,
dwStyle, X, Y, nWidth, nHeight,
hWndParent, hMenu, hInstance, lpParam);
if (!hWnd) {
return hWnd;
}
if (shouldHackDwmDarkTitlebar(hWnd)) {
Wh_Log(L"HACK hWnd: %#08x", hWnd);
hackDwmDarkTitlebar(hWnd);
}
return hWnd;
}
using CreateDialogIndirectParamW_t = decltype(&CreateDialogIndirectParamW);
CreateDialogIndirectParamW_t CreateDialogIndirectParamW_Original;
HWND WINAPI CreateDialogIndirectParamW_Hook(HINSTANCE hInstance,
LPCDLGTEMPLATEW lpTemplate,
HWND hWndParent,
DLGPROC lpDialogFunc,
LPARAM dwInitParam) {
HWND hWnd = CreateDialogIndirectParamW_Original(
hInstance, lpTemplate, hWndParent, lpDialogFunc, dwInitParam);
if (!hWnd) {
return hWnd;
}
if (shouldHackDwmDarkTitlebar(hWnd)) {
Wh_Log(L"HACK hWnd: %#08x", hWnd);
hackDwmDarkTitlebar(hWnd);
}
return hWnd;
}
using CreateDialogParamW_t = decltype(&CreateDialogParamW);
CreateDialogParamW_t CreateDialogParamW_Original;
HWND WINAPI CreateDialogParamW_Hook(HINSTANCE hInstance,
LPCWSTR lpTemplateName,
HWND hWndParent,
DLGPROC lpDialogFunc,
LPARAM dwInitParam) {
HWND hWnd = CreateDialogParamW_Original(
hInstance, lpTemplateName, hWndParent, lpDialogFunc, dwInitParam);
if (!hWnd) {
return hWnd;
}
if (shouldHackDwmDarkTitlebar(hWnd)) {
Wh_Log(L"HACK hWnd: %#08x", hWnd);
hackDwmDarkTitlebar(hWnd);
}
return hWnd;
}
DLGPROC Dlgproc_Original;
CALLBACK INT_PTR Dlgproc(HWND unnamedParam1,
UINT unnamedParam2,
WPARAM unnamedParam3,
LPARAM unnamedParam4) {
switch (unnamedParam2) {
case WM_INITDIALOG: {
/*
oHHHHH WORKS!!!
*/
EnumWindows(InitEnumWindowsCallback, NULL);
}
}
return Dlgproc_Original(unnamedParam1, unnamedParam2, unnamedParam3,
unnamedParam4);
}
using DialogBoxIndirectParamW_t = decltype(&DialogBoxIndirectParamW);
DialogBoxIndirectParamW_t DialogBoxIndirectParamW_Original;
INT_PTR WINAPI DialogBoxIndirectParamW_Hook(HINSTANCE hInstance,
LPCDLGTEMPLATEW hDialogTemplate,
HWND hWndParent,
DLGPROC lpDialogFunc,
LPARAM dwInitParam) {
Dlgproc_Original = lpDialogFunc;
/*
here the parent is the parent, obviously, not the dialog
*/
Wh_Log(L"aaa, %#08x", hWndParent);
INT_PTR res = DialogBoxIndirectParamW_Original(
hInstance, hDialogTemplate, hWndParent, Dlgproc, dwInitParam);
Wh_Log(L"ohh");
// EnumWindows(InitEnumWindowsCallback, NULL);
return res;
}
BOOL CALLBACK OnModUninit() {
for (std::vector<HWND>::iterator it = hWnds.begin(); it != hWnds.end();
it++) {
unhackDwmDarkTitlebar(*it);
Wh_Log(L"UNHACK hWnd: %#08x", *it);
}
return TRUE;
}
void LoadSettings() {}
BOOL Wh_ModInit() {
// Wh_Log(L"Wh_ModInit");
g_pid = GetCurrentProcessId();
LoadSettings();
Wh_SetFunctionHook((void*)CreateWindowExW, (void*)CreateWindowExW_Hook,
(void**)&CreateWindowExW_Original);
Wh_SetFunctionHook((void*)CreateDialogIndirectParamW,
(void*)CreateDialogIndirectParamW_Hook,
(void**)&CreateDialogIndirectParamW_Original);
Wh_SetFunctionHook((void*)CreateDialogParamW,
(void*)CreateDialogParamW_Hook,
(void**)&CreateDialogParamW_Original);
Wh_SetFunctionHook((void*)DialogBoxIndirectParamW,
(void*)DialogBoxIndirectParamW_Hook,
(void**)&DialogBoxIndirectParamW_Original);
Wh_Log(L"TID: %llu, PID: %llu ", GetCurrentThreadId(),
GetCurrentProcessId());
EnumWindows(InitEnumWindowsCallback, NULL);
return TRUE;
}
void Wh_ModUninit() {
// Wh_Log(L"Wh_ModUninit");
OnModUninit();
}
void Wh_ModSettingsChanged() {
Wh_Log(L"Wh_ModSettingsChanged");
LoadSettings();
}