Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

wpf

windows-10

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?

like image 471
Daniel Henry Avatar asked Jun 23 '17 05:06

Daniel Henry


People also ask

How do I turn on the dark theme in Windows 10?

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.

How do I turn off Microsoft dark mode?

If you'd like to keep Dark Mode enabled for your OS, but want to turn it off for individual Office apps, go to the app preferences (For example: Word > Preferences) and on the General tab look for the Personalize group. There you'll find Turn off Dark Mode.


2 Answers

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.

like image 178
Daniel Henry Avatar answered Sep 21 '22 22:09

Daniel Henry


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 { }
like image 28
Gauthier Avatar answered Sep 20 '22 22:09

Gauthier