Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How to make menu items dark #8

Open
jonaskohl opened this issue Aug 5, 2020 · 6 comments
Open

[Question] How to make menu items dark #8

jonaskohl opened this issue Aug 5, 2020 · 6 comments

Comments

@jonaskohl
Copy link

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
Copy link
Owner

ysc3839 commented Aug 5, 2020

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
Copy link
Author

jonaskohl commented Aug 5, 2020

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
Copy link
Owner

ysc3839 commented Aug 5, 2020

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
Copy link
Author

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
Copy link

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
Copy link

Shomnipotence commented Dec 22, 2022

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

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants