Export (0) Print
Expand All

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

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft