我找到了一个解决方案。实际上,基本要点已经被讨论过了。
步骤如下:
- 通过DwmGetColorizationParameters函数(未公开的API)获取参数。
- 将colorizationColor参数转换为颜色,忽略alpha通道。
- 准备基础灰色(R=217,G=217,B=217)。
- 使用colorizationColorBalance参数混合这两种颜色,该参数对应于“颜色强度”。
因此,我的代码如下:
public static Color? GetChromeColor()
{
bool isEnabled;
var hr1 = DwmIsCompositionEnabled(out isEnabled);
if ((hr1 != 0) || !isEnabled)
return null;
DWMCOLORIZATIONPARAMS parameters;
try
{
var hr2 = DwmGetColorizationParameters(out parameters);
if (hr2 != 0)
return null;
}
catch
{
return null;
}
var targetColor = Color.FromRgb(
(byte)(parameters.colorizationColor >> 16),
(byte)(parameters.colorizationColor >> 8),
(byte)parameters.colorizationColor);
var baseColor = Color.FromRgb(217, 217, 217);
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")]
private static extern int DwmGetColorizationParameters(out DWMCOLORIZATIONPARAMS parameters);
[StructLayout(LayoutKind.Sequential)]
private struct DWMCOLORIZATIONPARAMS
{
public uint colorizationColor;
public uint colorizationAfterglow;
public uint colorizationColorBalance;
public uint colorizationAfterglowBalance;
public uint colorizationBlurBalance;
public uint colorizationGlassReflectionIntensity;
public uint colorizationOpaqueBlend;
}
SystemParameters.WindowGlassBrush
属性吗? - Sheridan