Articles in this section
Category / Section

How to add an icon in a header of WinForms ContextMenuStripEx?

3 mins read

By default, there is no support to add the image in header of ContextMenuStripEx, but you can override the Paint method of ContextMenuStripEx control by rendering the customized ContextMenuStripEx class.

Step 1: Create the custom render class called CustomContextMenuStripEx inheriting from ContextMenuStripEx.

Step 2: Override the paint method to add the image in header area of ContextMenuStripEx.

Step 3: Then initialize the custom render class named CustomContextMenuStripEx  and add the ToolStripMenuItem in it.

Step 4: Now assign the initialized contextmenustrip to the Form’s ContextMenuStrip property.

Code:[C#]

 
//Custom renderer class
public class CustomContextMenuStripEx  :  ContextMenuStripEx
{
     public override Rectangle DisplayRectangle
     {
          get
          {
               Rectangle rc = base.DisplayRectangle;
               rc.Y += TitleHeight;
               return rc;
          }
     }
 
     public override Size GetPreferredSize(Size proposedSize)
     {
          Size szResult = base.GetPreferredSize(proposedSize);
          szResult.Height += TitleHeight;
          return szResult;
     }
 
     internal int TitleHeight
     {
          get { return 25; }
     }
 
     protected override void OnPaint(PaintEventArgs e)
     {
          base.OnPaint(e);
          Image image = Image.FromFile(@"..\..\Images\ExitIcon.png");
          e.Graphics.FillRectangle(new SolidBrush(Control.DefaultBackColor), 1, 1, this.Width - 2, TitleHeight);
          Rectangle imgRect = new Rectangle(2, 2, 16, 16);
          //Add the image
          e.Graphics.DrawImage(image, imgRect);
          Rectangle textRect = new Rectangle(imgRect.Width + 2, 2, this.Width, TitleHeight);
          e.Graphics.DrawString(Text, this.Font, new SolidBrush(Color.Black), textRect);
     }
}
 

 

Assigning the custom renderer ContextMenuStripEx class to the Form ContextMenuStrip Property.

 
public Form1()
{
     InitializeComponent();
     this.StartPosition = FormStartPosition.CenterScreen;
     //Initializing the custom renderer class
     this.contextMenuStripEx = new CustomContextMenuStripEx();
     this.contextMenuStripEx.Text = "Exit";
     this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
     this.toolStripMenuItem2 = new System.Windows.Forms.ToolStripMenuItem();
     this.toolStripMenuItem3 = new System.Windows.Forms.ToolStripMenuItem();
     //Associate the context menu
     this.toolStripMenuItem1.Image = System.Drawing.Image.FromFile(@"..\..\Images\CopyHS.png");
     this.toolStripMenuItem2.Image = System.Drawing.Image.FromFile(@"..\..\Images\CutHS.png");
     this.toolStripMenuItem3.Image = System.Drawing.Image.FromFile(@"..\..\Images\PasteHS.png");
     this.toolStripMenuItem1.Text = "New";
     this.toolStripMenuItem2.Text = "Copy";
     this.toolStripMenuItem3.Text = "Cut";
     this.contextMenuStripEx.Items.AddRange(new System.Windows.Forms.ToolStripItem[]      
     { 
          this.toolStripMenuItem1, this.toolStripMenuItem2, this.toolStripMenuItem3 
     });
     //Add the CustomcontextmenustripEx to form ContextMenuStrip
     this.ContextMenuStrip = this.contextMenuStripEx;
}
 

 

Output screenshot:

ContextMenuStripEx with header image

 

[View Sample in GitHub]

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied