diff options
| author | Oliver Eftevaag <oliver.eftevaag@qt.io> | 2025-07-23 14:32:16 +0200 |
|---|---|---|
| committer | Oliver Eftevaag <oliver.eftevaag@qt.io> | 2025-07-29 16:45:52 +0200 |
| commit | ca004ffd0c716b7fcbf693517ea416db64338b10 (patch) | |
| tree | 2482e6824c20bd8f67b06b4a890943e05c17c59e /src | |
| parent | 26c24bae7776e7cff83e5ea834e7f679ba59250f (diff) | |
Get accent color from GetSysColor for high contrast without winrt
When Qt for Windows isn't using cpp_winrt, the accent color would
be retreived from the Windows registry. This color would be different
from what native WinUI controls would use, which instead is the
COLOR_HIGHLIGHT value from GetSysColor.
Fix this by using GetSysColor, when a contrast theme is enabled, instead
of the value read from the Windows registry.
Pick-to: 6.10
Done-with: MohammadHossein Qanbari <mohammad.qanbari@qt.io>
Change-Id: If7025d4f52223abd6160feeb065df6b68b8fb2b5
Reviewed-by: Wladimir Leuschner <wladimir.leuschner@qt.io>
Reviewed-by: MohammadHossein Qanbari <mohammad.qanbari@qt.io>
Diffstat (limited to 'src')
| -rw-r--r-- | src/plugins/platforms/windows/qwindowstheme.cpp | 44 | ||||
| -rw-r--r-- | src/plugins/platforms/windows/qwindowstheme.h | 3 |
2 files changed, 28 insertions, 19 deletions
diff --git a/src/plugins/platforms/windows/qwindowstheme.cpp b/src/plugins/platforms/windows/qwindowstheme.cpp index f35d9a81025..64d459aee6b 100644 --- a/src/plugins/platforms/windows/qwindowstheme.cpp +++ b/src/plugins/platforms/windows/qwindowstheme.cpp @@ -95,6 +95,12 @@ static constexpr QColor getSysColor(winrt::Windows::UI::Color &&color) } #endif +static inline QColor getSysColor(int index) +{ + COLORREF cr = GetSysColor(index); + return QColor(GetRValue(cr), GetGValue(cr), GetBValue(cr)); +} + [[maybe_unused]] [[nodiscard]] static inline QColor qt_accentColor(AccentColorLevel level) { #if QT_CONFIG(cpp_winrt) @@ -108,18 +114,28 @@ static constexpr QColor getSysColor(winrt::Windows::UI::Color &&color) const QColor accentDarker = getSysColor(settings.GetColorValue(UIColorType::AccentDark2)); const QColor accentDarkest = getSysColor(settings.GetColorValue(UIColorType::AccentDark3)); #else - const QWinRegistryKey registry(HKEY_CURRENT_USER, LR"(Software\Microsoft\Windows\DWM)"); - if (!registry.isValid()) - return {}; - const QVariant value = registry.value(L"AccentColor"); - if (!value.isValid()) - return {}; - // The retrieved value is in the #AABBGGRR format, we need to - // convert it to the #AARRGGBB format which Qt expects. - const QColor abgr = QColor::fromRgba(qvariant_cast<DWORD>(value)); - if (!abgr.isValid()) + const QColor accent = []()->QColor { + // MS uses the aquatic contrast theme as an example in the URL below: + // https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getsyscolor#windows-1011-system-colors + if (QWindowsTheme::queryHighContrast()) + return getSysColor(COLOR_HIGHLIGHT); + const QWinRegistryKey registry(HKEY_CURRENT_USER, LR"(Software\Microsoft\Windows\DWM)"); + if (!registry.isValid()) + return {}; + const QVariant value = registry.value(L"AccentColor"); + if (!value.isValid()) + return {}; + // The retrieved value is in the #AABBGGRR format, we need to + // convert it to the #AARRGGBB format which Qt expects. + const QColor abgr = QColor::fromRgba(qvariant_cast<DWORD>(value)); + if (!abgr.isValid()) + return {}; + return QColor::fromRgb(abgr.blue(), abgr.green(), abgr.red(), abgr.alpha()); + + }(); + if (!accent.isValid()) return {}; - const QColor accent = QColor::fromRgb(abgr.blue(), abgr.green(), abgr.red(), abgr.alpha()); + const QColor accentLight = accent.lighter(120); const QColor accentLighter = accentLight.lighter(120); const QColor accentLightest = accentLighter.lighter(120); @@ -145,12 +161,6 @@ static constexpr QColor getSysColor(winrt::Windows::UI::Color &&color) } } -static inline QColor getSysColor(int index) -{ - COLORREF cr = GetSysColor(index); - return QColor(GetRValue(cr), GetGValue(cr), GetBValue(cr)); -} - // QTBUG-48823/Windows 10: SHGetFileInfo() (as called by item views on file system // models has been observed to trigger a WM_PAINT on the mainwindow. Suppress the // behavior by running it in a thread. diff --git a/src/plugins/platforms/windows/qwindowstheme.h b/src/plugins/platforms/windows/qwindowstheme.h index 63554d12887..c07f1a11695 100644 --- a/src/plugins/platforms/windows/qwindowstheme.h +++ b/src/plugins/platforms/windows/qwindowstheme.h @@ -64,7 +64,7 @@ public: static const char *name; static QPalette systemPalette(Qt::ColorScheme); - + static bool queryHighContrast(); private: void clearPalettes(); void refreshPalettes(); @@ -76,7 +76,6 @@ private: static Qt::ColorScheme queryColorScheme(); static Qt::ColorScheme effectiveColorScheme(); - static bool queryHighContrast(); static QWindowsTheme *m_instance; static inline Qt::ColorScheme s_colorScheme = Qt::ColorScheme::Unknown; |
