3

I've made a pretty slick Windows 8-ish interface using WPF. It already turns out way better than I could wish for, but I was wondering the following:

Is it somehow possible to retrieve the current window colour set by the user? You know, you can set the Aero colour when you right-click the desktop... My plan is to use that colour for a couple of canvas elements on my GUI.

Thanks in advance!

Mario S
  • 11,715
  • 24
  • 39
  • 47

4 Answers4

5

The SystemColours class exists for this very purpose. You can bind directly to it like so

"{DynamicResource {x:Static SystemColors.WindowColorKey}}"
Stefan Z Camilleri
  • 4,016
  • 1
  • 32
  • 42
4

You can query the ColorizationColor registry key for this.

I've even went a step further and created a method to get the hexadecimal colour value, hope this helps you:

public void SomeMethod()
{
    int argbColor = (int)Microsoft.Win32.Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\DWM","ColorizationColor", null);
    var color = System.Drawing.Color.FromArgb(argbColor);
    string hexadecimalColor = ConverterToHex(color);
}


private static String ConverterToHex(System.Drawing.Color c)
{
    return String.Format("#{0}{1}{2}", c.R.ToString("X2"), c.G.ToString("X2"), c.B.ToString("X2"));
}
Denys Wessels
  • 16,829
  • 14
  • 80
  • 120
  • I'm using your code instead, since I need the color in the code behind. I figured I'd better put it all together to maintain readability. –  Oct 29 '12 at 10:30
  • My answer will also change the color when the Aero color in fact changes (which happens often in Windows 8 with automatic coloring enabled). – Mathias Lykkegaard Lorenzen Aug 16 '14 at 09:20
  • How can I tell if this value changed with binding to SystemColors.WindowColorKey – Mike Feb 24 '17 at 14:59
1

I managed to get the correct colour using the following code: Little sidenote: It has a small correction in it to ignore the alpha bit of the hex number, so I get the full color rather than the less saturated one.

string colorizationValue = string.Format("{0:x}", Microsoft.Win32.Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\DWM", "ColorizationColor", "00000000"));
StringBuilder bl = new StringBuilder(colorizationValue);
bl[0] = 'd';
bl[1] = '9';
colorizationValue = bl.ToString();

BrushConverter bc = new BrushConverter();
Brush brush = (Brush)bc.ConvertFrom("#" + colorizationValue);
cvs_barColor.Background = brush;
0

I created an open-source library for this here which is also available on NuGet.

install-package aerocolor-wpf.AeroColor

After installing the package, you can refer to a DynamicResource called AeroColor and AeroBrush depending on what you need.

There's some setup code that's needed too, but it isn't much. Just put something in your Loaded event handler of the window, as seen below.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        Loaded += MainWindow_Loaded;
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        AeroResourceInitializer.Initialize();
    }
}

The neat thing about this library is that it installs a hook as well, which updates those resources as the actual Aero color changes in the system too. This means you don't have to handle that either, and if you use a DynamicResource to point to the color in your XAML instead of a StaticResource, WPF will automatically update the color in your UI as well.

Looks very cool when Windows 8 changes the Aero color transitionally and your color follows.

Mathias Lykkegaard Lorenzen
  • 15,031
  • 23
  • 100
  • 187