다음을 통해 공유


DrawItemEventHandler 대리자

ComboBox, ListBox, MenuItem 또는 TabControl 컨트롤의 DrawItem 이벤트를 처리할 메서드를 나타냅니다.

네임스페이스: System.Windows.Forms
어셈블리: System.Windows.Forms(system.windows.forms.dll)

구문

‘선언
Public Delegate Sub DrawItemEventHandler ( _
    sender As Object, _
    e As DrawItemEventArgs _
)
‘사용 방법
Dim instance As New DrawItemEventHandler(AddressOf HandlerMethod)
public delegate void DrawItemEventHandler (
    Object sender,
    DrawItemEventArgs e
)
public delegate void DrawItemEventHandler (
    Object^ sender, 
    DrawItemEventArgs^ e
)
/** @delegate */
public delegate void DrawItemEventHandler (
    Object sender, 
    DrawItemEventArgs e
)
JScript에서는 대리자를 사용할 수 있지만 새로 선언할 수는 없습니다.

매개 변수

  • sender
    이벤트 소스입니다.

설명

DrawItemEventArgs 대리자를 만드는 경우 이벤트를 처리할 메서드를 결정합니다. 이벤트를 이벤트 처리기와 연결하려면 대리자의 인스턴스를 해당 이벤트에 추가합니다. 대리자를 제거하지 않는 경우 이벤트가 발생할 때마다 이벤트 처리기가 호출됩니다. 이벤트 처리기 대리자에 대한 자세한 내용은 이벤트 및 대리자를 참조하십시오.

예제

다음 코드 예제에서는 소유자가 그린 메뉴 항목이 있는 메뉴를 보여 줍니다. 이 예제에서는 AddHandler 문과 AddressOf 연산자를 사용하여 MenuItem.DrawItem 이벤트를 처리하기 위한 대리자를 지정합니다. 이 예제를 실행하려면 System, System.Windows.Forms 및 System.Drawing 네임스페이스를 가져오는 폼에 다음 코드를 붙여넣습니다. 모든 이벤트가 해당 이벤트 처리 메서드와 연결되도록 합니다.

' 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, stringSize.Width, _
        stringSize.Height))
End Sub
// 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));
}
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.
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 = 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.set_OwnerDraw(true);
    //Add the event-handler delegate to handle the DrawItem event.
    menuItem2.add_DrawItem(new DrawItemEventHandler(DrawCustomMenuItem));
    // Add the items to the menu.
    mainMenu1 = new MainMenu(new MenuItem[] { menuItem1, menuItem2 });
    // Add the menu to the form.
    this.set_Menu(this.mainMenu1);
} //InitializeMenu

// 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.get_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.get_Graphics().MeasureString(customItem.get_Text(), aFont);
    // Draw the item and then draw the ellipse.
    e.get_Graphics().
        DrawString(customItem.get_Text(), aFont, aBrush,
        e.get_Bounds().get_X(), e.get_Bounds().get_Y());
    e.get_Graphics().
        DrawEllipse(new Pen(System.Drawing.Color.get_Black(), 2),
        new Rectangle(e.get_Bounds().get_X(), e.get_Bounds().get_Y(),
        System.Convert.ToInt32((System.Int32)stringSize.get_Width()),
        System.Convert.ToInt32((System.Int32)stringSize.get_Height())));
} //DrawCustomMenuItem

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

참고 항목

참조

System.Windows.Forms 네임스페이스
DrawItemEventArgs 클래스
ComboBox 클래스
ListBox
MenuItem
TabControl