How to detect Windows dark mode

Heiko 1,216 Reputation points
2022-01-29T15:53:59.047+00:00

In the Windows settings, dark mode is enabled for Windows and light mode is enabled for applications.

The following keys are missing from the registry:

HKCU\Microsoft\Windows\CurrentVersion\Themes\Personalize.
HKLM\Microsoft\Windows\CurrentVersion\Themes\Personalize

How can I detect Windows dark mode in a WPF application?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,703 questions
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,491 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,554 questions
{count} votes

Accepted answer
  1. Castorix31 82,661 Reputation points
    2022-01-29T16:42:54.387+00:00

    It depends on Windows versions
    On recent OS (>= Windows 10 1903) :

        [DllImport("UXTheme.dll", SetLastError = true, EntryPoint = "#138")] 
        public static extern bool ShouldSystemUseDarkMode();
    

    test :

        bool bRet = ShouldSystemUseDarkMode();
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Ken Tucker 5,846 Reputation points
    2022-01-29T17:07:21.077+00:00

    If you want to have your application respond to light and dark mode you can start by adding the NuGet package Microsoft.Windows.SDK.Contracts

    Lets create a simple View Model which returns a title and the brushes to display the data in the right color.

    First lets get the Colors to use for the brushes

        UISettings settings = new UISettings();
        var foreground = settings.GetColorValue(UIColorType.Foreground);
        var background = settings.GetColorValue(UIColorType.Background);
    

    Now we can create the brushes to use

        Color myforeColor = Color.FromArgb(foreground.A, foreground.R, foreground.G, foreground.B);
        ForeBrush = new SolidColorBrush(myforeColor);
    
        Color mybackColor = Color.FromArgb(background.A, background.R, background.G, background.B);
        BackBrush=new SolidColorBrush(mybackColor);
    

    Here is the whole View Model

    public class MainViewModel:INotifyPropertyChanged
    {
    public event PropertyChangedEventHandler PropertyChanged;

     // This method is called by the Set accessor of each property.
     // The CallerMemberName attribute that is applied to the optional propertyName
     // parameter causes the property name of the caller to be substituted as an argument.
     private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
     {
          if (PropertyChanged != null)
          {
              PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
          }
      }
    
    
      public MainViewModel()
      {
          UISettings settings = new UISettings();
          var foreground = settings.GetColorValue(UIColorType.Foreground);
          var background = settings.GetColorValue(UIColorType.Background);
          Color myforeColor = Color.FromArgb(foreground.A, foreground.R, foreground.G, foreground.B);
          ForeBrush = new SolidColorBrush(myforeColor);
    
          Color mybackColor = Color.FromArgb(background.A, background.R, background.G, background.B);
          BackBrush=new SolidColorBrush(mybackColor);
          Title = "Welcome to WPF";
      }
    
      private SolidColorBrush foreBrush = new SolidColorBrush(Colors.Black);
      public SolidColorBrush ForeBrush
      {
          get
          {
               return foreBrush;
          }
          set
          {
              foreBrush = value;
              NotifyPropertyChanged();
          }
      }
    
    
       private SolidColorBrush backBrush = new SolidColorBrush(Colors.White);
       public SolidColorBrush BackBrush
       {
          get
          {
                return backBrush;
           }
           set
           {
               backBrush = value;
               NotifyPropertyChanged();
           }
        }
    
       private string title;
    
       public string Title
       {
           get { return title; }
           set
           {
                title = value;
                NotifyPropertyChanged();
            }
        }
    
    }
    

    An alternate method to do this is to look in the registry

    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize\AppsUseLightTheme. In the value of the key is 1 Windows 10 is in Light mode.

    Depending on the value in the registry you would have to set the brush colors.

    Here is a link to an example
    https://github.com/vb2ae/WPFLightDarkMode