DrawMode 枚举

定义

指定如何绘制控件的元素。

public enum class DrawMode
public enum DrawMode
type DrawMode = 
Public Enum DrawMode
继承
DrawMode

字段

Normal 0

控件中的所有元素都由操作系统绘制,并且元素大小都相等。

OwnerDrawFixed 1

控件中的所有元素都是手动绘制的,并且元素大小都相等。

OwnerDrawVariable 2

控件中的所有元素都由手动绘制,元素大小可能不相等。

示例

以下示例演示如何创建所有者绘制 ListBox 的项。 代码使用 ListBox.DrawMode 属性指定绘制的项大小固定,以及 ListBox.DrawItem 执行将每个项绘制到 中的 ListBox事件。 该示例使用作为参数传递给事件处理程序的 DrawItemEventArgs 类的属性和方法来绘制项。 此示例假定已将名为 的ListBoxlistBox1控件添加到窗体中,并且该ListBox.DrawItem事件由示例中定义的事件处理程序处理。 该示例还假定已按该顺序将 ListBox “Apple”、“Orange”和“Plum”文本添加到 中。

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

注解

此枚举由 、、 和 ComboBox 类中的CheckedListBoxListBox成员DrawMode使用。

可以替代某些控件或某些元素的绘图。 此枚举用于指定控件是由操作系统绘制的,还是你自己的代码是否处理控件的绘制。

注意

CheckedListBox 仅支持 Normal;将忽略所有者绘制模式。

有关使用 DrawMode 枚举的详细信息,请参阅 MeasureItemDrawItem 事件和 ItemHeight 属性。

适用于

另请参阅