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

问题描述 投票:14回答:2

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

在黑暗模式:in dark mode

在光线模式:in light mode

Windows 10.1803在Settings applet as seen here for example中引入了Dark / Light开关。

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

windows winapi windows-10 win32gui
2个回答
15
投票

好吧,看起来这个选项不会直接暴露给常规的win32应用程序,但可以通过HKCUHKLM\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize\AppsUseLightTheme注册表项设置/检索。


1
投票

编辑:调用这个适用于所有Win32项目,只要你在启用c ++ 17的情况下构建。

如果您使用的是最新的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);
}

1
投票

Microsoft.Windows.SDK.Contracts NuGet包为.NET Framework 4.5+和.NET Core 3.0+应用程序提供对Windows 10 WinRT API的访问,包括Windows.UI.ViewManagement.Settings中提到的the answer by jarjar。将此软件包添加到包含此代码的.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}");
        }
    }
}

主题设置为黑暗时的输出是:

前景#FFFFFFFF背景#FF000000

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

前景#FF000000背景#FFFFFFFF

因为这是通过Microsoft提供的包公开的,该包说明:

此软件包包括Windows 10版本1903之前的所有受支持的Windows运行时API

可以肯定的是,这个API是可以访问的!

注意:这并没有明确地检查主题是亮还是暗,而是检查一对值,这些值表明正在使用的主题是两者中的一个,所以,...这种方法的正确性有点值得怀疑,但它是在至少是一种“纯粹的”C#方式来实现C ++在其他地方所概述的内容

© www.soinside.com 2019 - 2024. All rights reserved.