From 1e36f164d772da3a386aaf5893ab01da1118846d Mon Sep 17 00:00:00 2001 From: krlvm <51774833+krlvm@users.noreply.github.com> Date: Thu, 14 Jul 2022 21:01:24 +0400 Subject: [PATCH] Don't re-apply colorization if the accent color has not been changed Mitigation for Windows bug - since Windows 10 1809+ WM_DWMCOLORIZATIONCOLORCHANGED is sent multiple times --- AccentColorizer/AccentColorHelper.cpp | 12 ++++++++++-- AccentColorizer/AccentColorHelper.h | 2 +- AccentColorizer/AccentColorizer.cpp | 20 ++++++++++++++++++-- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/AccentColorizer/AccentColorHelper.cpp b/AccentColorizer/AccentColorHelper.cpp index 151129c..0462b71 100644 --- a/AccentColorizer/AccentColorHelper.cpp +++ b/AccentColorizer/AccentColorHelper.cpp @@ -4,12 +4,20 @@ COLORREF accent; -void UpdateAccentColors() +bool UpdateAccentColors() { COLORREF dwAccentRGB; BOOL bIsColorOpaque; DwmGetColorizationColor(&dwAccentRGB, &bIsColorOpaque); - accent = RGB2BGR(dwAccentRGB); + DWORD dwAccent = RGB2BGR(dwAccentRGB); + + if (accent == dwAccent) + { + return false; + } + + accent = dwAccent; + return true; } diff --git a/AccentColorizer/AccentColorHelper.h b/AccentColorizer/AccentColorHelper.h index fe98aeb..5d9d39e 100644 --- a/AccentColorizer/AccentColorHelper.h +++ b/AccentColorizer/AccentColorHelper.h @@ -3,4 +3,4 @@ extern COLORREF accent; -void UpdateAccentColors(); +bool UpdateAccentColors(); diff --git a/AccentColorizer/AccentColorizer.cpp b/AccentColorizer/AccentColorizer.cpp index 4f16d23..2114fac 100644 --- a/AccentColorizer/AccentColorizer.cpp +++ b/AccentColorizer/AccentColorizer.cpp @@ -9,17 +9,33 @@ const LPCWSTR szWindowClass = L"ACCENTCOLORIZER"; HANDLE hHandle; void ApplyAccentColorization() { - UpdateAccentColors(); + if (!UpdateAccentColors()) + { + // Accent Colors have not been changed. + // There's a bug in Windows 10 1809+ because of which + // WM_DWMCOLORIZATIONCOLORCHANGED message is sent multiple times, + // it also affects accent color changing performance in general. + // Apparently it is fixed in Windows 11 version 22H2 + return; + } ModifySysColors(accent); ModifyStyles(accent); } +#include LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { if (message == WM_DWMCOLORIZATIONCOLORCHANGED || message == WM_DPICHANGED || - (message == WM_WTSSESSION_CHANGE && wParam == WTS_SESSION_UNLOCK)) + message == WM_THEMECHANGED || + (message == WM_WTSSESSION_CHANGE && wParam == WTS_SESSION_UNLOCK) + ) { + if (message == WM_THEMECHANGED) + { + // We need to re-apply colorization if visual styles theme has been changed + accent = NULL; + } ApplyAccentColorization(); } return DefWindowProc(hWnd, message, wParam, lParam);