How to change Windows 7 Aero / Window border color programatically?

时间秒杀一切 提交于 2019-12-04 12:11:36

问题


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

I have this code

[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.


回答1:


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.

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.

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?)

Here's how you use it:

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);
      }
   }
}

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.

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);



回答2:


Maybe you could contact the developer of this small application. Ask how he did it.. i'm not sure he used C# to accomplish this. But it's worth a shot.

This is my final search, I couldn't find anything else usefull so...



来源:https://stackoverflow.com/questions/17801585/how-to-change-windows-7-aero-window-border-color-programatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!