共用方式為


DrawListViewItemEventArgs.Graphics 屬性

定義

取得用來繪製 GraphicsListViewItem

public:
 property System::Drawing::Graphics ^ Graphics { System::Drawing::Graphics ^ get(); };
public System.Drawing.Graphics Graphics { get; }
member this.Graphics : System.Drawing.Graphics
Public ReadOnly Property Graphics As Graphics

屬性值

用來繪製 GraphicsListViewItem

範例

下列程式碼範例示範如何在應用程式中使用 Graphics 屬性,以提供控制項的自訂繪圖 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

備註

類別 Graphics 提供在顯示裝置上繪製的實用屬性和方法。

適用於

另請參閱