Enable dark mode support for shell context menu #298

(Thanks to chip33 for API hint)
This commit is contained in:
Peter Kirmeier 2023-09-23 00:55:05 +02:00
parent 97ed24d9ed
commit bc30a77c04
3 changed files with 51 additions and 1 deletions

View file

@ -9,6 +9,7 @@ namespace SystemTrayMenu
using System.Windows;
using System.Windows.Media;
using Microsoft.Win32;
using SystemTrayMenu.DllImports;
using SystemTrayMenu.Properties;
using SystemTrayMenu.UserInterface.FolderBrowseDialog;
using SystemTrayMenu.Utilities;
@ -161,6 +162,10 @@ namespace SystemTrayMenu
isDarkMode = true;
}
// Required for native UI rendering like the ShellContextMenu
NativeMethods.SetPreferredAppMode(isDarkMode ? NativeMethods.PreferredAppMode.ForceDark : NativeMethods.PreferredAppMode.ForceLight);
NativeMethods.FlushMenuThemes();
readDarkModeDone = true;
}

45
NativeDllImport/Themes.cs Normal file
View file

@ -0,0 +1,45 @@
// <copyright file="Themes.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
//
// Copyright (c) 2023 Peter Kirmeier
namespace SystemTrayMenu.DllImports
{
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
/// <summary>
/// See: https://gist.github.com/rounk-ctrl/b04e5622e30e0d62956870d5c22b7017
/// See: https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/e36eb4c0-4370-4933-943d-b6fe22677e6c/dark-mode-apis?forum=windowssdk
/// Wraps the method calls to native Windows DLLs.
/// </summary>
public static partial class NativeMethods
{
internal enum PreferredAppMode : int
{
Default = 0,
AllowDark = 1,
ForceDark = 2,
ForceLight = 3,
}
/// <summary>
/// No official documentation.
/// Seems to set mode that the UI shall use for rendering UI elements.
/// </summary>
/// <param name="preferredAppMode">Desired mode.</param>
/// <returns>Undocumented.</returns>
[SupportedOSPlatform("windows")]
[DllImport("uxtheme.dll", EntryPoint = "#135", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
internal static extern int SetPreferredAppMode(PreferredAppMode preferredAppMode);
/// <summary>
/// No official documentation.
/// Seems to switch UI mode which was set by SetPreferredAppMode.
/// </summary>
[SupportedOSPlatform("windows")]
[DllImport("uxtheme.dll", EntryPoint = "#136", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
internal static extern void FlushMenuThemes();
}
}

View file

@ -269,7 +269,7 @@ Thanks for ideas, reporting issues and contributing!
#285 #286 [dao-net](https://github.com/dao-net),
#288 William P.,
#294 #295 #296 Stefan M.,
#225 #297 #299 #317 #321 #324 #330 #386 #390 #401 #402 #407 #409 #414 #416 #418 #428 #430 #443 [chip33](https://github.com/chip33),
#225 #297 #298 #299 #317 #321 #324 #330 #386 #390 #401 #402 #407 #409 #414 #416 #418 #428 #430 #443 [chip33](https://github.com/chip33),
#298 [phanirithvij](https://github.com/phanirithvij),
#306 [wini2](https://github.com/wini2),
#370 [dna5589](https://github.com/dna5589),