Get Aero Window Colour

前端 未结 4 962
离开以前
离开以前 2020-12-18 11:40

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

相关标签:
4条回答
  • 2020-12-18 12:13

    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 讨论(0)
  • 2020-12-18 12:14

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

    "{DynamicResource {x:Static SystemColors.WindowColorKey}}"
    
    0 讨论(0)
  • 2020-12-18 12:19

    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"));
    }
    
    0 讨论(0)
  • 2020-12-18 12:26

    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.

    0 讨论(0)
提交回复
热议问题