wpf / winform get windows10 system colors and theme color

Microsoft Windows10 start adding color system in the dark, for UWP is easily able to obtain the current color and theme color systems, and applications for Win32 is not so straightforward.
In wpf, you can SystemParameters.WindowGlassBrushget an approximate color, there is no way to change as the system color varies with, for obsessive-compulsive disorder, this is unbearable.
In fact, when the system colors change, the system will send all application windows to change the color of the message, as long as the correct handling of the news will be able to get to the current system of color and color theme.

WM_DWMCOLORIZATIONCOLORCHANGED = 0x0320

Of course, also possible to use a simpler method. If you are using .Net Framework, should be used directly; if you're using .Net Core version wpf and winform, need to use nuget installation Microsoft.Win32.SystemEvents.
After listening SystemEvents.UserPreferenceChangedevent, when a theme or color is changed events will be triggered. Of course, there are many cases of this event will be triggered when we need to determine what color response is not really changed.

AccentColor

Get color using DwmGetColorizationParameters api and read the registry in two ways, my advice is to get the color by reading the registry. Because DwmGetColorizationParameters this api is undocumented, Microsoft Zhibuding what time it is deleted (though unlikely).
About DwmGetColorizationParameters use of your own will be, we use here to read the registry way. Note If .Net Core, need to use nuget installation Microsoft.Win32.Registry. Registry path:

计算机\HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\DWM

Wherein the read ColorizationColorfield format 0xAARRGGBB, a simple conversion example is given below:

private Color ConvertToColor(int value)
{
    return Color.FromArgb(
        (byte)(value >> 24),
        (byte)(value >> 16),
        (byte)(value >> 8),
        (byte)value
    );
}

Under this path there are many other fields, such as whether to open and transparent, mixed colors and the like, for Win10 has no effect, and we do not let it go.

ActualTheme

Similarly, the dark light system can also be obtained from the registry location to another:

计算机\HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize

And the color difference is that this does not seem to get api, at least I did not find 23333
What we need is to control the application of the theme AppsUseLightTheme, the theme color of the control system SystemUsesLightThemeand the control system is open and transparent EnableTransparencythese three fields.
In previous versions of Win10 1903, the theme color settings of the system just apply color control, so there is no SystemUsesLightThemethis field. When in use must pay attention to determine whether the field exists.
Using the above color systems and color theme, it should be enough for us to fit the application system color.
My blog: AMD Blue Fire

Guess you like

Origin www.cnblogs.com/blue-fire/p/11874519.html