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

4

6

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.

Kayo answered 9/6, 2020 at 12:15 Comment(1)
.NET 9 (preview) has introduced dark mode for the entire application including the title bar. Use Application.SetDefaultDarkMode(DarkMode.Enabled) in Program.cs. You can use DarkMode.Inherit to inherit the current theme from Windows settings. It can be light or dark.Solidarity
A
7

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

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

enter image description here

Ageratum answered 9/6, 2020 at 12:36 Comment(1)
Brilliant! Took me a while to find out how to access the value via code, but it worked perfectly! Thank you very much!Kayo
C
8

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

Counterstroke answered 9/5, 2022 at 13:33 Comment(1)
If you're using Dark Mode for System, but not apps, then this will always return 1 (Light). So I used the key SystemUsesLightTheme instead of AppsUseLightTheme.Islander
A
7

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

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

enter image description here

Ageratum answered 9/6, 2020 at 12:36 Comment(1)
Brilliant! Took me a while to find out how to access the value via code, but it worked perfectly! Thank you very much!Kayo
R
3

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;
    }
}
Realty answered 27/1 at 16:13 Comment(0)
R
0

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);
Retention answered 19/8 at 19:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.