DataGridViewRowPostPaintEventArgs.Graphics DataGridViewRowPostPaintEventArgs.Graphics DataGridViewRowPostPaintEventArgs.Graphics DataGridViewRowPostPaintEventArgs.Graphics Property

定义

获取用于绘制当前 GraphicsDataGridViewRowGets the Graphics used to paint the current DataGridViewRow.

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

属性值

用于绘制当前 GraphicsDataGridViewRowThe Graphics used to paint the current DataGridViewRow.

示例

下面的代码示例演示如何使用Graphics属性来绘制自定义背景。The following code example demonstrates how to use the Graphics property to paint a custom background. 尽管代码实际使用DataGridViewRowPrePaintEventArgs.Graphics属性, 但此属性与的Graphics属性DataGridViewRowPostPaintEventArgs几乎完全相同。Although the code actually uses the DataGridViewRowPrePaintEventArgs.Graphics property, this property is nearly identical to the Graphics property of DataGridViewRowPostPaintEventArgs. 变量e的类型DataGridViewRowPrePaintEventArgs为。The variable, e, is of type DataGridViewRowPrePaintEventArgs. 此代码示例摘自了如何:自定义 Windows 窗体 DataGridView 控件中的行的外观。This code example is part of a larger example provided in How to: Customize the Appearance of Rows in the Windows Forms 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类提供适用于自定义绘图的属性和方法。The Graphics class provides properties and methods that are useful for custom drawing.

适用于

另请参阅