如何在Win32应用程序中检测Windows 10亮/暗模式?

时间:2018-07-14 02:16:30

标签: windows winapi windows-10 win32gui

一些上下文:Sciter(纯Win32应用程序)已经能够呈现类似UWP的UI:

在黑暗模式下: in dark mode

在灯光模式下: in light mode

Windows 10.1803在“设置”小程序as seen here for example中引入了“暗/亮”开关。

问题:如何确定Win32应用程序中该“应用程序模式”的当前类型?

3 个答案:

答案 0 :(得分:13)

好吧,此选项似乎没有直接暴露给常规的win32应用程序,但是可以通过HKCUHKLM\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize\AppsUseLightTheme注册表项进行设置/检索。

答案 1 :(得分:1)

编辑:请注意,只要您在启用c ++ 17的情况下进行构建,此方法就适用于所有Win32项目。

如果您使用的是最新的SDK,这对我有用。

#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);
}

答案 2 :(得分:1)

Microsoft.Windows.SDK.Contracts NuGet软件包使.NET Framework 4.5+和.NET Core 3.0+应用程序可以访问Windows 10 WinRT API,包括the answer by jarjar中提到的Windows.UI.ViewManagement.Settings。将此程序包添加到包含以下代码的.NET Core 3.0控制台应用程序中:

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}");
        }
    }
}

将主题设置为 Dark 时的输出为:

  

前景#FF FFFFFF 背景#FF 000000

当主题设置为 Light 时,它是:

  

前景#FF 000000 背景#FF FFFFFF

由于这是通过Microsoft提供的软件包公开的,该软件包指出:

  

此软件包包括所有受支持的Windows Runtime API,直至Windows 10版本1903

可以肯定的是,此API是可访问的!

注意:这不是明确检查主题是 Light 还是 Dark ,而是检查一对建议使用的主题是两个主题之一,因此,..该方法的正确性值得怀疑,但这至少是“纯” C#方式,可以用C ++达到其他目的。

相关问题