We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: WinForms |
Control: General |
Published Date: September 15, 2020 |
Last Revised Date: September 15, 2020

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]

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon