CODE INSIDE

Get the Windows 10 or 8 accent color in WPF

x.

Windows Accent Color

Since Windows 8 users can choose a system accent color. The color can be seen on the window borders of the default apps and it can be pretty easy be used inside a UWP App.

How to get the accent color in WPF?

Option 1: SystemParameters.WindowGlassBrush - not 100% the same color

As far as I know there are several ways to get the color code, one easy but not 100% correct way is to use the SystemParameters.WindowGlassBrush property that was introduced in .NET 4.5.

Sadly, the color is not 100% correct - I have no idea where this “similar”, but not identical color is used and why the API is returning this color.

It seems this is just a wrapper around the undocumented DwmGetColorizationParameters Win32 API.

Option 2: GetImmersiveColorFromColorSetEx

I found this solution here, which is just a wrapper around the GetImmersiveColorFromColorSetEx Win32 API.

Option 3: Registry, DwmGetColorizationParameters

The last option would be to read the Registry values - I found some hints on this site, but I wouldn’t recommend it, because it is more or less undocumented and might break in the future. So we will use option 1 or 2.

Usage:

The usage of both options is pretty easy (at least with the option 2 code provided) :

    // https://gist.github.com/paulcbetts/3c6aedc9f0cd39a77c37
    var accentColor = new SolidColorBrush(AccentColorSet.ActiveSet["SystemAccent"]);
    this.Code.Background = accentColor;
    this.Code.Text = "AccentColorSet Immersive 'SystemAccent' " + accentColor.Color.ToString();

    // Available in .NET 4.5
    this.SystemProperties.Background = SystemParameters.WindowGlassBrush;
    this.SystemProperties.Text = "SystemParameters.WindowGlassBrush " + ((SolidColorBrush)SystemParameters.WindowGlassBrush).Color.ToString();

Result:

x.

As you can see, the lower color does match the border color instead of the first option. Crazy, right? ¯\_(ツ)_/¯

From the comments: As Yves Goergen pointed out, the resulting color does not exactly match the border color, but it does match the system accent color, which is ok. I would guess that the border has some chrome behavior attached so that the color is slightly different.

The full code is on GitHub

Hope this helps.

blog comments powered by Disqus