如何在Windows 8.1中获取WPF窗口的标题栏颜色?

时间:2014-07-03 14:02:16

标签: c# wpf windows-8

要使用WindowChrome类自定义窗口的外观,我想在Windows 8.1中获得WPF窗口的标题栏颜色。

首先,我试过

SystemParameters.WindowGlassColor 

但是这个属性但不包括正确的alpha值(它总是255)。

其次,我尝试了这个功能:

DwmGetColorizationColor 

一开始似乎工作正常。返回值具有正确的颜色信息,包括alpha通道。但是,在“颜色和外观”对话框中通过滑块更改“颜色强度”时,返回值将远离实际值并指示奇怪的颜色。

那么,有没有人对替代方法或解决方法有所了解?

1 个答案:

答案 0 :(得分:5)

我找到了解决方案。实际上,已经讨论了基本观点。

序列将是:

  1. 通过DwmGetColorizationParameters函数(未记录的API)获取参数。
  2. 将colorizationColor参数转换为Color忽略Alpha通道。
  3. 准备基色灰色(R = 217,G = 217,B = 217)。
  4. 使用colorizationColorBalance参数混合两种颜色,该参数对应于"颜色强度"。
  5. 所以,我的代码如下:

    public static Color? GetChromeColor()
    {
        bool isEnabled;
        var hr1 = DwmIsCompositionEnabled(out isEnabled);
        if ((hr1 != 0) || !isEnabled) // 0 means S_OK.
            return null;
    
        DWMCOLORIZATIONPARAMS parameters;
        try
        {
            // This API is undocumented and so may become unusable in future versions of OSes.
            var hr2 = DwmGetColorizationParameters(out parameters);
            if (hr2 != 0) // 0 means S_OK.
                return null;
        }
        catch
        {
            return null;
        }
    
        // Convert colorization color parameter to Color ignoring alpha channel.
        var targetColor = Color.FromRgb(
            (byte)(parameters.colorizationColor >> 16),
            (byte)(parameters.colorizationColor >> 8),
            (byte)parameters.colorizationColor);
    
        // Prepare base gray color.
        var baseColor = Color.FromRgb(217, 217, 217);
    
        // Blend the two colors using colorization color balance parameter.
        return BlendColor(targetColor, baseColor, (double)(100 - parameters.colorizationColorBalance));
    }
    
    private static Color BlendColor(Color color1, Color color2, double color2Perc)
    {
        if ((color2Perc < 0) || (100 < color2Perc))
            throw new ArgumentOutOfRangeException("color2Perc");
    
        return Color.FromRgb(
            BlendColorChannel(color1.R, color2.R, color2Perc),
            BlendColorChannel(color1.G, color2.G, color2Perc),
            BlendColorChannel(color1.B, color2.B, color2Perc));
    }
    
    private static byte BlendColorChannel(double channel1, double channel2, double channel2Perc)
    {
        var buff = channel1 + (channel2 - channel1) * channel2Perc / 100D;
        return Math.Min((byte)Math.Round(buff), (byte)255);
    }
    
    [DllImport("Dwmapi.dll")]
    private static extern int DwmIsCompositionEnabled([MarshalAs(UnmanagedType.Bool)] out bool pfEnabled);
    
    [DllImport("Dwmapi.dll", EntryPoint = "#127")] // Undocumented API
    private static extern int DwmGetColorizationParameters(out DWMCOLORIZATIONPARAMS parameters);
    
    [StructLayout(LayoutKind.Sequential)]
    private struct DWMCOLORIZATIONPARAMS
    {
        public uint colorizationColor;
        public uint colorizationAfterglow;
        public uint colorizationColorBalance; // Ranging from 0 to 100
        public uint colorizationAfterglowBalance;
        public uint colorizationBlurBalance;
        public uint colorizationGlassReflectionIntensity;
        public uint colorizationOpaqueBlend;
    }