win32-darkmode
win32-darkmode copied to clipboard
[Question] How to make menu items dark
First of all: Sorry, I'm not very proficient with Win32 or C++ in general.
As the code is almost completely uncommented, how are the menu popup items in the top menu bar being set to dark? I found out that it has something to do with _SetPreferredAppMode
(or _AllowDarkModeForApp
for older versions), but when I try to implement this in my C# app using p/invoke, it does not make menus dark (and yes, I'm using MainMenu
and not MenuStrip
). Sorry if this is the wrong place to ask.
Also, is it possible for you to add detailed comments to the code so it can be understood more easily how to integrate dark mode in one's own app?
IIRC, C# is not using native menu. It uses it's own menu, so dark menu has no effects on it. Also Qt is not using native menu.
Here is a related answer: https://stackoverflow.com/a/10672152/6911112
And a screenshot from that link:
As I said, I am using the MainMenu
component and the ContextMenu
component. I got it to work in the meantime. The trick is to not asign this.Menu = mainMenu1
(remove that line in YourForm.Designer.cs
), then call SetPreferredAppMode
(using p/invoke) and only then assign the MainMenu
. Not doing that also for some reason breaks ContextMenu
s.
Now the only "bright" thing left is the menu bar itself, which can't be made dark (as discussed in #1)
Sorry I misunderstood you. So you mean the menu bar is still light? This is because Windows doesn't have dark theme resources for that. What we can do is to owner draw menu bar.
Yeah, owner drawing is probably the best strategy for having a dark menu bar, at least until Microsoft adds one natively in the future.
As far as I understand, the only semi-solution is to install an unsigned theme on your computer that will force item menu to be dark (what works in my case), which is not directly related to new Windows dark mode, it need to be improved, as jonaskohl said.
As I said, I am using the
MainMenu
component and theContextMenu
component. I got it to work in the meantime. The trick is to not asignthis.Menu = mainMenu1
(remove that line inYourForm.Designer.cs
), then callSetPreferredAppMode
(using p/invoke) and only then assign theMainMenu
. Not doing that also for some reason breaksContextMenu
s.Now the only "bright" thing left is the menu bar itself, which can't be made dark (as discussed in #1)
I try to do this to make the menu of Unity Editor be darkï¼but it doesnt work.
using System; using System.Runtime.InteropServices; using UnityEditor;
public class DarkModeMenu { enum GetAncestorFlags : uint { GA_PARENT = 1, GA_ROOT = 2, GA_ROOTOWNER = 3 }
const uint DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
enum PreferredAppMode { APPMODE_DEFAULT = 0, APPMODE_ALLOWDARK = 1, APPMODE_FORCEDARK = 2, APPMODE_FORCELIGHT = 3, APPMODE_MAX = 4 }
[DllImport("uxtheme.dll", EntryPoint = "SetPreferredAppMode", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern int SetPreferredAppMode(int preferredAppMode);
}