DrawItemEventArgs DrawItemEventArgs DrawItemEventArgs DrawItemEventArgs Class

定义

DrawItem 事件提供数据。Provides data for the DrawItem event.

public ref class DrawItemEventArgs : EventArgs
public class DrawItemEventArgs : EventArgs
type DrawItemEventArgs = class
    inherit EventArgs
Public Class DrawItemEventArgs
Inherits EventArgs
继承
DrawItemEventArgsDrawItemEventArgsDrawItemEventArgsDrawItemEventArgs
派生

示例

下面的示例演示如何创建所有者绘制ListBox项。The following example demonstrates how to create owner-drawn ListBox items. 该代码使用DrawMode属性指定绘制的项固定大小并DrawItem要执行到每个项的绘制事件ListBoxThe code uses the DrawMode property to specify that the items drawn are fixed sized and the DrawItem event to perform the drawing of each item into the ListBox. 该示例代码使用的属性和方法的DrawItemEventArgs类作为参数传递给要绘制项的事件处理程序。The example code uses the properties and methods of the DrawItemEventArgs class passed as a parameter to the event handler to draw the items. 此示例假定ListBox控件名为listBox1已添加到窗体,DrawItem由示例代码中定义的事件处理程序处理事件。This example assumes that a ListBox control that is named listBox1 has been added to a form and that the DrawItem event is handled by the event handler defined in the example code. 该示例还假定已将项添加到ListBox包括按该顺序的"Apple"、"橙色"和"Plum"文本。The example also assumes that items have been added to the ListBox with the text of "Apple", "Orange", and "Plum" in that order.

private ListBox ListBox1 = new ListBox();
private void InitializeListBox()
{
    ListBox1.Items.AddRange(new Object[] 
        { "Red Item", "Orange Item", "Purple Item" });
    ListBox1.Location = new System.Drawing.Point(81, 69);
    ListBox1.Size = new System.Drawing.Size(120, 95);
    ListBox1.DrawMode = DrawMode.OwnerDrawFixed;
    ListBox1.DrawItem += new DrawItemEventHandler(ListBox1_DrawItem);
    Controls.Add(ListBox1);
}

private void ListBox1_DrawItem(object sender, 
    System.Windows.Forms.DrawItemEventArgs e)
{
    // Draw the background of the ListBox control for each item.
    e.DrawBackground();
    // Define the default color of the brush as black.
    Brush myBrush = Brushes.Black;

    // Determine the color of the brush to draw each item based 
    // on the index of the item to draw.
    switch (e.Index)
    {
        case 0:
            myBrush = Brushes.Red;
            break;
        case 1:
            myBrush = Brushes.Orange;
            break;
        case 2:
            myBrush = Brushes.Purple;
            break;
    }

    // Draw the current item text based on the current Font 
    // and the custom brush settings.
    e.Graphics.DrawString(ListBox1.Items[e.Index].ToString(), 
        e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
    // If the ListBox has focus, draw a focus rectangle around the selected item.
    e.DrawFocusRectangle();
}
Private WithEvents ListBox1 As New ListBox()

Private Sub InitializeListBox() 
    ListBox1.Items.AddRange(New Object() _
        {"Red Item", "Orange Item", "Purple Item"})
    ListBox1.Location = New System.Drawing.Point(81, 69)
    ListBox1.Size = New System.Drawing.Size(120, 95)
    ListBox1.DrawMode = DrawMode.OwnerDrawFixed
    Controls.Add(ListBox1)

End Sub

Private Sub ListBox1_DrawItem(ByVal sender As Object, _
 ByVal e As System.Windows.Forms.DrawItemEventArgs) _
 Handles ListBox1.DrawItem

    ' Draw the background of the ListBox control for each item.
    e.DrawBackground()

    ' Define the default color of the brush as black.
    Dim myBrush As Brush = Brushes.Black

    ' Determine the color of the brush to draw each item based on   
    ' the index of the item to draw.
    Select Case e.Index
        Case 0
            myBrush = Brushes.Red
        Case 1
            myBrush = Brushes.Orange
        Case 2
            myBrush = Brushes.Purple
    End Select

    ' Draw the current item text based on the current 
    ' Font and the custom brush settings.
    e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(), _
        e.Font, myBrush, e.Bounds, StringFormat.GenericDefault)

    ' If the ListBox has focus, draw a focus rectangle around  _ 
    ' the selected item.
    e.DrawFocusRectangle()
End Sub

注解

DrawItem事件引发由所有者描述控件,如ListBoxComboBox控件。The DrawItem event is raised by owner draw controls, such as the ListBox and ComboBox controls. 它包含要绘制的指定的项,包括项索引的用户所需的所有信息Rectangle,和Graphics上完成绘图。It contains all the information that is needed for the user to paint the specified item, including the item index, the Rectangle, and the Graphics on which the drawing should be done.

构造函数

DrawItemEventArgs(Graphics, Font, Rectangle, Int32, DrawItemState) DrawItemEventArgs(Graphics, Font, Rectangle, Int32, DrawItemState) DrawItemEventArgs(Graphics, Font, Rectangle, Int32, DrawItemState) DrawItemEventArgs(Graphics, Font, Rectangle, Int32, DrawItemState)

为带有指定的字体、状态、绘制表面和绘制边界的指定控件初始化 DrawItemEventArgs 类的新实例。Initializes a new instance of the DrawItemEventArgs class for the specified control with the specified font, state, surface to draw on, and the bounds to draw within.

DrawItemEventArgs(Graphics, Font, Rectangle, Int32, DrawItemState, Color, Color) DrawItemEventArgs(Graphics, Font, Rectangle, Int32, DrawItemState, Color, Color) DrawItemEventArgs(Graphics, Font, Rectangle, Int32, DrawItemState, Color, Color) DrawItemEventArgs(Graphics, Font, Rectangle, Int32, DrawItemState, Color, Color)

为带有指定的字体、状态、前景色、背景色、绘制表面和绘制边界的指定控件初始化 DrawItemEventArgs 类的新实例。Initializes a new instance of the DrawItemEventArgs class for the specified control with the specified font, state, foreground color, background color, surface to draw on, and the bounds to draw within.

属性

BackColor BackColor BackColor BackColor

获取所绘制的项的背景色。Gets the background color of the item that is being drawn.

Bounds Bounds Bounds Bounds

获取表示所绘制项的边界的矩形。Gets the rectangle that represents the bounds of the item that is being drawn.

Font Font Font Font

获取分配给所绘制项的字体。Gets the font that is assigned to the item being drawn.

ForeColor ForeColor ForeColor ForeColor

获取所绘制项的前景色。Gets the foreground color of the of the item being drawn.

Graphics Graphics Graphics Graphics

获取要在其上绘制项的图形表面。Gets the graphics surface to draw the item on.

Index Index Index Index

获取所绘制项的索引值。Gets the index value of the item that is being drawn.

State State State State

获取所绘制项的状态。Gets the state of the item being drawn.

方法

DrawBackground() DrawBackground() DrawBackground() DrawBackground()

DrawItemEventArgs 构造函数指定的边界范围内用适当的颜色绘制背景。Draws the background within the bounds specified in the DrawItemEventArgs constructor and with the appropriate color.

DrawFocusRectangle() DrawFocusRectangle() DrawFocusRectangle() DrawFocusRectangle()

DrawItemEventArgs 构造函数指定的边界范围内绘制聚焦框。Draws a focus rectangle within the bounds specified in the DrawItemEventArgs constructor.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

适用于

另请参阅