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 的目前狀態。

範例

下列程式碼範例示範如何在應用程式中使用 State 屬性,以提供控制項的 ListView 自訂繪圖。 在此範例中 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 屬性擷取專案,並直接檢查其屬性。

適用於

另請參閱