我正在考虑制作一个程序,根据电池电量改变
Windows 7的航空颜色.我是c#的新手,我想知道如何以编程方式更改Windows 7 Aero
我有这个代码
[DllImport("dwmapi.dll",EntryPoint = "#127",PreserveSig = false)] public static extern void DwmGetColorizationParameters(out WDM_COLORIZATION_PARAMS parameters); [DllImport("dwmapi.dll",EntryPoint = "#131",PreserveSig = false)] public static extern void DwmSetColorizationParameters(WDM_COLORIZATION_PARAMS parameters,uint uUnknown); public struct WDM_COLORIZATION_PARAMS { public uint Color1; public uint Color2; public uint Intensity; public uint Unknown1; public uint Unknown2; public uint Unknown3; public uint Opaque; }
虽然,我不知道如何使用它并设置自定义颜色.
解决方法
没有记录的API.这完全是设计上的:此设置旨在由用户更改,而不是由应用程序更改.并且有一个内置的小程序供用户使用:Personalize控制面板.
但是就像你已经提到的代码一样,你可以使用一个没有文档的API -DwmSetColorizationParameters.您只需要仔细测试您的代码是否适用于所有目标操作系统,并注意它可能会破坏任何新版本的Windows和/或当前版本的Windows的任何更新.
我知道它曾经在Windows 7中运行,但我没有使用所有最新的服务包和其他更新进行测试,也不知道它是否适用于Windows 8.这完全由你来测试.使用未记录的API是很多工作.
不过你很幸运. Someone else已经为您完成了逆向工程. (也可能是其他人,就像编写你在问题中显示的代码的那些人一样.给他们信用很好.也许是this guy?)
以下是您使用它的方式:
using System; using System.Drawing; using System.Globalization; using System.Runtime.InteropServices; class DwmManager { private struct DWM_COLORIZATION_PARAMS { public uint clrColor; public uint clrAfterGlow; public uint nIntensity; public uint clrAfterGlowBalance; public uint clrBlurBalance; public uint clrGlassReflectionIntensity; public bool fOpaque; } [DllImport("dwmapi.dll",PreserveSig = false)] private static extern void DwmGetColorizationParameters(out DWM_COLORIZATION_PARAMS parameters); [DllImport("dwmapi.dll",PreserveSig = false)] private static extern void DwmSetColorizationParameters(ref DWM_COLORIZATION_PARAMS parameters,bool unknown); // Helper method to convert from a Win32 BGRA-format color to a .NET color. private static Color BgraToColor(uint color) { return Color.FromArgb(Int32.Parse(color.ToString("X"),NumberStyles.HexNumber)); } // Helper method to convert from a .NET color to a Win32 BGRA-format color. private static uint ColorToBgra(Color color) { return (uint)(color.B | (color.G << 8) | (color.R << 16) | (color.A << 24)); } // Gets or sets the current color used for DWM glass,based on the user's color scheme. public static Color ColorizationColor { get { // Call the DwmGetColorizationParameters function to fill in our structure. DWM_COLORIZATION_PARAMS parameters; DwmGetColorizationParameters(out parameters); // Convert the colorization color to a .NET color and return it. return BgraToColor(parameters.clrColor); } set { // Retrieve the current colorization parameters,just like we did above. DWM_COLORIZATION_PARAMS parameters; DwmGetColorizationParameters(out parameters); // Then modify the colorization color. // Note that the other parameters are left untouched,so they will stay the same. // You can also modify these; that is left as an exercise. parameters.clrColor = ColorToBgra(value); // Call the DwmSetColorizationParameters to make the change take effect. DwmSetColorizationParameters(ref parameters,false); } } }
一旦将该类添加到项目中,就可以通过ColorizationColor属性与其进行交互.与评论一样,DWM_COLORIZATION_PARAMS结构为您提供了更多信息.如果您愿意,可以添加属性以获取/设置每个附加参数.虽然需要一些实验来弄清楚他们究竟做了什么.
请注意,在运行任何这些功能之前,还需要检查主机操作系统是否支持和启用DWM组合. (否则,PreserveSig属性将确保抛出异常.)这是相当明显的,但无论如何值得一提.为此,您还需要此功能:
[DllImport("dwmapi.dll")] private static extern int DwmIsCompositionEnabled(ref bool pfEnabled);