DataGridViewRowPostPaintEventArgs.Graphics 属性

定义

获取用于绘制当前 GraphicsDataGridViewRow

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

属性值

Graphics

用于绘制当前 GraphicsDataGridViewRow

示例

下面的代码示例演示如何使用 Graphics 属性绘制自定义背景。 虽然代码实际使用该 DataGridViewRowPrePaintEventArgs.Graphics 属性,但此属性几乎与 Graphics 属性 DataGridViewRowPostPaintEventArgs相同。 变量 e的类型 DataGridViewRowPrePaintEventArgs。 此代码示例是 How to: Customize the Appearance of Rows in the Windows 窗体 DataGridView Control 中提供的较大示例的一部分。

// Determine whether the cell should be painted
// with the custom selection background.
if ((e.State & DataGridViewElementStates.Selected) ==
            DataGridViewElementStates.Selected)
{
    // Calculate the bounds of the row.
    Rectangle rowBounds = new Rectangle(
        this.dataGridView1.RowHeadersWidth, e.RowBounds.Top,
        this.dataGridView1.Columns.GetColumnsWidth(
            DataGridViewElementStates.Visible) -
        this.dataGridView1.HorizontalScrollingOffset + 1,
        e.RowBounds.Height);

    // Paint the custom selection background.
    using (Brush backbrush =
        new System.Drawing.Drawing2D.LinearGradientBrush(rowBounds,
            this.dataGridView1.DefaultCellStyle.SelectionBackColor,
            e.InheritedRowStyle.ForeColor,
            System.Drawing.Drawing2D.LinearGradientMode.Horizontal))
    {
        e.Graphics.FillRectangle(backbrush, rowBounds);
    }
}
' Determine whether the cell should be painted with the 
' custom selection background.
If (e.State And DataGridViewElementStates.Selected) = _
    DataGridViewElementStates.Selected Then

    ' Calculate the bounds of the row.
    Dim rowBounds As New Rectangle( _
        Me.dataGridView1.RowHeadersWidth, e.RowBounds.Top, _
        Me.dataGridView1.Columns.GetColumnsWidth( _
        DataGridViewElementStates.Visible) - _
        Me.dataGridView1.HorizontalScrollingOffset + 1, _
        e.RowBounds.Height)

    ' Paint the custom selection background.
    Dim backbrush As New _
        System.Drawing.Drawing2D.LinearGradientBrush(rowBounds, _
        Me.dataGridView1.DefaultCellStyle.SelectionBackColor, _
        e.InheritedRowStyle.ForeColor, _
        System.Drawing.Drawing2D.LinearGradientMode.Horizontal)
    Try
        e.Graphics.FillRectangle(backbrush, rowBounds)
    Finally
        backbrush.Dispose()
    End Try
End If

注解

Graphics 类提供可用于自定义绘图的属性和方法。

适用于

另请参阅