MenuItem.DrawItem 事件

当菜单项的 OwnerDraw 属性设置为 true 并且发出绘制菜单项的请求时发生。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Event DrawItem As DrawItemEventHandler
用法
Dim instance As MenuItem
Dim handler As DrawItemEventHandler

AddHandler instance.DrawItem, handler
public event DrawItemEventHandler DrawItem
public:
event DrawItemEventHandler^ DrawItem {
    void add (DrawItemEventHandler^ value);
    void remove (DrawItemEventHandler^ value);
}
/** @event */
public void add_DrawItem (DrawItemEventHandler value)

/** @event */
public void remove_DrawItem (DrawItemEventHandler value)
JScript 支持使用事件,但不支持进行新的声明。

备注

传递给 DrawItem 事件处理程序的 DrawItemEventArgs 参数提供使您得以在菜单项图面上执行绘图和其他图形操作的 Graphics 对象。可以使用该事件处理程序创建满足应用程序需要的自定义菜单。有关处理事件的更多信息,请参见 使用事件

示例

下面的代码示例演示如何处理 DrawItem 事件。本示例使用 BrushFont 绘制一个菜单项,然后在菜单项周围绘制一个 Rectangle。绘制工作是通过 Graphics 对象执行的,该对象被 DrawItemEventArgs 参数的事件处理程序使用。此示例要求已经将此项的 OwnerDraw 属性初始化为 true。对于 C# 示例,请在窗体的构造函数中添加以下代码,放在 InitializeComponent 后,以便挂接到事件上:

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

' 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
// 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::AliceBlue;
      System::Drawing::Font^ myFont = gcnew 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 )) );
   }
// 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 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

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

MenuItem 类
MenuItem 成员
System.Windows.Forms 命名空间
MenuItem.OwnerDraw 属性
MeasureItem