如何以编程方式更改Windows 7 Aero / Window边框颜色?
我正在考虑编写一个程序,该程序将根据电池电量更改Windows 7的航空颜色。我是C#的新手,我想知道如何以编程方式更改Windows 7 Aero
I am thinking of making a program that would change the Windows 7 aero color according to the battery level. I am fairly new to c# and I would like to know how to change the Windows 7 Aero programmatically
我有此代码
[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;
}
尽管,我不知道如何使用它并设置自定义颜色。
Although, I do not know how to use it and set a custom color.
没有记录的API。这完全是设计使然:此设置旨在由用户更改,而不是由应用程序更改。还有一个供用户使用的内置小程序:Personalize控制面板。
There is no documented API for this. That's entirely by design: this setting is intended to be changed by the user, not by applications. And there's a built-in applet for the user to use to do this: the Personalize control panel.
但是就像您所暗示的代码一样,您可以使用的未公开文档 API – DwmSetColorizationParameters
。您只需要仔细测试您的代码是否可以在所有目标操作系统上运行,并注意它可能会因Windows的任何新版本和/或Windows当前版本的任何更新而中断。
But like the code that you've got hints at, there is an undocumented API that you can use—DwmSetColorizationParameters
. You just need to carefully test that your code works on all targeted operating systems and be aware that it is subject to break with any new versions of Windows and/or any updates to the current version of Windows.
我知道它以前可以在Windows 7中运行,但是我还没有对所有最新的Service Pack和其他更新进行过测试,也不知道它是否可以在Windows 8中运行。全部由您来测试。使用未记录的API的工作量很大。
I know that it used to work in Windows 7, but I haven't tested it with all of the latest service packs and other updates, nor do I have any idea if it works in Windows 8. That's all up to you to test. Using undocumented APIs is a lot of work.
不过,您很幸运。 其他人已经为您完成了逆向工程。 (也可能还有其他人,例如编写您在问题中显示的代码的人。给他们功劳很好。也许是这个家伙?)
You're lucky, though. Someone else has already done the reverse engineering for you. (And probably other people, too, like the person who wrote the code you show in your question. It would be nice to give them credit. Maybe it was 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", EntryPoint = "#127", PreserveSig = false)]
private static extern void DwmGetColorizationParameters(out DWM_COLORIZATION_PARAMS parameters);
[DllImport("dwmapi.dll", EntryPoint = "#131", 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
结构为您提供了 lot 更多信息。如果愿意,您可以添加属性以获取/设置这些附加参数中的每一个。尽管需要做一些实验才能弄清楚它们的确切作用。
Once you've got that class added to your project, you interact with it through the ColorizationColor
property. Like the comments say, the DWM_COLORIZATION_PARAMS
structure gives you a lot more information. You could add properties to get/set each of these additional parameters, if you like. Although it'll take some experimentation to figure out exactly what they do.
请注意,您还需要检查主机操作系统是否支持并启用了DWM组合之前运行任何这些功能。 (否则, PreserveSig
属性将确保引发异常。)这是很明显的,但仍然值得一提。为此,您还将需要此功能:
Note that you also need to check that DWM composition is supported and enabled by the host operating system before running any of these functions. (Otherwise, the PreserveSig
attribute will ensure that an exception is thrown.) This is fairly obvious, but worth mentioning anyway. To do that, you will also need this function:
[DllImport("dwmapi.dll")]
private static extern int DwmIsCompositionEnabled(ref bool pfEnabled);