ListView.DrawItem イベントとは? わかりやすく解説

ListView.DrawItem イベント

メモ : このイベントは、.NET Framework version 2.0 で新しく追加されたものです。

ListView が描画され、OwnerDraw プロパティが true に設定されているときに発生します。

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

Public Event DrawItem As
 DrawListViewItemEventHandler
Dim instance As ListView
Dim handler As DrawListViewItemEventHandler

AddHandler instance.DrawItem, handler
public event DrawListViewItemEventHandler DrawItem
public:
event DrawListViewItemEventHandler^ DrawItem {
    void add (DrawListViewItemEventHandler^ value);
    void remove (DrawListViewItemEventHandler^ value);
}
/** @event */
public void add_DrawItem (DrawListViewItemEventHandler
 value)

/** @event */
public void remove_DrawItem (DrawListViewItemEventHandler
 value)
JScript では、イベントは使用できますが、新規に宣言することはできません。
解説解説

このイベントを利用すると、オーナー描画を使用して ListView コントロールの外観をカスタマイズできます。これは、OwnerDraw プロパティが true に設定されている場合にだけ発生します。オーナー描画の詳細については、OwnerDraw プロパティのリファレンス トピックを参照してください。

DrawItem イベントは、各ListView 項目に対して発生します。View プロパティが View.Details に設定されている場合は、DrawSubItem イベントと DrawColumnHeader イベントも発生します。この場合、DrawItem イベントを処理して、すべての項目に共通の要素 (背景など) を描画し、DrawSubItem イベントを処理して、個々のサブ項目 (テキスト値など) の要素を描画できます。また、利便性は低下しますが、この 2 つのイベントのどちらか 1 つだけを使用して、ListView コントロール内のすべての項目を描画することもできます。詳細ビューに列ヘッダーを描画するには、DrawColumnHeader イベントを処理する必要があります。

メモメモ

基になる Win32 コントロールのバグにより、行上にマウス ポインタを移動したときに DrawItem イベントが発生しますが、詳細ビューの各行について DrawSubItem イベントは発生しません。そのため、DrawSubItem イベント ハンドラで描画される内容は、DrawItem イベント ハンドラで描画されるカスタムの背景で上書きされてしまいます。追加イベントの発生時に各行の処理を無効にする代替手段については、OwnerDraw のリファレンス トピックを参照してください。別の代替手段として、すべてのカスタム描画コードを DrawSubItem イベント ハンドラに配置し、DrawListViewSubItemEventArgs.ColumnIndex 値が 0 の場合にだけ、(サブ項目を含む) 項目全体の背景を描画する方法もあります。

イベント処理の詳細については、「イベントの利用」を参照してください。

使用例使用例

DrawItem イベント ハンドラの実装を提供するコード例を次に示します。詳細については、OwnerDraw のリファレンス トピックを参照してください。

' Draws the backgrounds for entire ListView items.
Private Sub listView1_DrawItem(ByVal
 sender As Object, _
    ByVal e As DrawListViewItemEventArgs) _
    Handles listView1.DrawItem

    If Not (e.State And
 ListViewItemStates.Selected) = 0 Then

        ' Draw the background for a selected item.
        e.Graphics.FillRectangle(Brushes.Maroon, e.Bounds)
        e.DrawFocusRectangle()

    Else

        ' Draw the background for an unselected item.
        Dim brush As New
 LinearGradientBrush(e.Bounds, Color.Orange, _
            Color.Maroon, LinearGradientMode.Horizontal)
        Try
            e.Graphics.FillRectangle(brush, e.Bounds)
        Finally
            brush.Dispose()
        End Try

    End If

    ' Draw the item text for views other than the Details view.
    If Not Me.listView1.View
 = View.Details Then
        e.DrawText()
    End If

End Sub
// Draws the backgrounds for entire ListView items.
private void listView1_DrawItem(object sender
,
    DrawListViewItemEventArgs e)
{
    if ((e.State & ListViewItemStates.Selected) != 0)
    {
        // Draw the background and focus rectangle for a selected item.
        e.Graphics.FillRectangle(Brushes.Maroon, e.Bounds);
        e.DrawFocusRectangle();
    }
    else
    {
        // Draw the background for an unselected item.
        using (LinearGradientBrush brush =
            new LinearGradientBrush(e.Bounds, Color.Orange,
            Color.Maroon, LinearGradientMode.Horizontal))
        {
            e.Graphics.FillRectangle(brush, e.Bounds);
        }
    }

    // Draw the item text for views other than the Details view.
    if (listView1.View != View.Details)
    {
        e.DrawText();
    }
}
   // Draws the backgrounds for entire ListView items.
private:
   void listView1_DrawItem( Object^ /*sender*/, DrawListViewItemEventArgs^
 e )
   {
      if ( (e->State & ListViewItemStates::Selected) !=
 (ListViewItemStates)0 )
      {
         // Draw the background for a selected item.
         e->Graphics->FillRectangle( Brushes::Maroon, e->Bounds );
         e->DrawFocusRectangle();
      }
      else
      {
         // Draw the background for an unselected item.
         LinearGradientBrush^ myBrush = gcnew LinearGradientBrush( e->Bounds,Color::Orange,Color::Maroon,LinearGradientMode::Horizontal
 );
         try
         {
            e->Graphics->FillRectangle( myBrush, e->Bounds );
         }
         finally
         {
            if ( myBrush )
               delete (IDisposable^)myBrush;
         }
      }

      if ( listView1->View != View::Details )
      {
         e->DrawText();
      }
   }
// Draws the backgrounds for the column header row and for entire
// ListView items.
private void myListView_DrawItem(Object sender,
 DrawListViewItemEventArgs e)
{
    // Draw the background for the column header row.
    if (e.get_ItemIndex() == -1) {
        e.get_Item().set_BackColor(Color.get_Black());
        e.DrawBackground();
    }
    // Draw the background for a selected item.
    else {
        if (Convert.ToInt32((e.get_State() & ListViewItemStates.Selected))
            != 0) {
            e.get_Graphics().FillRectangle(Brushes.get_Maroon(),
                e.get_Bounds());
            e.DrawFocusRectangle();
        }
        // Draw the background for an unselected item.
        else {
            LinearGradientBrush myBrush = new LinearGradientBrush(
                e.get_Bounds(), Color.get_Orange(), Color.get_Maroon(),
                LinearGradientMode.Horizontal);
            try {
                e.get_Graphics().FillRectangle(myBrush, e.get_Bounds());
            }
            finally {
                myBrush.Dispose();
            }    
        }
    }
    // Draw the item text for views other than the Details view.
    if (!(((ListView)sender).get_View().Equals(View.Details)))
 {
        e.DrawText();
    }
} //myListView_DrawItem
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「ListView.DrawItem イベント」の関連用語

ListView.DrawItem イベントのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



ListView.DrawItem イベントのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS