How to change the color of the border of Windows 7 Aero / Window programmatically?

I'm thinking of creating a program that will change the color of Windows 7 irons according to the battery level. I am new to C # and I would like to know how to programmatically change Windows 7 Aero

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 my own color.

+6
source share
2 answers

There is no documented API for this. This is completely by design: this option is intended to be modified by the user, not by applications. And for this, the user can use the built-in applet: the Personalize control panel.

But, like the code you are referring to, there is an undocumented API that you can use - DwmSetColorizationParameters . You just need to thoroughly test that your code works on all target operating systems, and keep in mind that it can be broken with any new versions of Windows and / or any updates for the current version of Windows.

I know that it worked in Windows 7, but I did not test it with all the latest service packs and other updates, and I do not know if it works in Windows 8. Test all this for you. Using undocumented APIs is a lot of work.

Lucky you. Someone else has already done the reverse engineering for you. (And probably other people, too, like the one who wrote the code that 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 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 have added this class to your project, you interact with it through the ColorizationColor property. Like comments, the DWM_COLORIZATION_PARAMS structure gives you much more information. You can add properties to get / set each of these additional parameters if you wish. Although, some experimentation will be required to determine what they are doing.

Note that you also need to verify that the DWM composition is supported and enabled by the host operating system before running any of these functions. (Otherwise, the PreserveSig attribute will throw an exception). This is pretty obvious, but worth mentioning anyway. For this you will also need this function:

 [DllImport("dwmapi.dll")] private static extern int DwmIsCompositionEnabled(ref bool pfEnabled); 
+9
source

Perhaps you could contact the developer of this small application . Ask how he did it. I'm not sure that he used C# to achieve this. But it's worth it.

This is my final search, I did not find anything useful, therefore ...

+1
source

Source: https://habr.com/ru/post/950038/


All Articles