ComboBox.DrawItem 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
發生於主控描繪 ComboBox 的視覺外觀變更時。
public:
event System::Windows::Forms::DrawItemEventHandler ^ DrawItem;
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
和 處理 DrawItem 和 MeasureItem 事件,來建立擁有者繪製的下拉式方塊。 它也示範如何設定 DropDownWidth 和 DropDownStyle 屬性。 若要執行範例,請將下列程式碼貼到表單中。
InitializeComboBox
在表單的建構函式或 Load 事件中呼叫 方法。
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 的 使用。 您可以使用此事件來執行 在 中 ComboBox 繪製專案所需的工作。 如果您在將 屬性設定為 OwnerDrawVariable
) 的值 System.Windows.Forms.DrawMode 時, (變數大小專案 ComboBox.DrawMode ,則會 MeasureItem 在繪製專案之前引發 事件。 您可以為 MeasureItem 事件建立事件處理常式,以指定要在事件的事件處理常式 DrawItem 中繪製的專案大小。
如需處理事件的詳細資訊,請參閱 處理和引發事件。