C# 控制台获取 Windows 10 强调色 [英] C# console get Windows 10 Accent Color

查看:24
本文介绍了C# 控制台获取 Windows 10 强调色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.Net Core 2.X 中有没有办法在控制台应用程序中读取选定的 Windows 10 强调色.

Is there a way in .Net Core 2.X to read the Selected Windows 10 Accent Color in a Console Application.

我发现的大多数解决方案都是 UWP 或 WPF 应用程序.

Most of the solution I found are UWP or WPF apps.

也告诉你我的意思是哪种颜色,这是它的图片:

Too show you which color I mean, here is a picture of it:

推荐答案

HKEY_CURRENT_USERSoftwareMicrosoftWindowsDWM - 存储所有装饰颜色.因此,如果应用程序以 HKEY_CURRENT_USER 权限启动,您可以读取或更改AccentColor"属性(和目录中的其他人)或自行更改十六进制表示法的颜色代码.

HKEY_CURRENT_USERSoftwareMicrosoftWindowsDWM - Stores all decoration colors. So if app launched with rights to HKEY_CURRENT_USER you can read or change "AccentColor" property (and others in directory) or change the color code in hexadecimal notation on your own.

要访问 Windows 注册表,您需要安装软件包:https://www.nuget.org/packages/Microsoft.Windows.Compatibility/

To get access to windows registry you need to install package: https://www.nuget.org/packages/Microsoft.Windows.Compatibility/

这里关于包裹的信息:https://docs.microsoft.com/en-us/dotnet/core/porting/windows-compat-pack/

颜色值以 ABGR 顺序存储为注册表 DWORD(32 位整数)值(相对于 ARGBRGBA 顺序).

The color values are stored as Registry DWORD (32-bit integer) values in ABGR order (as opposed to ARGB or RGBA order).

using Microsoft.Win32;

public static ( Byte r, Byte g, Byte b, Byte a ) GetAccentColor()
{
    const String DWM_KEY = @"SoftwareMicrosoftWindowsDWM";
    using( RegistryKey dwmKey = Registry.CurrentUser.OpenSubKey( DWM_KEY, RegistryKeyPermissionCheck.ReadSubTree ) )
    {
        const String KEY_EX_MSG = "The "HKCU\" + DWM_KEY + "" registry key does not exist.";
        if( dwmKey is null ) throw new InvalidOperationException( KEY_EX_MSG );

        Object accentColorObj = dwmKey.GetValue( "AccentColor" );
        if( accentColorObj is Int32 accentColorDword )
        {
            return ParseDWordColor( accentColorDword );
        }
        else
        {
            const String VALUE_EX_MSG = "The "HKCU\" + DWM_KEY + "\AccentColor" registry key value could not be parsed as an ABGR color.";
            throw new InvalidOperationException( VALUE_EX_MSG );
        }
    }

}

private static ( Byte r, Byte g, Byte b, Byte a ) ParseDWordColor( Int32 color )
{
    Byte
        a = ( color >> 24 ) & 0xFF,
        b = ( color >> 16 ) & 0xFF,
        g = ( color >>  8 ) & 0xFF,
        r = ( color >>  0 ) & 0xFF;

    return ( r, g, b, a );
}

这篇关于C# 控制台获取 Windows 10 强调色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆