Skip to content

Commit 7fc5313

Browse files
Refactor System-handling for SetColor.
1 parent 8914c12 commit 7fc5313

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

src/System.Windows.Forms/src/System/Windows/Forms/Application.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public sealed partial class Application
5050

5151
private const string DarkModeKeyPath = "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize";
5252
private const string DarkModeKey = "AppsUseLightTheme";
53-
private const int DarkModeNotAvailable = -1;
53+
private const int SystemDarkModeDisabled = 1;
5454

5555
/// <summary>
5656
/// Events the user can hook into
@@ -283,6 +283,12 @@ public static void SetColorMode(SystemColorMode systemColorMode)
283283
return;
284284
}
285285

286+
if (GetSystemColorModeInternal() != SystemDarkModeDisabled)
287+
{
288+
s_systemColorMode = SystemColorMode.Dark;
289+
return;
290+
}
291+
286292
s_systemColorMode = SystemColorMode.Classic;
287293
}
288294
finally
@@ -358,20 +364,16 @@ private static int GetSystemColorModeInternal()
358364

359365
int systemColorMode = DarkModeNotAvailable;
360366

361-
// Dark mode is supported when we are >= W11/22000
362-
// Technically, we could go earlier, but then the APIs we're using weren't officially public.
363-
if (OsVersion.IsWindows11_OrGreater())
367+
try
368+
{
369+
// 0 for dark mode and |1| for light mode.
370+
systemColorMode = Math.Abs((Registry.GetValue(
371+
keyName: DarkModeKeyPath,
372+
valueName: DarkModeKey,
373+
defaultValue: SystemDarkModeDisabled) as int?) ?? systemColorMode);
374+
}
375+
catch (Exception ex) when (!ex.IsCriticalException())
364376
{
365-
try
366-
{
367-
systemColorMode = (Registry.GetValue(
368-
keyName: DarkModeKeyPath,
369-
valueName: DarkModeKey,
370-
defaultValue: DarkModeNotAvailable) as int?) ?? systemColorMode;
371-
}
372-
catch (Exception ex) when (!ex.IsCriticalException())
373-
{
374-
}
375377
}
376378

377379
return systemColorMode;

0 commit comments

Comments
 (0)