DataGridViewRowPrePaintEventArgs.DrawFocus(Rectangle, Boolean) DataGridViewRowPrePaintEventArgs.DrawFocus(Rectangle, Boolean) DataGridViewRowPrePaintEventArgs.DrawFocus(Rectangle, Boolean) DataGridViewRowPrePaintEventArgs.DrawFocus(Rectangle, Boolean) Method

定義

指定した境界の周囲にフォーカスを表す四角形を描画します。Draws the focus rectangle around the specified bounds.

public:
 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)

パラメーター

bounds
Rectangle Rectangle Rectangle Rectangle

フォーカス領域を指定する RectangleA Rectangle that specifies the focus area.

cellsPaintSelectionBackground
Boolean Boolean Boolean Boolean

SelectionBackColor プロパティの InheritedStyle プロパティを使用して、フォーカスを表す四角形の色を決定する場合は trueBackColorInheritedStyle プロパティを使用する場合は falsetrue 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 フォームの 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,
        this.dataGridView1.Columns.GetColumnsWidth(
            DataGridViewElementStates.Visible) -
        this.dataGridView1.HorizontalScrollingOffset + 1,
        e.RowBounds.Height);

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

        // Get the content that spans multiple columns.
        object recipe =
            this.dataGridView1.Rows.SharedRow(e.RowIndex).Cells[2].Value;

        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 -
                e.InheritedRowStyle.Padding.Bottom;
            textArea.Height = (textArea.Height / e.InheritedRowStyle.Font.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;
            e.Graphics.SetClip(clip);

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

            e.Graphics.SetClip(oldClip);
        }
    }
    finally
    {
        forebrush.Dispose();
    }

    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
    Try
        ' Determine the foreground color.
        If (e.State And DataGridViewElementStates.Selected) = _
            DataGridViewElementStates.Selected Then

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

        ' Get the content that spans multiple columns.
        Dim recipe As Object = _
            Me.dataGridView1.Rows.SharedRow(e.RowIndex).Cells(2).Value

        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) * _
                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
            e.Graphics.SetClip(clip)

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

            e.Graphics.SetClip(oldClip)
        End If
    Finally
        forebrush.Dispose()
    End Try

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

End Sub 'dataGridView1_RowPostPaint

注釈

使用して、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.HandledtrueCellPaintingRowPostPaintイベントは発生しません。When HandledEventArgs.Handled is true, the CellPainting and RowPostPaint events do not occur.

適用対象

こちらもご覧ください