DrawItemEventArgs 類別

定義

提供 DrawItem 事件的資料。

public ref class DrawItemEventArgs : EventArgs
public ref class DrawItemEventArgs : EventArgs, IDisposable, System::Drawing::IDeviceContext
public class DrawItemEventArgs : EventArgs
public class DrawItemEventArgs : EventArgs, IDisposable, System.Drawing.IDeviceContext
type DrawItemEventArgs = class
    inherit EventArgs
type DrawItemEventArgs = class
    inherit EventArgs
    interface IDisposable
    interface IDeviceContext
Public Class DrawItemEventArgs
Inherits EventArgs
Public Class DrawItemEventArgs
Inherits EventArgs
Implements IDeviceContext, IDisposable
繼承
DrawItemEventArgs
衍生
實作

範例

下列範例示範如何建立擁有者繪製 ListBox 的專案。 程式碼會 DrawMode 使用 屬性來指定繪製的專案會固定大小,而 DrawItem 事件則會執行每個專案的繪圖。 ListBox 此範例程式碼會使用傳遞為事件處理常式參數的 DrawItemEventArgs 類別屬性和方法,以繪製專案。 本範例假設 ListBox 名為 listBox1 的控制項已新增至表單,而且 DrawItem 事件是由範例程式碼中所定義的事件處理常式所處理。 此範例也假設專案已以該順序的文字 「Apple」、「Orange」 和 「Plum」 新增至 ListBox

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 控制項。 它包含使用者繪製指定專案所需的所有資訊,包括專案索引、 Rectangle 應完成繪圖的 和 Graphics

建構函式

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

為具有指定字型、狀態、要繪製的介面和界線的指定控制項,初始化 DrawItemEventArgs 類別的新執行個體。

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

為具有指定字型、狀態、前景色彩、背景色彩、要繪製的介面和界線的指定控制項,初始化 DrawItemEventArgs 類別的新執行個體。

屬性

BackColor

取得所繪製項目的背景色彩。

Bounds

取得表示所繪製項目的界限的矩形。

Font

取得指派給所繪製項目的字型。

ForeColor

取得所繪製項目的前景色彩。

Graphics

取得項目繪製其上的圖形介面。

Index

取得所繪製項目的索引值。

State

取得所繪製項目的狀態。

方法

Dispose()

執行與釋放 (Free)、釋放 (Release) 或重設 Unmanaged 資源相關聯之應用程式定義的工作。

Dispose(Boolean)

提供 DrawItem 事件的資料。

DrawBackground()

DrawItemEventArgs 建構函式 (Constructor) 中指定的界限內繪製背景,並使用適當的色彩。

DrawFocusRectangle()

DrawItemEventArgs 建構函式中指定的界限內繪製焦點矩形 (Focus Rectangle)。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

IDeviceContext.GetHdc()

傳回 Windows 裝置內容的控制代碼。

IDeviceContext.ReleaseHdc()

釋放 Windows 裝置內容的控制代碼。

適用於

另請參閱