44
44
WM_UAHNCPAINTMENUPOPUP = 0x0095
45
45
};
46
46
47
+ // undocumented app mode enum for the private SetPreferredAppMode API
48
+ enum class PreferredAppMode
49
+ {
50
+ Default,
51
+ AllowDark,
52
+ ForceDark,
53
+ ForceLight,
54
+ Max
55
+ };
56
+
47
57
// describes the sizes of the menu bar or menu item
48
58
typedef union tagUAHMENUITEMMETRICS
49
59
{
@@ -116,11 +126,10 @@ typedef struct {
116
126
HBRUSH menubaritem_bgbrush_selected;
117
127
} theme_cfg;
118
128
129
+ // global variables
119
130
static HTHEME g_menuTheme = nullptr ;
120
131
HHOOK g_hook = nullptr ;
121
132
122
- LRESULT CALLBACK CallWndSubClassProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData);
123
-
124
133
bool IsWndClass (HWND hWnd, const TCHAR* classname) {
125
134
TCHAR buf[512 ];
126
135
GetClassName (hWnd, buf, 512 );
@@ -169,8 +178,8 @@ const theme_cfg* LoadThemeConfig() {
169
178
configFile << " menubar_textcolor_disabled = 160,160,160" << std::endl;
170
179
configFile << " menubar_bgcolor = 48,48,48" << std::endl;
171
180
configFile << " menubaritem_bgcolor = 48,48,48" << std::endl;
172
- configFile << " menubaritem_bgcolor_hot = 48,48,48 " << std::endl;
173
- configFile << " menubaritem_bgcolor_selected = 48,48,48 " << std::endl;
181
+ configFile << " menubaritem_bgcolor_hot = 62,62,62 " << std::endl;
182
+ configFile << " menubaritem_bgcolor_selected = 62,62,62 " << std::endl;
174
183
configFile.close ();
175
184
}
176
185
@@ -208,38 +217,41 @@ const theme_cfg* LoadThemeConfig() {
208
217
// https://stackoverflow.com/questions/39261826/change-the-color-of-the-title-bar-caption-of-a-win32-application
209
218
// https://gist.github.com/rounk-ctrl/b04e5622e30e0d62956870d5c22b7017
210
219
// https://github.com/microsoft/WindowsAppSDK/issues/41
220
+ // https://gist.github.com/ericoporto/1745f4b912e22f9eabfce2c7166d979b
211
221
void EnableDarkMode (HWND hWnd) {
212
222
213
- const BOOL USE_DARK_MODE = true ;
214
-
215
- DwmSetWindowAttribute (hWnd,
216
- DWMWA_USE_IMMERSIVE_DARK_MODE,
217
- &USE_DARK_MODE,
218
- sizeof (USE_DARK_MODE));
223
+ // apply dark mode to the window
224
+ {
225
+ const BOOL USE_DARK_MODE = true ;
219
226
220
- static HMODULE hUxtheme = nullptr ;
227
+ DwmSetWindowAttribute (hWnd,
228
+ DWMWA_USE_IMMERSIVE_DARK_MODE,
229
+ &USE_DARK_MODE,
230
+ sizeof (USE_DARK_MODE));
231
+ }
221
232
222
- enum class PreferredAppMode
233
+ // apply dark mode to the context menus
223
234
{
224
- Default,
225
- AllowDark,
226
- ForceDark,
227
- ForceLight,
228
- Max
229
- };
230
-
231
- // ordinal 135, in 1903
232
- using fnSetPreferredAppMode = PreferredAppMode (WINAPI*)(PreferredAppMode appMode);
233
- fnSetPreferredAppMode SetPreferredAppMode;
234
-
235
- if (!hUxtheme) {
236
- hUxtheme = LoadLibraryExW (L" uxtheme.dll" , nullptr , LOAD_LIBRARY_SEARCH_SYSTEM32);
237
- }
235
+ using fnSetPreferredAppMode = PreferredAppMode (WINAPI*)(PreferredAppMode appMode);
236
+ fnSetPreferredAppMode SetPreferredAppMode;
237
+
238
+ static HMODULE hUxtheme = nullptr ;
239
+
240
+ if (!hUxtheme) {
241
+ hUxtheme = LoadLibraryExW (L" uxtheme.dll" , nullptr , LOAD_LIBRARY_SEARCH_SYSTEM32);
242
+ }
238
243
239
- SetPreferredAppMode = (fnSetPreferredAppMode)GetProcAddress (hUxtheme, MAKEINTRESOURCEA (135 ));
240
- SetPreferredAppMode (PreferredAppMode::ForceDark);
244
+ if (hUxtheme) {
245
+ // #135 is the ordinal for SetPreferredAppMode private API
246
+ // which is available in uxtheme.dll since Windows 10 1903+
247
+ SetPreferredAppMode = (fnSetPreferredAppMode)GetProcAddress (hUxtheme, MAKEINTRESOURCEA (135 ));
248
+ SetPreferredAppMode (PreferredAppMode::ForceDark);
249
+ }
250
+ }
241
251
}
242
252
253
+ LRESULT CALLBACK CallWndSubClassProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData);
254
+
243
255
LRESULT CALLBACK CBTProc (int nCode, WPARAM wParam, LPARAM lParam) {
244
256
switch (nCode) {
245
257
case HCBT_CREATEWND:
@@ -417,6 +429,7 @@ LRESULT CALLBACK CallWndSubClassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM
417
429
return TRUE ;
418
430
}
419
431
}
432
+ break ;
420
433
}
421
434
case WM_NCACTIVATE:
422
435
{
@@ -600,6 +613,7 @@ bool APIENTRY DllMain(HMODULE hModule, DWORD reason, LPVOID lpRes) {
600
613
switch (reason)
601
614
{
602
615
case DLL_PROCESS_ATTACH: {
616
+ // enable dark mode for the current process window
603
617
EnableDarkMode (nullptr );
604
618
605
619
// catch windows that have been created before we set up the CBTProc hook
@@ -611,6 +625,7 @@ bool APIENTRY DllMain(HMODULE hModule, DWORD reason, LPVOID lpRes) {
611
625
EnableDarkMode (hWnd);
612
626
}
613
627
628
+ // set up the CBTProc hook to catch new windows
614
629
g_hook = SetWindowsHookEx (WH_CBT, CBTProc, nullptr , GetCurrentThreadId ());
615
630
break ;
616
631
}
0 commit comments