Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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.

[Visual Basic]
Public Event DrawItem As DrawItemEventHandler
[C#]
public event DrawItemEventHandler DrawItem;
[C++]
public: __event DrawItemEventHandler* DrawItem;

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

Event Data

The event handler receives an argument of type DrawItemEventArgs containing data related to this event. The following DrawItemEventArgs properties provide information specific to this event.

Property Description
BackColor Gets the background color of the item that is being drawn.
Bounds Gets the rectangle that represents the bounds of the item that is being drawn.
Font Gets the font assigned to the item being drawn.
ForeColor Gets the foreground color of the of the item being drawn.
Graphics Gets the graphics surface to draw the item on.
Index Gets the index value of the item that is being drawn.
State Gets the state of the item being drawn.

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 Consuming Events.

Example

[Visual Basic, C#, C++] The following 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 assumes that you have initialized the OwnerDraw property for the item to true.

[Visual Basic] 
' The DrawItem event handler.
Private Sub MenuItem1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles MenuItem1.DrawItem


    Dim MyCaption As String = "Owner Draw Item1"

    ' Create a Brush and a Font with which to draw the item.
    Dim MyBrush As System.Drawing.Brush = System.Drawing.Brushes.AliceBlue
    Dim MyFont As New Font(FontFamily.GenericSerif, 14, FontStyle.Underline, GraphicsUnit.Pixel)
    Dim MySizeF As SizeF = 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(Drawing.Pens.Black, New Rectangle(e.Bounds.X, e.Bounds.Y, MySizeF.Width, MySizeF.Height))

End Sub


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

}

[C++] 
private:
    // The DrawItem event handler.
    void menuItem1_DrawItem(Object* /*sender*/, 
        System::Windows::Forms::DrawItemEventArgs* e) {

            String* myCaption = S"Owner Draw Item1";

            // Create a Brush and a Font with which to draw the item.
            Brush* myBrush = System::Drawing::Brushes::AliceBlue;
            System::Drawing::Font* myFont = 
                new System::Drawing::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, 
                (float)e->Bounds.X, (float)e->Bounds.Y);
            e->Graphics->DrawRectangle(Pens::Black, 
                Rectangle(e->Bounds.X, e->Bounds.Y, 
                Convert::ToInt32(mySizeF.Width), 
                Convert::ToInt32(mySizeF.Height)));

    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

MenuItem Class | MenuItem Members | System.Windows.Forms Namespace | OwnerDraw | MeasureItem

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.