7

Default styles offered for the Windows Forms context menu control (using ContextMenuStrip) are looking like something that was created for Office XP - its styling doesn't quite match that of Windows 11.

Example of an old context menu in Windows 11

For contrast, a modern Windows 11 context menu has rounded corners and is theme-aware:

Theme-aware Windows 11 context menu

Even for cases where it is not theme-aware for some legacy applications, it's still using rounded corners.

Example of modern Windows 11 context menu

How can I replicate the theme-aware style and the modern (rounded corner) context menu for a Windows 11 tray icon from a Windows Forms (or Console) application?

Ideally, I am trying not to write a whole tray menu renderer from scratch and instead just re-use built-in OS components, but so far I am running into a wall when it comes to figuring out what tooling I need to use to even get the menu.

3
  • According to Raymond Chen, Windows 11-style menus are built from XAML CommandBars. Mar 9, 2022 at 2:45
  • Native win32 apps can opt in or out of the rounding and Windows rounds by default if it does not detect owner draw, window regions etc.
    – Anders
    Mar 9, 2022 at 5:03
  • 1
    The ContextMenu class uses the OS-provided context menu services, ContextMenuStrip has its own renderer. Which was not updated to be Win11 aware. One caveat for ContextMenu is that there's no design-time support for it in a .NETCore project. Mar 9, 2022 at 10:57

1 Answer 1

2

Use this class

class CustomContextMenu : ContextMenuStrip
{
    [DllImport("dwmapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern long DwmSetWindowAttribute(IntPtr hwnd,
                                                        DWMWINDOWATTRIBUTE attribute,
                                                        ref DWM_WINDOW_CORNER_PREFERENCE pvAttribute,
                                                        uint cbAttribute);
    
    public CustomContextMenu()
    {
        var preference = DWM_WINDOW_CORNER_PREFERENCE.DWMWCP_ROUND;     //change as you want
        DwmSetWindowAttribute(Handle,
                              DWMWINDOWATTRIBUTE.DWMWA_WINDOW_CORNER_PREFERENCE,
                              ref preference,
                              sizeof(uint));
    }

    public enum DWMWINDOWATTRIBUTE
    {
        DWMWA_WINDOW_CORNER_PREFERENCE = 33
    }
    public enum DWM_WINDOW_CORNER_PREFERENCE
    {
        DWMWA_DEFAULT = 0,
        DWMWCP_DONOTROUND = 1,
        DWMWCP_ROUND = 2,
        DWMWCP_ROUNDSMALL = 3,
    }
}
1
  • Sorry that does not look the same at all.
    – Edwin
    May 20 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.