ysc3839 / win32-darkmode

Example application shows how to use undocumented dark mode API introduced in Windows 10 1809.
MIT License
437 stars 48 forks source link

[Question] How to make menu items dark #8

Open jonaskohl opened 4 years ago

jonaskohl commented 4 years ago

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?

ysc3839 commented 4 years ago

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

jonaskohl commented 4 years ago

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)

ysc3839 commented 4 years ago

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.

jonaskohl commented 4 years ago

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

VladWinner commented 4 years ago

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.

Shomnipotence commented 1 year ago

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

}