How to detect Windows 10 light/dark mode in Win32 application?

此生再无相见时 提交于 2019-12-03 04:19:40

问题


A bit of context: Sciter (pure win32 application) is already capable to render UWP alike UIs:

in dark mode:

in light mode:

Windows 10.1803 introduces Dark/Light switch in Settings applet as seen here for example.

Question: how do I determine current type of that "app mode" in Win32 application?


回答1:


Well, it looks like this option is not exposed to regular Win32 applications directly, however it can be set / retrieved through the AppsUseLightTheme key at the HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize registry path.




回答2:


EDIT: Calling out that this works in all Win32 projects as long as you're building with c++17 enabled.

If you're using the latest SDK, this worked for me.

#include <winrt/Windows.UI.ViewManagement.h>

using namespace winrt::Windows::UI::ViewManagement;

if (RUNNING_ON_WINDOWS_10) {
  UISettings settings;
  auto background = settings.GetColorValue(UIColorType::Background);
  auto foreground = settings.GetColorValue(UIColorType::Foreground);
}



回答3:


The Microsoft.Windows.SDK.Contracts NuGet package gives .NET Framework 4.5+ and .NET Core 3.0+ applications access to Windows 10 WinRT APIs, including Windows.UI.ViewManagement.Settings mentioned in the answer by jarjar. With this package added to a .NET Core 3.0 console app that consists of this code:

using System;
using Windows.UI.ViewManagement;

namespace WhatColourAmI
{
    class Program
    {
        static void Main(string[] args)
        {

            var settings = new UISettings();
            var foreground = settings.GetColorValue(UIColorType.Foreground);
            var background = settings.GetColorValue(UIColorType.Background);

            Console.WriteLine($"Foreground {foreground} Background {background}");
        }
    }
}

The output when the theme is set to Dark is:

Foreground #FFFFFFFF Background #FF000000

When the theme is set to Light it's:

Foreground #FF000000 Background #FFFFFFFF

As this is exposed via a Microsoft provided package that states:

This package includes all the supported Windows Runtime APIs up to Windows 10 version 1903

It's a pretty safe bet that it's intentional that this API is accessible!

Note: This isn't explicitly checking whether the theme is Light or Dark but checking for a pair of values that suggest that the theme in use is one of the two, so,.. the correctness of this method is mildly questionable but it's at least a "pure" C# way of achieving what's been outlined elsewhere with C++



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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!