DrawMode 枚举

定义

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

C#
public enum DrawMode
继承
DrawMode

字段

名称 说明
Normal 0

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

OwnerDrawFixed 1

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

OwnerDrawVariable 2

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

示例

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

C#
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();
}

注解

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

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

备注

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

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

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅