Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put an image beside the text of a ToolStripMenuItem (or similar control)?

Tags:

c#

.net

winforms

I know that it's possible to add an image to a ToolStripMenuItem, like in the "Close All Documents" item in the image below:

Menu with some ToolStripMenuItems

However, what I would like to achieve is something like this:

enter image description here

That is to place the image to the side of the item's text, but without overlapping the check space. I have searched a bit but couldn't find a Winforms control that can do this. Can you point me to any? Or do I have to implement my own? Thank you.

EDIT: thanks everyone for the answers! I will accept one after the weekend.

like image 797
elnigno Avatar asked Oct 18 '25 17:10

elnigno


2 Answers

You have to use some custom painting. There are some ways to implement this, however I would like to introduce to using some custom ToolStripRenderer. The following is just a demo code, you can improve it yourself. I save each inner Image in the corresponding Item's Tag for convenience:

public class CustomRenderer : ToolStripProfessionalRenderer
{
    int innerImagePadding = 2;
    protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e) {
        Image img = e.Item.Tag as Image;
        if(img == null) base.OnRenderItemText(e);
        else {
          e.Graphics.DrawImage(img, e.Item.ContentRectangle.Left + e.Item.Bounds.Height + innerImagePadding,
                               e.Item.ContentRectangle.Top + innerImagePadding, 
                               Math.Max(1, e.Item.ContentRectangle.Height - innerImagePadding*2), 
                               Math.Max(1, e.Item.ContentRectangle.Height - innerImagePadding*2));              
          Rectangle textRect = new Rectangle(e.Item.ContentRectangle.Left + e.Item.Bounds.Height*2, 
                               e.Item.ContentRectangle.Top +1, 
                               e.TextRectangle.Width, 
                               e.TextRectangle.Height);
          e.Graphics.DrawString(e.Text, e.TextFont, new SolidBrush(e.TextColor), textRect);
       }
    }
}

//Usage
ContextMenuStrip cm = new ContextMenuStrip();
cm.Items.Add(new ToolStripMenuItem("Clear all", myImage) {Tag = myImage});
cm.Items.Add(new ToolStripMenuItem("Remove all", myImage){Tag = myImage});
this.ContextMenuStrip = cm; //set the ContextMenuStrip for the form
//set the custom Renderer
cm.Renderer = new CustomRenderer();

enter image description here

like image 187
King King Avatar answered Oct 20 '25 07:10

King King


You can't do it from the designer, but you can show the extra margins by casting the parent DropDown property to a ToolStripDownDownMenu item to access the Show properties:

public Form1() {
  InitializeComponent();
  ((ToolStripDropDownMenu)FileMenuItem.DropDown).ShowCheckMargin = true;
  ((ToolStripDropDownMenu)FileMenuItem.DropDown).ShowImageMargin = true;
}

Result:

enter image description here

like image 30
LarsTech Avatar answered Oct 20 '25 09:10

LarsTech



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!