How to Get The System Accent Color for Uwp-Apps

Change Accent Color in Windows 10 UWP

On Win10 UWP, System Accent color is defined as ThemeResource SystemControlHighlightAccentBrush.
You can override it as following.

<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Default">
<SolidColorBrush x:Key="SystemControlHighlightAccentBrush" Color="Orange" />
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
<SolidColorBrush x:Key="SystemControlHighlightAccentBrush" Color="Green" />
</ResourceDictionary>
<ResourceDictionary x:Key="Light">
<SolidColorBrush x:Key="SystemControlHighlightAccentBrush" Color="Blue" />
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>

How to get the system accent color for UWP-Apps?

you can use WinRT API to do that : Windows.UI.ViewManagement.UIColorType enumeration provides all accents colors. You can make something like :

var uiSettings = new Windows.UI.ViewManagement.UISettings();
var rgba = uiSettings.getColorValue(Windows.UI.ViewManagement.UIColorType.accent);
var cssColorString = "rgba(" + rgba.r + "," + rgba.g + "," + rgba.b + ", " + rgba.a + ")";

How to override user accent color in UWP app

Found the problem.

In my app.xaml I had this for WinUI controls:

 <Application>
<Application.Resources>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
</Application.Resources>
</Application>

In each page I had a color theme as a resource dictionary.

   <Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ThemeDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Page.Resources>

For some reason that does not work correctly.

When I put both in the app.xaml and removed page resources, the weird problems with accent colors disappeared.

   <Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
<ResourceDictionary Source="ThemeDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>

I am now having problems with ContentDialog, but that's a different SO post. Something is not right with this resource merging it seems...

C# console get Windows 10 Accent Color

HKEY_CURRENT_USER\Software\Microsoft\Windows\DWM\ - 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.

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

Here info about package:
https://learn.microsoft.com/en-us/dotnet/core/porting/windows-compat-pack/


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 = @"Software\Microsoft\Windows\DWM";
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 );
}

How to get Windows 10 accent color?

You will get only Hex Color in this code:

Application.Current.Resources["SystemAccentColor"]

You have to convert it into usable color format, here is the solution.

var color = Application.Current.Resources["SystemAccentColor"];
btnTest.Background = GetColorFromHex(color.ToString());

And here is the converting function:

public static SolidColorBrush GetColorFromHex(string hexaColor)
{
return new SolidColorBrush(
Color.FromArgb(
Convert.ToByte(hexaColor.Substring(1, 2), 16),
Convert.ToByte(hexaColor.Substring(3, 2), 16),
Convert.ToByte(hexaColor.Substring(5, 2), 16),
Convert.ToByte(hexaColor.Substring(7, 2), 16)
));
}

How to update a WinJS UWP app after the Windows system accent color changes

You could detect the accent color changed in the colorvalueschangedevent handler of UISettings instance. When you change the system' theme accent color and it will be fired.

var uiSettings = new Windows.UI.ViewManagement.UISettings();
uiSettings.addEventListener("colorvalueschanged", onColorChanged);

function onColorChanged() {

var accentColor = uiSettings.getColorValue(Windows.UI.ViewManagement.UIColorType.accent);

}

How do I change the accent colour of a Xamarin.Forms UWP application?

Here is the style code of FormsTextBox for UWP.

You need to override below styled colors:

<Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}" />
<Setter Property="Background" Value="{ThemeResource SystemControlBackgroundAltHighBrush}" />
<Setter Property="BackgroundFocusBrush" Value="{ThemeResource SystemControlBackgroundChromeWhiteBrush}" />
<Setter Property="BorderBrush" Value="{ThemeResource SystemControlForegroundChromeDisabledLowBrush}" />

So to change the colour of your textbox boarder brush you can add these ThemeResources to your App.xaml like so:

<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<SolidColorBrush x:Key="SystemControlHighlightAccentBrush" Color="#ff0000" />
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
</Application.Resources>


Related Topics



Leave a reply



Submit