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 プロパティとメソッドを使用して、項目を描画します。 この例では、 という名前listBox1ListBoxコントロールがフォーム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、 コントロールや ComboBox コントロールなどのListBox所有者描画コントロールによって発生します。 これには、ユーザーが指定したアイテムを描画するために必要なすべての情報が含まれます。これには、図面を実行する必要がある項目インデックス、 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()

アンマネージ リソースの解放またはリセットに関連付けられているアプリケーション定義のタスクを実行します。

Dispose(Boolean)

DrawItem イベントのデータを提供します。

DrawBackground()

DrawItemEventArgs コンストラクターで指定されている境界内に適切な色で背景を描画します。

DrawFocusRectangle()

DrawItemEventArgs コンストラクターで指定されている境界内に、フォーカスを示す四角形を描画します。

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

明示的なインターフェイスの実装

IDeviceContext.GetHdc()

Windows デバイス コンテキストを識別するハンドルを返します。

IDeviceContext.ReleaseHdc()

Windows デバイス コンテキストのハンドルを解放します。

適用対象

こちらもご覧ください