简体   繁体   English

如何将图标添加到上下文菜单

[英]How to add an icon to a context menu

I have a notification icon with a ContextMenu attached (rather than a ContextMenuStrip , in order to keep the Windows look-and-feel), as detailed here . 我有一个带有ContextMenu的通知图标(而不是ContextMenuStrip ,以便保持Windows的外观), 如此处所述

How can I add an icon to the context menu items? 如何在上下文菜单项中添加图标?

If you have to use a MenuItem, you'll have to draw the menuitem yourself and add an image. 如果必须使用MenuItem,则必须自己绘制菜单项并添加图像。 For that, set the OwnerDraw property to true and subscribe to the DrawItem event. 为此,将OwnerDraw属性设置为true并订阅DrawItem事件。

Or 要么

You can derive your own MenuItem class: 您可以派生自己的MenuItem类:

public class MyMenuItem : MenuItem
{
    public MyMenuItem()
    {
        OwnerDraw = true;

    }
    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        base.OnDrawItem(e);

        // Add paint logic here
    }
}

For specific links on how to draw on the MenuItem see here and here 有关如何在MenuItem上绘制的特定链接,请参见此处此处。

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

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