DataGridViewRowPrePaintEventArgs.DrawFocus(Rectangle, Boolean) 메서드


지정된 범위 주위에 포커스 영역을 그립니다.Draws the focus rectangle around the specified bounds.

 void DrawFocus(System::Drawing::Rectangle bounds, bool cellsPaintSelectionBackground);
public void DrawFocus (System.Drawing.Rectangle bounds, bool cellsPaintSelectionBackground);
member this.DrawFocus : System.Drawing.Rectangle * bool -> unit
Public Sub DrawFocus (bounds As Rectangle, cellsPaintSelectionBackground As Boolean)

매개 변수


포커스 영역을 지정하는 Rectangle입니다.A Rectangle that specifies the focus area.


SelectionBackColor 속성의 InheritedStyle 속성을 사용하여 포커스 영역의 색을 확인하려면 true이고, BackColorInheritedStyle 속성을 사용하려면 false입니다.true to use the SelectionBackColor property of the InheritedStyle property to determine the color of the focus rectangle; false to use the BackColor property of the InheritedStyle.


RowIndex가 0보다 작거나 DataGridView 컨트롤의 행 수에서 1을 뺀 값보다 큰 경우RowIndex is less than zero or greater than the number of rows in the DataGridView control minus one.


다음 코드 예제를 사용 하는 방법에 설명 합니다 DrawFocus 중점을 두고 표시 되는 행의 셀 주위 그릴 메서드.The following code example demonstrates how to use the DrawFocus method to draw a focus around the visible cells of the row. 코드를 사용 하 여 실제로 DataGridViewRowPostPaintEventArgs.DrawFocus 메서드를 거의 동일 합니다 DrawFocus 메서드의 DataGridViewRowPrePaintEventArgs.The code actually uses the DataGridViewRowPostPaintEventArgs.DrawFocus method, which is nearly identical to the DrawFocus method of DataGridViewRowPrePaintEventArgs. 이 예제는에서 사용할 수 있는 보다 큰 예제의 일부 방법: Windows Forms DataGridView 컨트롤에서 행 모양 사용자 지정합니다.This example is part of a larger example available in How to: Customize the Appearance of Rows in the Windows Forms DataGridView Control.

// Paints the content that spans multiple columns and the focus rectangle.
void dataGridView1_RowPostPaint(object sender,
    DataGridViewRowPostPaintEventArgs e)
    // Calculate the bounds of the row.
    Rectangle rowBounds = new Rectangle(
        this.dataGridView1.RowHeadersWidth, e.RowBounds.Top,
            DataGridViewElementStates.Visible) -
        this.dataGridView1.HorizontalScrollingOffset + 1,

    SolidBrush forebrush = null;
        // Determine the foreground color.
        if ((e.State & DataGridViewElementStates.Selected) ==
            forebrush = new SolidBrush(e.InheritedRowStyle.SelectionForeColor);
            forebrush = new SolidBrush(e.InheritedRowStyle.ForeColor);

        // Get the content that spans multiple columns.
        object recipe =

        if (recipe != null)
            String text = recipe.ToString();

            // Calculate the bounds for the content that spans multiple 
            // columns, adjusting for the horizontal scrolling position 
            // and the current row height, and displaying only whole
            // lines of text.
            Rectangle textArea = rowBounds;
            textArea.X -= this.dataGridView1.HorizontalScrollingOffset;
            textArea.Width += this.dataGridView1.HorizontalScrollingOffset;
            textArea.Y += rowBounds.Height - e.InheritedRowStyle.Padding.Bottom;
            textArea.Height -= rowBounds.Height -
            textArea.Height = (textArea.Height / e.InheritedRowStyle.Font.Height) *

            // Calculate the portion of the text area that needs painting.
            RectangleF clip = textArea;
            clip.Width -= this.dataGridView1.RowHeadersWidth + 1 - clip.X;
            clip.X = this.dataGridView1.RowHeadersWidth + 1;
            RectangleF oldClip = e.Graphics.ClipBounds;

            // Draw the content that spans multiple columns.
                text, e.InheritedRowStyle.Font, forebrush, textArea);


    if (this.dataGridView1.CurrentCellAddress.Y == e.RowIndex)
        // Paint the focus rectangle.
        e.DrawFocus(rowBounds, true);
' Paints the content that spans multiple columns and the focus rectangle.
Sub dataGridView1_RowPostPaint(ByVal sender As Object, _
    ByVal e As DataGridViewRowPostPaintEventArgs) _
    Handles dataGridView1.RowPostPaint

    ' 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)

    Dim forebrush As SolidBrush = Nothing
        ' Determine the foreground color.
        If (e.State And DataGridViewElementStates.Selected) = _
            DataGridViewElementStates.Selected Then

            forebrush = New SolidBrush(e.InheritedRowStyle.SelectionForeColor)
            forebrush = New SolidBrush(e.InheritedRowStyle.ForeColor)
        End If

        ' Get the content that spans multiple columns.
        Dim recipe As Object = _

        If (recipe IsNot Nothing) Then
            Dim text As String = recipe.ToString()

            ' Calculate the bounds for the content that spans multiple 
            ' columns, adjusting for the horizontal scrolling position 
            ' and the current row height, and displaying only whole
            ' lines of text.
            Dim textArea As Rectangle = rowBounds
            textArea.X -= Me.dataGridView1.HorizontalScrollingOffset
            textArea.Width += Me.dataGridView1.HorizontalScrollingOffset
            textArea.Y += rowBounds.Height - e.InheritedRowStyle.Padding.Bottom
            textArea.Height -= rowBounds.Height - e.InheritedRowStyle.Padding.Bottom
            textArea.Height = (textArea.Height \ e.InheritedRowStyle.Font.Height) * _

            ' Calculate the portion of the text area that needs painting.
            Dim clip As RectangleF = textArea
            clip.Width -= Me.dataGridView1.RowHeadersWidth + 1 - clip.X
            clip.X = Me.dataGridView1.RowHeadersWidth + 1
            Dim oldClip As RectangleF = e.Graphics.ClipBounds

            ' Draw the content that spans multiple columns.
            e.Graphics.DrawString(text, e.InheritedRowStyle.Font, forebrush, _

        End If
    End Try

    If Me.dataGridView1.CurrentCellAddress.Y = e.RowIndex Then
        ' Paint the focus rectangle.
        e.DrawFocus(rowBounds, True)
    End If

End Sub


사용 된 DrawFocus 메서드를 그릴 때를 DataGridViewRow 해당 셀의 내용을 직접 및 합니다.Use the DrawFocus method when you draw the DataGridViewRow and its cells' contents yourself. 수동으로 전체 행과 해당 셀의 내용을 그리는 설정 합니다 HandledEventArgs.Handled 속성을 true입니다.If you manually paint the entire row and its cells' contents, set the HandledEventArgs.Handled property to true. HandledEventArgs.Handled 됩니다 trueCellPaintingRowPostPaint 이벤트가 발생 하지 않습니다.When HandledEventArgs.Handled is true, the CellPainting and RowPostPaint events do not occur.

적용 대상

추가 정보