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

2

本文介绍了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 强调色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14