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
/** @event */
public void add_DrawItem (DrawItemEventHandler value)

/** @event */
public void remove_DrawItem (DrawItemEventHandler value)

In JScript, you can handle the events defined by a class, but you cannot define your own.
Not applicable.

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

}

// 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.get_AliceBlue();
    Font myFont = new Font(FontFamily.get_GenericSerif(), 14, 
         FontStyle.Underline, GraphicsUnit.Pixel);
    SizeF mySizeF = e.get_Graphics().MeasureString(myCaption, myFont);
    // Draw the item, and then draw a Rectangle around it.
    e.get_Graphics().DrawString(myCaption, myFont, myBrush,
        e.get_Bounds().get_X(), e.get_Bounds().get_Y());
    e.get_Graphics().DrawRectangle(Pens.get_Black(), 
        new Rectangle(e.get_Bounds().get_X(), e.get_Bounds().get_Y(), 
        Convert.ToInt32(mySizeF.get_Width()), 
        Convert.ToInt32(mySizeF.get_Height())));
} //menuItem1_DrawItem

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft