ListView.DrawItem 이벤트

정의

ListView가 그려지고 OwnerDraw 속성이 true로 설정될 때 발생합니다.

public:
 event System::Windows::Forms::DrawListViewItemEventHandler ^ DrawItem;
public event System.Windows.Forms.DrawListViewItemEventHandler DrawItem;
public event System.Windows.Forms.DrawListViewItemEventHandler? DrawItem;
member this.DrawItem : System.Windows.Forms.DrawListViewItemEventHandler 
Public Custom Event DrawItem As DrawListViewItemEventHandler 

이벤트 유형

예제

다음 코드 예제에서는 이벤트 처리기의 구현을 DrawItem 제공합니다. 전체 예제를 참조 하세요.를 OwnerDraw 참조 항목입니다.

// 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 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

설명

이 이벤트의 모양을 사용자 지정할 수 있습니다는 ListView 소유자 그리기를 사용 하 여 제어 합니다. 속성이 로 설정된 경우에만 OwnerDraw 발생합니다 true. 소유자 그리기에 대한 자세한 내용은 속성 참조 항목을 참조 OwnerDraw 하세요.

이벤트는 DrawItemListView 항목에 대해 발생할 수 있습니다. 속성이 ViewDrawSubItem 설정View.Details되면 및 DrawColumnHeader 이벤트도 발생합니다. 이 경우 이벤트를 처리 DrawItem 하여 배경과 같은 모든 항목에 공통적인 요소를 그리고 이벤트를 처리 DrawSubItem 하여 텍스트 값과 같은 개별 하위 항목에 대한 요소를 그릴 수 있습니다. 두 이벤트 중 하나만 사용하여 컨트롤의 ListView 모든 항목을 그릴 수도 있지만, 이 작업은 덜 편리할 수 있습니다. 세부 정보 보기에서 열 머리글을 그리려면 이벤트를 처리 DrawColumnHeader 해야 합니다.

참고

기본 Win32 컨트롤 DrawItem 의 버그로 인해 마우스 포인터가 행 위로 이동할 때 세부 정보 보기에서 행당 한 번 이벤트를 동반 DrawSubItem 하지 않고 이벤트가 발생하므로 이벤트 처리기에 그려진 DrawSubItem 모든 항목이 이벤트 처리기에 그려진 사용자 지정 배경에 DrawItem 의해 그려집니다. 추가 이벤트가 발생할 때 각 행을 무효화하는 해결 방법은 참조 항목의 예제 OwnerDraw 를 참조하세요. 다른 해결 방법은 모든 사용자 지정 그리기 코드를 DrawSubItem 이벤트 처리기에 배치하고 값이 0인 경우에만 전체 항목(하위 항목 포함)에 DrawListViewSubItemEventArgs.ColumnIndex 대한 배경을 그리는 것입니다.

이벤트 처리에 대한 자세한 내용은 이벤트 처리 및 발생 을 참조하십시오.

적용 대상

추가 정보