简体   繁体   中英

How can I get whether Windows 10 Anniversary Update or later is using its light or dark theme in a WPF app?

I have written a WPF desktop app that I want to put in the Windows Store using the Desktop Bridge. The app is capable of presenting itself in light and dark modes, and using an accent color. But, to be a good citizen of Windows 10, I want to get that information from the OS, if possible.

It is my current understanding that I can get the accent color from here:

var accentBrush = SystemParameters.WindowGlassBrush;

How can I get whether Windows 10 is in its light or dark theme? Also, what method would you recommend to be notified of a change in the user's preference for either light/dark or the accent color?

There is a registry value that is updated whenever this light/dark mode setting in Windows 10 Anniversary Update or later changes. It's key is:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize

The name of the value is:

AppsUseLightTheme

If it is 1, then the theme is Light. If it is 0, then the theme is Dark. I'm going to assume Light if I can't find the key or the value (as would be the case in previous versions of Windows).

As far as I'm concerned, lindexi deserves credit for the answer. Without that comment, it didn't occur to me to investigate.

Some simple code to give you a bool for light mode in normal WPF apps, not UWP. Never tested with high contrast modes. If the value does not exist or something goes wrong, it assumes light mode.

bool is_light_mode = true;
try
{
    var v = Microsoft.Win32.Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize", "AppsUseLightTheme", "1");
    if (v != null && v.ToString() == "0")
        is_light_mode = false;
}
catch { }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM