MenuItem.DrawItem Event


Occurs when the OwnerDraw property of a menu item is set to true and a request is made to draw the menu item.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public event DrawItemEventHandler DrawItem

The DrawItemEventArgs argument passed to a DrawItem event handler provides a Graphics object that enables you to perform drawing and other graphical operations on the surface of the menu item. You can use this event handler to create custom menus that meet the needs of your application. For more information about handling events, see Handling and Raising Events.

The following code example demonstrates how to handle the DrawItem event. This example draws a menu item using a Brush and a Font, and then draws a Rectangle around the menu item. The drawing is performed through the Graphics object, which is passed to the event handler in the DrawItemEventArgs parameter. This example requires that you have initialized the OwnerDraw property for the item to true. For the C# example, add the following code in the form's constructor, after InitializeComponent, to hook up the event:

this.menuItem1.DrawItem += new DrawItemEventHandler(menuItem1_DrawItem);

// The DrawItem event handler.
private void menuItem1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)

	string myCaption = "Owner Draw Item1";

	// Create a Brush and a Font with which to draw the item.
	Brush myBrush = System.Drawing.Brushes.AliceBlue;
	Font myFont = new Font(FontFamily.GenericSerif, 14, FontStyle.Underline, GraphicsUnit.Pixel);
	SizeF mySizeF = e.Graphics.MeasureString(myCaption, myFont);

	// Draw the item, and then draw a Rectangle around it.
	e.Graphics.DrawString(myCaption, myFont, myBrush, e.Bounds.X, e.Bounds.Y);
	e.Graphics.DrawRectangle(Pens.Black, new Rectangle(e.Bounds.X, e.Bounds.Y, Convert.ToInt32(mySizeF.Width), Convert.ToInt32(mySizeF.Height)));


.NET Framework
Available since 1.1
Return to top