Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

aero

dwm

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.

like image 594
Trontor Avatar asked Jul 23 '13 04:07

Trontor


People also ask

How do you change window border color?

Step 1: Right-click on the desktop, click Personalize to open the Settings app. Step 2: On the left pane, click Colors. On the right side, make sure that Title bars and window borders option is turned on. turn on the option labeled Automatically pick an accent color from my background.

How do I make black borders in Windows?

Select Start > Settings . Select Personalization > Colors. In the list for Choose your mode, select Custom. In the list for Choose your default Windows mode, select Dark.


1 Answers

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);
like image 113
Cody Gray Avatar answered Sep 30 '22 18:09

Cody Gray