C# 控制台获取 Windows 10 强调色

C# console get Windows 10 Accent Color(C# 控制台获取 Windows 10 强调色)
本文介绍了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 强调色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)