Skip to content

Instantly share code, notes, and snippets.

@rossy
Created September 21, 2019 13:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rossy/ebd83ba8f22339ce25ef68bfc007dfd2 to your computer and use it in GitHub Desktop.
Save rossy/ebd83ba8f22339ce25ef68bfc007dfd2 to your computer and use it in GitHub Desktop.
Windows 10 dark mode
#include <windows.h>
#include <uxtheme.h>
#include <dwmapi.h>
#define DWMWA_USE_IMMERSIVE_DARK_MODE 19
extern IMAGE_DOS_HEADER __ImageBase;
#define HINST_THISCOMPONENT ((HINSTANCE)&__ImageBase)
struct window {
HWND window;
};
static void handle_nccreate(HWND window, CREATESTRUCTW *cs)
{
struct window *data = cs->lpCreateParams;
SetWindowLongPtrW(window, GWLP_USERDATA, (LONG_PTR)data);
SetWindowTheme(window, L"DarkMode_Explorer", NULL);
DwmSetWindowAttribute(window, DWMWA_USE_IMMERSIVE_DARK_MODE,
&(BOOL) { TRUE }, sizeof(BOOL));
}
static LRESULT CALLBACK window_proc(HWND window, UINT msg, WPARAM wparam,
LPARAM lparam)
{
struct window *data = (void *)GetWindowLongPtrW(window, GWLP_USERDATA);
if (!data) {
if (msg == WM_NCCREATE)
handle_nccreate(window, (CREATESTRUCTW *)lparam);
return DefWindowProcW(window, msg, wparam, lparam);
}
switch (msg) {
case WM_CLOSE:
DestroyWindow(window);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProcW(window, msg, wparam, lparam);
}
int main()
{
ATOM cls = RegisterClassExW(&(WNDCLASSEXW) {
.cbSize = sizeof(WNDCLASSEXW),
.lpfnWndProc = window_proc,
.hInstance = HINST_THISCOMPONENT,
.hCursor = LoadCursor(NULL, IDC_ARROW),
.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1),
.lpszClassName = L"dark-mode-test",
});
struct window *data = calloc(1, sizeof(struct window));
data->window = CreateWindowExW(WS_EX_APPWINDOW, (LPWSTR)MAKEINTATOM(cls),
L"Dark Mode Test", WS_OVERLAPPEDWINDOW | WS_SIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT, 200, 200, NULL, NULL,
HINST_THISCOMPONENT, data);
ShowWindow(data->window, SW_SHOWDEFAULT);
UpdateWindow(data->window);
MSG message;
while (GetMessageW(&message, NULL, 0, 0)) {
TranslateMessage(&message);
DispatchMessageW(&message);
}
free(data);
UnregisterClassW((LPWSTR)MAKEINTATOM(cls), HINST_THISCOMPONENT);
return message.wParam;
}
@Yakov5776
Copy link

Above 20H1 uses 20 not 19

@Yakov5776
Copy link

You have to make a build check

@joshuasanchez6867
Copy link

Why doesnt your code work on my end?
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment