DrawMode 枚举

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

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Enumeration DrawMode
用法
Dim instance As DrawMode
public enum DrawMode
public enum class DrawMode
public enum DrawMode
public enum DrawMode

成员

  成员名称 说明
Normal 控件中的所有元素都由操作系统绘制,并且元素大小都相等。 
OwnerDrawFixed 控件中的所有元素都是手动绘制的,并且元素大小都相等。 
OwnerDrawVariable 控件中的所有元素都由手动绘制,元素大小可能不相等。 

备注

此枚举由 ListBoxCheckedListBoxComboBox 类中的 DrawMode 等成员使用。

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

提示

CheckedListBox 类仅支持 DrawMode.Normal;所有者描述模式被忽略。

有关使用 DrawMode 枚举的更多信息,请参见 MeasureItemDrawItem 事件以及 ItemHeight 属性。

示例

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

Private Sub listBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
   ' Set the DrawMode property to draw fixed sized items.
   ListBox1.DrawMode = DrawMode.OwnerDrawFixed
   ' 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

   ' 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), e.Font, myBrush, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
   ' If the ListBox has focus, draw a focus rectangle around the selected item.
   e.DrawFocusRectangle()
End Sub
private void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
   // Set the DrawMode property to draw fixed sized items.
   listBox1.DrawMode = DrawMode.OwnerDrawFixed;
   // 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:
   void listBox1_DrawItem( Object^ /*sender*/, System::Windows::Forms::DrawItemEventArgs^ e )
   {
      // Set the DrawMode property to draw fixed sized items.
      listBox1->DrawMode = DrawMode::OwnerDrawFixed;

      // 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 void listBox1_DrawItem(Object sender,
    System.Windows.Forms.DrawItemEventArgs e)
{
    // Set the DrawMode property to draw fixed sized items.
    listBox1.set_DrawMode(DrawMode.OwnerDrawFixed);
    // Draw the background of the ListBox control for each item.
    e.DrawBackground();
    // Create a new Brush and initialize to a Black colored brush
    // by default.
    Brush myBrush = Brushes.get_Black();
    // Determine the color of the brush to draw each item based on the
    // index of the item to draw.
    switch (e.get_Index()) {
        case 0 :
            myBrush = Brushes.get_Red();
            break;
        case 1 :
            myBrush = Brushes.get_Orange();
            break;
        case 2 :
            myBrush = Brushes.get_Purple();
            break;
    }

    // Draw the current item text based on the current Font and the custom
    // brush settings.
    e.get_Graphics().DrawString(System.Convert.ToString(listBox1.
        get_Items().get_Item(e.get_Index())), e.get_Font(), myBrush,
        RectangleF.op_Implicit(e.get_Bounds()), StringFormat.
        get_GenericDefault());
    // If the ListBox has focus, draw a focus rectangle around the selected
    // item.
    e.DrawFocusRectangle();
} //listBox1_DrawItem

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

System.Windows.Forms 命名空间
ListBox
CheckedListBox 类
ComboBox 类