MenuItem.DrawItem Event

Definition

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

C#
public event System.Windows.Forms.DrawItemEventHandler DrawItem;

Event Type

Examples

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);

C#

// 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)));
}

Remarks

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.

Applies to

Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 10

See also