Gabic
Gabic

Reputation: 704

Is it possible to detect Windows dark mode on winforms application?

I am developing a winforms application using all those flat style options, and it makes the application look a lot like Win10 applications, so I was wondering if is it possible to detect if the OS is using dark mode, so I could adjust the colors to fit the dark (or light) mode.

I found some questions about it, but they were related to UWP and WPF, so the solutions didn't work on my apllication.

Upvotes: 6

Views: 3005

Answers (4)

dynamichael
dynamichael

Reputation: 852

What I do...

    public enum ThemeModes { Default, Dark, Light }

    public static ThemeModes GetSystemThemeMode() => _GetThemeMode("SystemUsesLightTheme");
    public static ThemeModes GetApplicationThemeMode() => _GetThemeMode("AppsUseLightTheme");

    private static ThemeModes _GetThemeMode(string key) => (ThemeModes)((int)Registry.GetValue(@"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize", key, -1) + 1);

Upvotes: 1

Rena821
Rena821

Reputation: 351

Based on Waescher's solution, here is the code for that:

using Microsoft.Win32;

try
{
    int res = (int)Registry.GetValue("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", "AppsUseLightTheme", -1);
}
catch 
{
 //Exception Handling     
}
    

res contains the value for the default theme on windows

0 : dark theme

1 : light theme

-1 : AppsUseLightTheme could not be found

Upvotes: 8

BlueMystic
BlueMystic

Reputation: 2297

Based on the other answers, here is my approach:

/// <summary>Returns Windows Color Mode for Applications.
/// <para>0=dark theme, 1=light theme</para>
/// </summary>
public static int GetWindowsColorMode(bool GetSystemColorModeInstead = false)
{
    try
    {
        return (int)Microsoft.Win32.Registry.GetValue(
            @"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize",
            GetSystemColorModeInstead ? "SystemUsesLightTheme" : "AppsUseLightTheme", 
            -1);
    }
    catch
    {
        return 1;
    }
}

Upvotes: 3

Waescher
Waescher

Reputation: 5737

You can read the current user preferences from the Windows Registry:

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

enter image description here

Upvotes: 7

Related Questions