0

Is there a programming way to access the system theme (i.e., theme for Windows)?

The similar question #UWP get system theme (Light/Dark) is answered here:

var DefaultTheme = new Windows.UI.ViewManagement.UISettings();
var uiTheme = DefaultTheme.GetColorValue(Windows.UI.ViewManagement.UIColorType.Background).ToString();

But as tipa comments, the accepted answer suggests a way to access the theme for applications, not the theme for Windows.

Therefore, I wonder if there are other ways to access the system theme.

Frank
  • 136
  • 1
  • 10

4 Answers4

2

Try this:

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

If the system uses dark mode, it will return true.

That's not the theme for applications.

GeorgeLin
  • 61
  • 5
1

Here is a method I have used previously in WPF applications to determine if Windows is in High Contrast or Dark theme.

It hasn't been updated for a while so it maybe out of date, but might be a starting point? You can easily adapt it to return an enum or bool for just light/dark if required.

private static string GetWindowsTheme()
{
    string RegistryKeyPath = @"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize";
    string RegistryValueName = "AppsUseLightTheme";

    if (SystemParameters.HighContrast)
        return "High Contrast";

    using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RegistryKeyPath))
    {
        object registryValueObject = key?.GetValue(RegistryValueName);
        if (registryValueObject == null)
            return "Default";

        int registryValue = (int)registryValueObject;
        return registryValue > 0 ? "Default" : "Dark Theme";
    }
}
Ryan Thomas
  • 1,724
  • 2
  • 14
  • 27
  • Yes, I've tried it before, and it worked. However, I'm using Windows App SDK, and I wonder if there is a more "modern" way to do that. – Frank Jan 18 '23 at 13:58
1

It can be done easily without any requirement to Windows libraries and SDK via running reg query command in cmd as a sub-process:

public static bool IsDarkTheme()
{
    try
    {
        Process process = new();
        process.StartInfo.FileName = "cmd.exe";
        process.StartInfo.Arguments =
            @"/C reg query HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize\";
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.CreateNoWindow = true;
        process.Start();

        string output = process.StandardOutput.ReadToEnd();
        string[] keys = output.Split("\r\n", StringSplitOptions.RemoveEmptyEntries);
        for (int i = 0; i < keys.Length; i++)
        {
            if (keys[i].Contains("AppsUseLightTheme"))
            {
                return keys[i].EndsWith("0");
            }
        }
    }
    catch (Exception ex)
    {
        Debug.LogException(ex);
    }

    return false;
}
Enz
  • 185
  • 9
0

In WinUI 3, the enum ApplicationTheme is defined in the Microsoft.UI.Xaml namespace.

public enum ApplicationTheme
{
    //
    // Summary:
    //     Use the **Light** default theme.
    Light,
    //
    // Summary:
    //     Use the **Dark** default theme.
    Dark
}

You can get the theme like this.

public App()
{
    this.InitializeComponent();
    ApplicationTheme theme = RequestedTheme;
}
Andrew KeepCoding
  • 7,040
  • 2
  • 14
  • 21
  • I believe it returns `theme for app`, not `theme for Windows` – Frank Jan 18 '23 at 14:03
  • The default value (if you don't change it) is the Windows theme. – Andrew KeepCoding Jan 18 '23 at 14:11
  • Yes, that's true. However, let me quote @tipa's comment: "that's not what I meant. In the Windows settings you have 3 options for the "Theme": Dark, Light and Custom. When you select Custom, you can specify a different Theme (Light or Dark) for both Windows and Apps. Now, with the code above the "app theme" is returned, but not the "Windows theme" (which is what I would call "(Windows) system theme" as asked by the creator of this question)" – Frank Jan 18 '23 at 14:22
  • 1
    Oh, sorry. Now I get what you mean. Then just let me drop [this](https://github.com/microsoft/WindowsAppSDK/issues/41). Just have in mind that ``ShouldSystemUseDarkMode()`` might change in later versions. – Andrew KeepCoding Jan 18 '23 at 15:00