Improve dark mode detection in wxMSW

Use the registry key corresponding to the selection of "Light" or "Dark"
for the "Colors" in the "Settings" applet, see

https://stackoverflow.com/questions/51334674/how-to-detect-windows-10-light-dark-mode-in-win32-application/

Closes #22020.
This commit is contained in:
QuentinC
2022-01-15 10:51:41 +01:00
committed by Vadim Zeitlin
parent aae203c7b5
commit 622a2f9d60
2 changed files with 28 additions and 1 deletions

View File

@@ -75,12 +75,17 @@ wxString wxSystemAppearance::GetName() const
return wxString();
}
#endif // !__WXOSX__
// These ports implement this function using platform-specific API.
#if !defined(__WXOSX__) && !defined(__WXMSW__)
bool wxSystemAppearance::IsDark() const
{
return IsUsingDarkBackground();
}
#endif // !__WXOSX__
#endif // !__WXOSX__ && !__WXMSW__
bool wxSystemAppearance::IsUsingDarkBackground() const
{

View File

@@ -32,6 +32,7 @@
#include "wx/msw/private.h"
#include "wx/msw/missing.h" // for SM_CXCURSOR, SM_CYCURSOR, SM_TABLETPC
#include "wx/msw/private/metrics.h"
#include "wx/msw/registry.h"
#include "wx/fontutil.h"
#include "wx/fontenum.h"
@@ -359,3 +360,24 @@ extern wxFont wxGetCCDefaultFont()
}
#endif // wxUSE_LISTCTRL || wxUSE_TREECTRL
// There is no official API for determining whether dark mode is being used,
// but // HKCU\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize has
// a value AppsUseLightTheme = 0 for dark mode and 1 for normal mode, so use it
// and fall back to the generic algorithm in IsUsingDarkBackground() if it's
// absent.
//
// Adapted from https://stackoverflow.com/a/51336913/15275 ("How to detect
// Windows 10 light/dark mode in Win32 application?").
bool wxSystemAppearance::IsDark() const
{
wxRegKey rk(wxRegKey::HKCU, "Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize");
if ( rk.Exists() && rk.HasValue("AppsUseLightTheme") )
{
long value = -1;
if ( rk.QueryValue("AppsUseLightTheme", &value) )
return value <= 0;
}
return IsUsingDarkBackground();
}