From e603a2d8d966192e276bf5aeac637424ff3a8442 Mon Sep 17 00:00:00 2001 From: Leonard Hecker Date: Thu, 10 Nov 2022 14:39:22 +0100 Subject: [PATCH] wip --- src/internal/stubs.cpp | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/internal/stubs.cpp b/src/internal/stubs.cpp index f92f4096969..3ab48a39e3a 100644 --- a/src/internal/stubs.cpp +++ b/src/internal/stubs.cpp @@ -5,6 +5,8 @@ #include "../inc/conint.h" +#include + using namespace Microsoft::Console::Internal; [[nodiscard]] HRESULT ProcessPolicy::CheckAppModelPolicy(const HANDLE /*hToken*/, @@ -21,7 +23,28 @@ using namespace Microsoft::Console::Internal; return S_OK; } -[[nodiscard]] HRESULT Theming::TrySetDarkMode(HWND /*hwnd*/) noexcept +[[nodiscard]] HRESULT Theming::TrySetDarkMode(HWND hwnd) noexcept { + static const auto ShouldAppsUseDarkMode = []() { + static const auto uxtheme = LoadLibraryExW(L"uxtheme.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); + return uxtheme ? reinterpret_cast(GetProcAddress(uxtheme, MAKEINTRESOURCEA(132))) : nullptr; + }(); + static const auto IsHighContrastOn = []() { + bool highContrast = false; + HIGHCONTRAST hc{ sizeof(hc) }; + if (SystemParametersInfoW(SPI_GETHIGHCONTRAST, sizeof(hc), &hc, 0)) + { + highContrast = (HCF_HIGHCONTRASTON & hc.dwFlags) != 0; + } + return highContrast; + }; + + if (ShouldAppsUseDarkMode) + { + const BOOL useDarkMode = ShouldAppsUseDarkMode() && !IsHighContrastOn(); + SetWindowTheme(hwnd, useDarkMode ? L"DarkMode_Explorer" : L"", nullptr); + DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE, &useDarkMode, sizeof(useDarkMode)); + } + return S_FALSE; }