MenuItem.DrawItem Událost

Definice

Vyvolá se, OwnerDraw když je vlastnost položky nabídky nastavena na true a je proveden požadavek na vykreslení položky nabídky.

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 

Event Type

Příklady

Následující příklad kódu ukazuje, jak zpracovat DrawItem událost. Tento příklad nakreslí položku nabídky pomocí Brush a a Fontpak nakreslí Rectangle kolem položky nabídky. Kreslení se provádí prostřednictvím objektu Graphics , který je předán obslužné rutině události v parametru DrawItemEventArgs . Tento příklad vyžaduje inicializaci OwnerDraw vlastnosti položky na true. V příkladu jazyka C# přidejte do konstruktoru formuláře za InitializeComponentnásledující kód, který připojí událost:

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

Poznámky

Argument DrawItemEventArgs předaný obslužné DrawItem rutině události poskytuje Graphics objekt, který umožňuje provádět kreslení a další grafické operace na povrchu položky nabídky. Tuto obslužnou rutinu události můžete použít k vytvoření vlastních nabídek, které vyhovují potřebám vaší aplikace. Další informace o zpracování událostí najdete v tématu Zpracování a vyvolávání událostí.

Platí pro

Viz také