MenuItem.DrawItem Kejadian
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Terjadi ketika OwnerDraw properti item menu diatur ke true
dan permintaan dibuat untuk menggambar item 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
Jenis Acara
Contoh
Contoh kode berikut menunjukkan cara menangani DrawItem peristiwa. Contoh ini menggambar item menu menggunakan Brush dan Font, lalu menggambar Rectangle di sekitar item menu. Gambar dilakukan melalui Graphics objek , yang diteruskan ke penanganan aktivitas dalam DrawItemEventArgs parameter . Contoh ini mengharuskan Anda menginisialisasi OwnerDraw properti untuk item ke true
. Untuk contoh C#, tambahkan kode berikut di konstruktor formulir, setelah InitializeComponent
, untuk menghubungkan peristiwa:
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
Keterangan
Argumen DrawItemEventArgs yang DrawItem diteruskan ke penanganan aktivitas menyediakan Graphics objek yang memungkinkan Anda melakukan menggambar dan operasi grafis lainnya di permukaan item menu. Anda dapat menggunakan penanganan aktivitas ini untuk membuat menu kustom yang memenuhi kebutuhan aplikasi Anda. Untuk informasi selengkapnya tentang menangani peristiwa, lihat Menangani dan Menaikkan Peristiwa.