MenuItem.DrawItem Zdarzenie

Definicja

Występuje, gdy OwnerDraw właściwość elementu menu jest ustawiona na true , a żądanie jest wykonywane w celu narysowania elementu menu.

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 

Typ zdarzenia

Przykłady

W poniższym przykładzie kodu pokazano, jak obsłużyć DrawItem zdarzenie. W tym przykładzie rysuje element menu przy użyciu elementu Brush i Font, a następnie rysuje Rectangle wokół elementu menu. Rysunek jest wykonywany przez Graphics obiekt, który jest przekazywany do programu obsługi zdarzeń w parametrze DrawItemEventArgs . Ten przykład wymaga zainicjowania OwnerDraw właściwości elementu na truewartość . W przykładzie języka C# dodaj następujący kod w konstruktorze formularza po InitializeComponent, aby podłączyć zdarzenie:

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

Uwagi

Argument DrawItemEventArgs przekazany do DrawItem procedury obsługi zdarzeń udostępnia Graphics obiekt, który umożliwia wykonywanie rysunku i innych operacji graficznych na powierzchni elementu menu. Za pomocą tej procedury obsługi zdarzeń można tworzyć niestandardowe menu spełniające potrzeby aplikacji. Aby uzyskać więcej informacji na temat obsługi zdarzeń, zobacz Obsługa i podnoszenie zdarzeń.

Dotyczy

Zobacz też