DrawItemEventHandler DrawItemEventHandler DrawItemEventHandler DrawItemEventHandler Delegate

定义

表示将处理 ComboBoxListBoxMenuItemTabControl 控件的 DrawItem 事件的方法。Represents the method that will handle the DrawItem event of a ComboBox, ListBox, MenuItem, or TabControl control.

public delegate void DrawItemEventHandler(System::Object ^ sender, DrawItemEventArgs ^ e);
public delegate void DrawItemEventHandler(object sender, DrawItemEventArgs e);
type DrawItemEventHandler = delegate of obj * DrawItemEventArgs -> unit
Public Delegate Sub DrawItemEventHandler(sender As Object, e As DrawItemEventArgs)

参数

sender
Object Object Object Object

事件源。The source of the event.

继承
DrawItemEventHandlerDrawItemEventHandlerDrawItemEventHandlerDrawItemEventHandler

示例

下面的代码示例演示一个具有所有者描述的菜单项的菜单。The following code example demonstrates a menu with an owner-drawn menu item. 此示例使用AddHandler语句AddressOf和运算符来指定用于处理事件的MenuItem.DrawItem委托。This example uses the AddHandler statement and the AddressOf operator to designate a delegate to handle the MenuItem.DrawItem event. 若要运行该示例, 请将其粘贴到导入系统、System.web 和 System.web 命名空间的窗体中。To run the example place paste it in a form that imports the System, System.Windows.Forms, and System.Drawing namespaces. 确保所有事件都与其事件处理方法关联。Ensure all events are associated with their event-handling methods.

internal:
   // Declare the MainMenu control.
   System::Windows::Forms::MainMenu^ MainMenu1;

   // Declare MenuItem2 as With-Events because it will be user drawn.
   System::Windows::Forms::MenuItem^ MenuItem2;

private:
   void InitializeMenu()
   {
      
      // Create MenuItem1, which will be drawn by the operating system.
      MenuItem^ MenuItem1 = gcnew MenuItem( "Regular Menu Item" );
      
      // Create MenuItem2.
      MenuItem2 = gcnew MenuItem( "Custom Menu Item" );
      
      // Set OwnerDraw property to true. This requires handling the
      // DrawItem event for this menu item.
      MenuItem2->OwnerDraw = true;
      
      //Add the event-handler delegate to handle the DrawItem event.
      MenuItem2->DrawItem += gcnew DrawItemEventHandler( this, &Form1::DrawCustomMenuItem );
      
      // Add the items to the menu.
      array<MenuItem^>^temp0 = {MenuItem1,MenuItem2};
      MainMenu1 = gcnew MainMenu( temp0 );
      
      // Add the menu to the form.
      this->Menu = this->MainMenu1;
   }

   // Draw the custom menu item.
   void DrawCustomMenuItem( Object^ sender, DrawItemEventArgs^ e )
   {
      // Cast the sender to MenuItem so you can access text property.
      MenuItem^ customItem = dynamic_cast<MenuItem^>(sender);
      
      // Create a Brush and a Font to draw the MenuItem.
      System::Drawing::Brush^ aBrush = System::Drawing::Brushes::DarkMagenta;
      System::Drawing::Font^ aFont = gcnew System::Drawing::Font( "Garamond",10,FontStyle::Italic,GraphicsUnit::Point );
      
      // Get the size of the text to use later to draw an ellipse
      // around the item.
      SizeF stringSize = e->Graphics->MeasureString( customItem->Text, aFont );
      
      // Draw the item and then draw the ellipse.
      e->Graphics->DrawString( customItem->Text, aFont, aBrush, (float)e->Bounds.X, (float)e->Bounds.Y );
      e->Graphics->DrawEllipse( gcnew Pen( System::Drawing::Color::Black,2 ), Rectangle(e->Bounds.X,e->Bounds.Y,(System::Int32)stringSize.Width,(System::Int32)stringSize.Height) );
   }
// Declare the MainMenu control.
internal System.Windows.Forms.MainMenu MainMenu1;

// Declare MenuItem2 as With-Events because it will be user drawn.
internal System.Windows.Forms.MenuItem MenuItem2;


private void InitializeMenu()
{

	// Create MenuItem1, which will be drawn by the operating system.
	MenuItem MenuItem1 = new MenuItem("Regular Menu Item");

	// Create MenuItem2.
	MenuItem2 = new MenuItem("Custom Menu Item");

	// Set OwnerDraw property to true. This requires handling the
	// DrawItem event for this menu item.
	MenuItem2.OwnerDraw = true;

	//Add the event-handler delegate to handle the DrawItem event.
       MenuItem2.DrawItem += new DrawItemEventHandler(DrawCustomMenuItem);
	
       // Add the items to the menu.
	MainMenu1 = new MainMenu(new MenuItem[]{MenuItem1, MenuItem2});																													  

	// Add the menu to the form.
	this.Menu = this.MainMenu1;
}

// Draw the custom menu item.
private void DrawCustomMenuItem(object sender, 
	DrawItemEventArgs e)
{

	// Cast the sender to MenuItem so you can access text property.
	MenuItem customItem = (MenuItem) sender;

	// Create a Brush and a Font to draw the MenuItem.
	System.Drawing.Brush aBrush = System.Drawing.Brushes.DarkMagenta;
	Font aFont = new Font("Garamond", 10, 
		FontStyle.Italic, GraphicsUnit.Point);

	// Get the size of the text to use later to draw an ellipse
	// around the item.
	SizeF stringSize = e.Graphics.MeasureString(
		customItem.Text, aFont);

	// Draw the item and then draw the ellipse.
	e.Graphics.DrawString(customItem.Text, aFont, 
		aBrush, e.Bounds.X, e.Bounds.Y);
	e.Graphics.DrawEllipse(new Pen(System.Drawing.Color.Black, 2),
		new Rectangle(e.Bounds.X, e.Bounds.Y, 
		(System.Int32)stringSize.Width,
		(System.Int32)stringSize.Height));
}
' Declare the MainMenu control.
Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu

' Declare MenuItem2 as With-Events because it will be user drawn.
Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem


Private Sub InitializeMenu()

    ' Create MenuItem1, which will be drawn by the operating system.
    Dim MenuItem1 As New MenuItem("Regular Menu Item")

    ' Create MenuItem2.
    MenuItem2 = New MenuItem("Custom Menu Item")

    ' Set OwnerDraw property to true. This requires handling the
    ' DrawItem event for this menu item.
    MenuItem2.OwnerDraw = True

    'Add the event-handler delegate to handle the DrawItem event.
    AddHandler MenuItem2.DrawItem, New DrawItemEventHandler(AddressOf DrawCustomMenuItem)

    ' Add the items to the menu.
    MainMenu1 = New MainMenu(New MenuItem() {MenuItem1, MenuItem2})

    ' Add the menu to the form.
    Me.Menu = Me.MainMenu1
End Sub

' Draw the custom menu item.
Private Sub DrawCustomMenuItem(ByVal sender As Object, ByVal e As _
        System.Windows.Forms.DrawItemEventArgs)

    ' Cast the sender to MenuItem so you can access text property.
    Dim customItem As MenuItem = CType(sender, MenuItem)

    ' Create a Brush and a Font to draw the MenuItem.
    Dim aBrush As System.Drawing.Brush = System.Drawing.Brushes.DarkMagenta
    Dim aFont As New Font("Garamond", 10, FontStyle.Italic, _
        GraphicsUnit.Point)

    ' Get the size of the text to use later to draw an ellipse
    ' around the item.
    Dim stringSize As SizeF = e.Graphics.MeasureString( _
        customItem.Text, aFont)

    ' Draw the item and then draw the ellipse.
    e.Graphics.DrawString(customItem.Text, aFont, _
        aBrush, e.Bounds.X, e.Bounds.Y)
    e.Graphics.DrawEllipse(New Pen(System.Drawing.Color.Black, 2), _
        New Rectangle(e.Bounds.X, e.Bounds.Y, CInt(stringSize.Width), _
        CInt(stringSize.Height)))
End Sub

注解

创建 DrawItemEventArgs 委托时,需要标识将处理该事件的方法。When you create a DrawItemEventArgs delegate, you identify the method that will handle the event. 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。To associate the event with your event handler, add an instance of the delegate to the event. 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。The event handler is called whenever the event occurs, unless you remove the delegate. 有关事件处理程序委托的详细信息, 请参阅处理和引发事件For more information about event handler delegates, see Handling and Raising Events.

扩展方法

GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate)

获取指示指定委托表示的方法的对象。Gets an object that represents the method represented by the specified delegate.

适用于

另请参阅