简体   繁体   中英

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 .

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. For that, set the OwnerDraw property to true and subscribe to the DrawItem event.

Or

You can derive your own MenuItem class:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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