DrawListViewItemEventArgs.State 属性

定义

获取要绘制的 ListViewItem 的当前状态。

public:
 property System::Windows::Forms::ListViewItemStates State { System::Windows::Forms::ListViewItemStates get(); };
public System.Windows.Forms.ListViewItemStates State { get; }
member this.State : System.Windows.Forms.ListViewItemStates
Public ReadOnly Property State As ListViewItemStates

属性值

ListViewItemStates 值的按位组合,指示 ListViewItem 的当前状态。

示例

下面的代码示例演示如何在为控件提供自定义绘图ListView的应用程序中使用 State 属性。 在此示例中,事件的处理程序 ListView.DrawItem 绘制整个项的背景。 在除详细信息视图以外的所有视图中,此处理程序还会绘制前景文本。 在详细信息视图中,将在 事件中 ListView.DrawSubItem 绘制前景文本。

有关完整示例,请参阅 DrawListViewItemEventArgs 概述参考主题。

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

注解

使用此属性检查要绘制的 是否ListViewItem处于特定状态。 此属性仅提供有关项的基本状态信息。 例如,可以使用此属性来确定是选择、选中还是聚焦项。 如果需要了解详细信息,请通过 属性检索项,Item并直接检查其属性。

适用于

另请参阅