You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#中查询Windows 10颜色设置及透明效果启用状态

判断Windows透明效果是否启用的C#实现

在C#中可以通过结合DWM API调用和注册表读取的方式,准确判断Windows系统的透明效果是否启用,以下是具体实现:

方法一:检查DWM桌面合成状态

透明效果依赖桌面窗口管理器(DWM)的合成功能,Windows Vista及以上系统支持该特性,通过DwmGetWindowAttribute函数可获取合成是否启用:

using System;
using System.Runtime.InteropServices;

public static class TransparencyHelper
{
    private const int DWMWA_COMPOSITION_ENABLED = 0;

    [DllImport("dwmapi.dll", PreserveSig = true)]
    private static extern int DwmGetWindowAttribute(IntPtr hwnd, int dwAttribute, out bool pvAttribute, int cbAttribute);

    public static bool IsDwmCompositionActive()
    {
        // Windows Vista及以上才支持DWM
        if (Environment.OSVersion.Version.Major < 6)
            return false;

        try
        {
            _ = DwmGetWindowAttribute(IntPtr.Zero, DWMWA_COMPOSITION_ENABLED, out bool isEnabled, Marshal.SizeOf<bool>());
            return isEnabled;
        }
        catch
        {
            // 调用失败时默认返回未启用
            return false;
        }
    }
}

方法二:读取系统设置的透明效果开关

Windows 10及以上版本允许用户单独关闭透明效果(不关闭DWM合成),此时需要读取注册表项确认用户设置:

using Microsoft.Win32;

public static class TransparencyHelper
{
    // 接上方法一的代码...

    public static bool IsTransparencyEnabledInSystemSettings()
    {
        try
        {
            using var personalizeKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize");
            if (personalizeKey == null)
                return false;

            var enableValue = personalizeKey.GetValue("EnableTransparency");
            return enableValue is int value && value == 1;
        }
        catch
        {
            return false;
        }
    }
}

综合判断逻辑

将两种方法结合,覆盖不同Windows版本的情况:

public static class TransparencyHelper
{
    // 接上述代码...

    public static bool IsWindowsTransparencyEnabled()
    {
        // Windows 10及以上需要同时满足DWM合成启用和用户开启透明设置
        if (Environment.OSVersion.Version.Major >= 10)
            return IsDwmCompositionActive() && IsTransparencyEnabledInSystemSettings();
        
        // Windows Vista/7仅需检查DWM合成状态
        return IsDwmCompositionActive();
    }
}

说明

  • 异常处理确保在系统版本不兼容、权限不足等情况下程序不会崩溃;
  • Windows Vista/7中,透明效果与DWM合成绑定,关闭合成即关闭透明效果;
  • Windows 10/11中,用户可通过「设置-个性化-颜色」单独关闭透明效果,因此需要额外检查注册表项。

内容的提问来源于stack exchange,提问作者Shabba

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 04:41:10