代码之家  ›  专栏  ›  技术社区  ›  c-smile

如何在win32应用程序中检测windows 10的亮/暗模式?

  •  16
  • c-smile  · 技术社区  · 7 年前

    一点上下文: Sciter (纯win32应用程序)已经能够呈现类似于uwp的ui:

    在黑暗模式下: in dark mode

    在灯光模式下: in light mode

    Windows 10.1803在设置小程序中引入暗/光开关 as seen here for example 是的。

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

    2 回复  |  直到 7 年前
        1
  •  18
  •   Mike Omeiri Kouider    5 年前

    嗯,看起来这个选项没有直接暴露给普通的win32应用程序,但是可以通过 AppsUseLightTheme 钥匙在 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize 注册表路径。

        2
  •  2
  •   jarjar    6 年前

    编辑:指出只要您在启用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);
    }
    
        3
  •  2
  •   Rob    6 年前

    这个 Microsoft.Windows.SDK.Contracts nuget包使.net framework 4.5+和.net core 3.0+应用程序可以访问windows10winrtapi,包括 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}");
            }
        }
    }
    

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

    前景FF FFFFFF公司 背景FF 百万

    当主题设置为 它是:

    前景FF 百万 背景FF FFFFFF公司

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

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

    这是一个相当安全的打赌,它是故意的,这个api是可访问的!

    注: 这不是显式检查主题是否 黑暗的 但是检查一对值 建议 使用的主题是其中之一,所以…这种方法的正确性是有问题的,但它至少是一种“纯”的C++实现方式。