Skip to content

Commit

Permalink
Auto Dark/Light mode change.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChGen committed Aug 27, 2023
1 parent 8ef7a7a commit 0ba64d5
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion DarkTitle/DarkTitle.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <dwmapi.h>
#include <unordered_map>
#include <algorithm>

#pragma comment(lib, "dwmapi.lib")

//Non-public Win32 APIs. Credits to:
//https://github.com/ysc3839/win32-darkmode/blob/master/win32-darkmode/DarkMode.h
//https://gist.github.com/rounk-ctrl/b04e5622e30e0d62956870d5c22b7017
Expand All @@ -21,12 +24,32 @@ struct WINDOWCOMPOSITIONATTRIBDATA
SIZE_T cbData;
};

#ifndef DWMWA_USE_IMMERSIVE_DARK_MODE
#define DWMWA_USE_IMMERSIVE_DARK_MODE 20
#endif

using fnSetWindowCompositionAttribute = BOOL(WINAPI*)(HWND hWnd, WINDOWCOMPOSITIONATTRIBDATA*);
fnSetWindowCompositionAttribute _SetWindowCompositionAttribute = nullptr;

bool _isLight = false;
bool _mark = false;
std::unordered_map<HWND, bool> _titles;

bool isLightTheme() {
DWORD value = 0;
DWORD cbData = static_cast<DWORD>(sizeof(value));
auto res = RegGetValueW(
HKEY_CURRENT_USER,
L"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize",
L"AppsUseLightTheme",
RRF_RT_REG_DWORD, NULL, &value, &cbData
);
if (res != ERROR_SUCCESS) {
return true;
}
return value == 1;
}

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
Expand All @@ -45,17 +68,21 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
BOOL dark = TRUE;
WINDOWCOMPOSITIONATTRIBDATA data = { WCA_USEDARKMODECOLORS, &dark, sizeof(dark) };
SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_LOWEST);
WNDENUMPROC proc = [](HWND hWnd, LPARAM lParam)
{
BOOL val = !_isLight;
if (_titles.find(hWnd) == _titles.cend())
_SetWindowCompositionAttribute(hWnd, (WINDOWCOMPOSITIONATTRIBDATA*)lParam);
DwmSetWindowAttribute(hWnd, DWMWA_USE_IMMERSIVE_DARK_MODE, &val, sizeof(val));
_titles[hWnd] = _mark;
return TRUE;
};

_isLight = isLightTheme();
while (true)
{
EnumWindows(proc, (LPARAM)&data);

for (auto it = _titles.begin(); it != _titles.end();)
{
if (it->second != _mark)
Expand All @@ -65,6 +92,12 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
}
Sleep(75);
_mark = !_mark;
bool status = isLightTheme();
if (status != _isLight)
{
_isLight = status;
_titles.clear();
}
}
return 0;
}

0 comments on commit 0ba64d5

Please sign in to comment.