win32-darkmode icon indicating copy to clipboard operation
win32-darkmode copied to clipboard

[Question] How to make menu items dark

Open jonaskohl opened this issue 4 years ago • 6 comments

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?

jonaskohl avatar Aug 05 '20 12:08 jonaskohl

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: image

ysc3839 avatar Aug 05 '20 17:08 ysc3839

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 ContextMenus.

Now the only "bright" thing left is the menu bar itself, which can't be made dark (as discussed in #1)

jonaskohl avatar Aug 05 '20 20:08 jonaskohl

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.

ysc3839 avatar Aug 05 '20 23:08 ysc3839

Yeah, owner drawing is probably the best strategy for having a dark menu bar, at least until Microsoft adds one natively in the future.

jonaskohl avatar Aug 06 '20 11:08 jonaskohl

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.

VladWinner avatar Aug 07 '20 10:08 VladWinner

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 ContextMenus.

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);

}

Shomnipotence avatar Dec 22 '22 15:12 Shomnipotence