MenuItem.DrawItem Событие

Определение

Вызывается, когда свойство OwnerDraw элемента меню устанавливается в значение true и выполняется запрос на отображение данного элемента меню.

public:
 event System::Windows::Forms::DrawItemEventHandler ^ DrawItem;
public event System.Windows.Forms.DrawItemEventHandler DrawItem;
member this.DrawItem : System.Windows.Forms.DrawItemEventHandler 
Public Custom Event DrawItem As DrawItemEventHandler 

Тип события

Примеры

В следующем примере кода показано, как обрабатывать событие DrawItem. В этом примере элемент меню рисуется с помощью Brush и Font, а затем рисуется Rectangle вокруг элемента меню. Рисование выполняется через Graphics объект , который передается обработчику событий в параметре DrawItemEventArgs . В этом примере требуется инициализировать OwnerDraw свойство для элемента в true. В примере C# добавьте следующий код в конструктор формы после InitializeComponent, чтобы подключить событие:

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

Комментарии

Аргумент DrawItemEventArgs , передаваемый обработчику DrawItem событий, предоставляет Graphics объект , позволяющий выполнять рисование и другие графические операции на поверхности элемента меню. Этот обработчик событий можно использовать для создания настраиваемых меню, соответствующих потребностям приложения. Дополнительные сведения об обработке событий см. в разделе Обработка и вызов событий.

Применяется к

См. также раздел