Hi egurre_egurre,
You can add a Form in your project and paste the code snippet below:
System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;
WindowsFormsApplication2
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{
//Add the menu strip.
MenuStrip menu = new MenuStrip();
this.Controls.Add(menu);
this.MainMenuStrip = menu;
//Add a menu.
MyMenuItem menuItem = new MyMenuItem();
menuItem.Offset = 6;
menuItem.Text = "File";
this.MainMenuStrip.Items.Add(menuItem);
//Add a sub menu.
MyMenuItem subItem = new MyMenuItem();
subItem.Offset = 34;
subItem.Text = "Open";
menuItem.DropDownItems.Add(subItem);
}
}
public class MyMenuItem : ToolStripMenuItem
{
//This paramter needs to be adjusted.
public int Offset { set; get; }
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (this.Selected)
{
e.Graphics.FillRectangle(Brushes.Green, e.ClipRectangle);
Rectangle rect = e.ClipRectangle;
rect.X += Offset;
rect.Width -= Offset;
StringFormat sf = new StringFormat();
sf.LineAlignment = StringAlignment.Center;
e.Graphics.DrawString(this.Text, this.Font, Brushes.Black, rect, sf);
}
}
}
Besides, we need to pay attention to the Offset property, its value needs to be modified based on the menu level. For example, the main menu and its sub menu would be different.
Please feel free to tell me if my answer doesn’t satisfy you or you need more help.
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.