ComboBox.DrawItem ComboBox.DrawItem ComboBox.DrawItem ComboBox.DrawItem Event

정의

소유자가 그린 ComboBox의 시각적 측면이 변경될 때 발생합니다.Occurs when a visual aspect of an owner-drawn ComboBox changes.

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 

예제

다음 코드 예제에는 설정 하 여는 소유자가 그린 콤보 상자를 만드는 방법을 보여 줍니다 합니다 DrawMode 속성을 OwnerDrawnVariable 하 고 처리 합니다 DrawItemMeasureItem 이벤트.The following code example demonstrates how to create an owner-drawn combo box by setting the DrawMode property to OwnerDrawnVariable and handling the DrawItem and MeasureItem events. 설정 하는 방법도 보여 줍니다 합니다 DropDownWidthDropDownStyle 속성입니다.It also demonstrates setting the DropDownWidth and DropDownStyle properties. 예제를 실행 하는 폼에 다음 코드를 붙여넣습니다.To run the example, paste the following code in a form. 호출 된 InitializeComboBox 폼의 생성자 또는 Load 이벤트입니다.Call the InitializeComboBox method in the form's constructor or Load event.

internal:
   System::Windows::Forms::ComboBox^ ComboBox1;

private:
   array<String^>^ animals;

   // This method initializes the owner-drawn combo box.
   // The drop-down width is set much wider than the size of the combo box
   // to accomodate the large items in the list.  The drop-down style is set to 
   // ComboBox.DropDown, which requires the user to click on the arrow to 
   // see the list.
   void InitializeComboBox()
   {
      this->ComboBox1 = gcnew ComboBox;
      this->ComboBox1->DrawMode = System::Windows::Forms::DrawMode::OwnerDrawVariable;
      this->ComboBox1->Location = System::Drawing::Point( 10, 20 );
      this->ComboBox1->Name = "ComboBox1";
      this->ComboBox1->Size = System::Drawing::Size( 100, 120 );
      this->ComboBox1->DropDownWidth = 250;
      this->ComboBox1->TabIndex = 0;
      this->ComboBox1->DropDownStyle = ComboBoxStyle::DropDown;
      animals = gcnew array<String^>{"Elephant","c r o c o d i l e","lion"};
      ComboBox1->DataSource = animals;
      this->Controls->Add( this->ComboBox1 );
      
      // Hook up the MeasureItem and DrawItem events
      this->ComboBox1->DrawItem +=
         gcnew DrawItemEventHandler( this, &Form1::ComboBox1_DrawItem );
      this->ComboBox1->MeasureItem +=
         gcnew MeasureItemEventHandler( this, &Form1::ComboBox1_MeasureItem );
   }

   // If you set the Draw property to DrawMode.OwnerDrawVariable, 
   // you must handle the MeasureItem event. This event handler 
   // will set the height and width of each item before it is drawn. 
private:
   void ComboBox1_MeasureItem( Object^ sender,
      System::Windows::Forms::MeasureItemEventArgs^ e )
   {
      switch ( e->Index )
      {
         case 0:
            e->ItemHeight = 45;
            break;
         case 1:
            e->ItemHeight = 20;
            break;
         case 2:
            e->ItemHeight = 35;
            break;
      }
      e->ItemWidth = 260;
   }

private:
   // You must handle the DrawItem event for owner-drawn combo boxes.  
   // This event handler changes the color, size and font of an 
   // item based on its position in the array.
   void ComboBox1_DrawItem( Object^ sender,
      System::Windows::Forms::DrawItemEventArgs^ e )
   {
      float size = 0;
      System::Drawing::Font^ myFont;
      FontFamily^ family = nullptr;

      System::Drawing::Color animalColor;
      switch ( e->Index )
      {
         case 0:
            size = 30;
            animalColor = System::Drawing::Color::Gray;
            family = FontFamily::GenericSansSerif;
            break;
         case 1:
            size = 10;
            animalColor = System::Drawing::Color::LawnGreen;
            family = FontFamily::GenericMonospace;
            break;
         case 2:
            size = 15;
            animalColor = System::Drawing::Color::Tan;
            family = FontFamily::GenericSansSerif;
            break;
      }

      // Draw the background of the item.
      e->DrawBackground();
      
      // Create a square filled with the animals color. Vary the size
      // of the rectangle based on the length of the animals name.
      Rectangle rectangle = Rectangle( 2, e->Bounds.Top + 2,
         e->Bounds.Height, e->Bounds.Height - 4 );
      e->Graphics->FillRectangle( gcnew SolidBrush( animalColor ), rectangle );
      
      // Draw each string in the array, using a different size, color,
      // and font for each item.
      myFont = gcnew System::Drawing::Font( family, size, FontStyle::Bold );
      e->Graphics->DrawString( animals[ e->Index ], myFont,
         System::Drawing::Brushes::Black, RectangleF(
            e->Bounds.X + rectangle.Width, e->Bounds.Y,
            e->Bounds.Width, e->Bounds.Height) );
      
      // Draw the focus rectangle if the mouse hovers over an item.
      e->DrawFocusRectangle();
   }

internal System.Windows.Forms.ComboBox ComboBox1;
private string[] animals;
 
// This method initializes the owner-drawn combo box.
// The drop-down width is set much wider than the size of the combo box
// to accomodate the large items in the list.  The drop-down style is set to 
// ComboBox.DropDown, which requires the user to click on the arrow to 
// see the list.
private void InitializeComboBox()
{
	this.ComboBox1 = new ComboBox();
	this.ComboBox1.DrawMode = 
		System.Windows.Forms.DrawMode.OwnerDrawVariable;
	this.ComboBox1.Location = new System.Drawing.Point(10, 20);
	this.ComboBox1.Name = "ComboBox1";
	this.ComboBox1.Size = new System.Drawing.Size(100, 120);
	this.ComboBox1.DropDownWidth = 250;
	this.ComboBox1.TabIndex = 0;
	this.ComboBox1.DropDownStyle = ComboBoxStyle.DropDown;
	animals = new string[]{"Elephant", "c r o c o d i l e", "lion"};
	ComboBox1.DataSource = animals;
	this.Controls.Add(this.ComboBox1);

	// Hook up the MeasureItem and DrawItem events
	this.ComboBox1.DrawItem += 
		new DrawItemEventHandler(ComboBox1_DrawItem);
	this.ComboBox1.MeasureItem += 
		new MeasureItemEventHandler(ComboBox1_MeasureItem);
}

// If you set the Draw property to DrawMode.OwnerDrawVariable, 
// you must handle the MeasureItem event. This event handler 
// will set the height and width of each item before it is drawn. 
private void ComboBox1_MeasureItem(object sender, 
	System.Windows.Forms.MeasureItemEventArgs e)
{

	switch(e.Index)
	{
		case 0:
			e.ItemHeight = 45;
			break;
		case 1:
			e.ItemHeight = 20;
			break;
		case 2:
			e.ItemHeight = 35;
			break;
	}
	e.ItemWidth = 260;

}

// You must handle the DrawItem event for owner-drawn combo boxes.  
// This event handler changes the color, size and font of an 
// item based on its position in the array.
private void ComboBox1_DrawItem(object sender, 
	System.Windows.Forms.DrawItemEventArgs e)
{

	float size = 0;
	System.Drawing.Font myFont;
	FontFamily family = null;

	System.Drawing.Color animalColor = new System.Drawing.Color();
	switch(e.Index)
	{
		case 0:
			size = 30;
			animalColor = System.Drawing.Color.Gray;
			family = FontFamily.GenericSansSerif;
			break;
		case 1:
			size = 10;
			animalColor = System.Drawing.Color.LawnGreen;
			family = FontFamily.GenericMonospace;
			break;
		case 2:
			size = 15;
			animalColor = System.Drawing.Color.Tan;
			family = FontFamily.GenericSansSerif;
			break;
	}

	// Draw the background of the item.
	e.DrawBackground();

	// Create a square filled with the animals color. Vary the size
	// of the rectangle based on the length of the animals name.
	Rectangle rectangle = new Rectangle(2, e.Bounds.Top+2, 
			e.Bounds.Height, e.Bounds.Height-4);
	e.Graphics.FillRectangle(new SolidBrush(animalColor), rectangle);

	// Draw each string in the array, using a different size, color,
	// and font for each item.
	myFont = new Font(family, size, FontStyle.Bold);
	e.Graphics.DrawString(animals[e.Index], myFont, System.Drawing.Brushes.Black, new RectangleF(e.Bounds.X+rectangle.Width, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));

	// Draw the focus rectangle if the mouse hovers over an item.
	e.DrawFocusRectangle();
}


Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
Private animals() As String

' This method initializes the owner-drawn combo box.
' The drop-down width is set much wider than the size of the combo box
' to accomodate the large items in the list.  The drop-down style is set to 
' ComboBox.DropDown, which requires the user to click on the arrow to 
' see the list.
Private Sub InitializeComboBox()
    Me.ComboBox1 = New ComboBox
    Me.ComboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable
    Me.ComboBox1.Location = New System.Drawing.Point(10, 20)
    Me.ComboBox1.Name = "ComboBox1"
    Me.ComboBox1.Size = New System.Drawing.Size(100, 120)
    Me.ComboBox1.DropDownWidth = 250
    Me.ComboBox1.TabIndex = 0
    Me.ComboBox1.DropDownStyle = ComboBoxStyle.DropDown
    animals = New String() {"Elephant", "c r o c o d i l e", "lion"}
    ComboBox1.DataSource = animals
    Me.Controls.Add(Me.ComboBox1)
End Sub

' If you set the Draw property to DrawMode.OwnerDrawVariable, 
' you must handle the MeasureItem event. This event handler 
' will set the height and width of each item before it is drawn. 
 Private Sub ComboBox1_MeasureItem(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MeasureItemEventArgs) _
        Handles ComboBox1.MeasureItem

    Select Case e.Index
        Case 0
            e.ItemHeight = 45
        Case 1
            e.ItemHeight = 20
        Case 2
            e.ItemHeight = 35
    End Select
    e.ItemWidth = 260

End Sub

' You must handle the DrawItem event for owner-drawn combo boxes.  
' This event handler changes the color, size and font of an 
' item based on its position in the array.
Private Sub ComboBox1_DrawItem(ByVal sender As Object,  _ 
    ByVal e As System.Windows.Forms.DrawItemEventArgs) _
    Handles ComboBox1.DrawItem

    Dim size As Single
    Dim myFont As System.Drawing.Font
    Dim family As FontFamily

    Dim animalColor As New System.Drawing.Color
    Select Case e.Index
        Case 0
            size = 30
            animalColor = System.Drawing.Color.Gray
            family = FontFamily.GenericSansSerif
        Case 1
            size = 10
            animalColor = System.Drawing.Color.LawnGreen
            family = FontFamily.GenericMonospace
        Case 2
            size = 15
            animalColor = System.Drawing.Color.Tan
            family = FontFamily.GenericSansSerif
    End Select

    ' Draw the background of the item.
    e.DrawBackground()

    ' Create a square filled with the animals color. Vary the size
    ' of the rectangle based on the length of the animals name.
    Dim rectangle As Rectangle = New Rectangle(2, e.Bounds.Top + 2, _
        e.Bounds.Height, e.Bounds.Height - 4)
    e.Graphics.FillRectangle(New SolidBrush(animalColor), rectangle)

    ' Draw each string in the array, using a different size, color,
    ' and font for each item.
    myFont = New Font(family, size, FontStyle.Bold)
    e.Graphics.DrawString(animals(e.Index), myFont, System.Drawing.Brushes.Black, _
        New RectangleF(e.Bounds.X + rectangle.Width, e.Bounds.Y, _ 
        e.Bounds.Width, e.Bounds.Height))

    ' Draw the focus rectangle if the mouse hovers over an item.
    e.DrawFocusRectangle()
End Sub

설명

이 이벤트는 소유자가 그린 사용한 ComboBox합니다.This event is used by an owner-drawn ComboBox. 이 이벤트를 사용 하 여 항목을 그리는 데 필요한 작업을 수행 하는 ComboBox합니다.You can use this event to perform the tasks needed to draw items in the ComboBox. 가변 크기 항목이 있는 경우 (때를 ComboBox.DrawMode 속성을 OwnerDrawVariableSystem.Windows.Forms.DrawMode), 항목을 그리기 전에 MeasureItem 이벤트가 발생 합니다.If you have a variable sized item (when the ComboBox.DrawMode property is set to the OwnerDrawVariable value of System.Windows.Forms.DrawMode), before drawing an item, the MeasureItem event is raised. 에 대 한 이벤트 처리기를 만들 수 있습니다 합니다 MeasureItem 이벤트에 대 한 이벤트 처리기에서 그릴 하려는 항목에 대 한 크기를 지정 하는 DrawItem 이벤트입니다.You can create an event handler for the MeasureItem event to specify the size for the item that you are going to draw in your event handler for the DrawItem event.

이벤트 처리에 대한 자세한 내용은 이벤트 처리 및 발생 을 참조하십시오.For more information about handling events, see Handling and Raising Events.

적용 대상