MenuItem.DrawItem Evento

Definición

Se produce cuando la propiedad OwnerDraw de un elemento del menú se establece en true y se realiza una solicitud para dibujar el elemento del menú.Occurs when the OwnerDraw property of a menu item is set to true and a request is made to draw the menu item.

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 

Ejemplos

En el ejemplo de código siguiente se muestra cómo DrawItem controlar el evento.The following code example demonstrates how to handle the DrawItem event. En este ejemplo se dibuja un elemento de Brush menú mediante Fonty Rectangle , y, a continuación, se dibuja alrededor del elemento de menú.This example draws a menu item using a Brush and a Font, and then draws a Rectangle around the menu item. El dibujo se realiza a través Graphics del objeto, que se pasa al controlador de eventos en DrawItemEventArgs el parámetro.The drawing is performed through the Graphics object, which is passed to the event handler in the DrawItemEventArgs parameter. En este ejemplo se requiere que se haya inicializado la OwnerDraw propiedad del elemento en. trueThis example requires that you have initialized the OwnerDraw property for the item to true. En el C# ejemplo, agregue el siguiente código en el constructor del formulario, después InitializeComponentde, para enlazar el evento: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;
      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

Comentarios

El DrawItemEventArgs argumento que se pasa DrawItem a un controlador de Graphics eventos proporciona un objeto que permite realizar el dibujo y otras operaciones gráficas en la superficie del elemento de menú.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. Puede usar este controlador de eventos para crear menús personalizados que satisfagan las necesidades de la aplicación.You can use this event handler to create custom menus that meet the needs of your application. Para obtener más información sobre el manejo de eventos, consulte controlar y provocar eventos.For more information about handling events, see Handling and Raising Events.

Se aplica a

Consulte también: