简体   繁体   English

Windows 11 上下文菜单样式

[英]Windows 11 Context Menu Style

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.为 Windows 提供的默认 styles Forms 上下文菜单控件(使用ContextMenuStrip )看起来像是为 Office XP 创建的东西 - 它的样式与 Windows 11 的样式不太匹配。

Windows 11 中旧上下文菜单的示例

For contrast, a modern Windows 11 context menu has rounded corners and is theme-aware:相比之下,现代 Windows 11 上下文菜单具有圆角并且是主题感知的:

主题感知 Windows 11 上下文菜单

Even for cases where it is not theme-aware for some legacy applications, it's still using rounded corners.即使对于某些遗留应用程序不支持主题的情况,它仍然使用圆角。

现代 Windows 11 上下文菜单示例

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?如何从 Windows Forms(或控制台)应用程序复制 Windows 11 托盘图标的主题感知样式和现代(圆角)上下文菜单?

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.理想情况下,我尽量不要从头开始编写整个托盘菜单渲染器,而只是重新使用内置的操作系统组件,但到目前为止,在弄清楚我需要使用什么工具来实现甚至得到菜单。

Use this class使用这个 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,
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM