DataGridViewRowPrePaintEventArgs.DrawFocus(Rectangle, Boolean) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
围绕指定的边界绘制聚焦框。
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)
参数
- cellsPaintSelectionBackground
- Boolean
若为 true
,则使用 SelectionBackColor 属性的 InheritedStyle 属性确定聚焦框的颜色;若为 false
,则使用 BackColor 的 InheritedStyle 属性。
例外
RowIndex 小于零或大于将 DataGridView 控件中的行数减一得到的值。
示例
下面的代码示例演示如何使用 DrawFocus 方法在行的可见单元格周围绘制焦点。 代码实际上使用 DataGridViewRowPostPaintEventArgs.DrawFocus 方法,该方法几乎与 的 DataGridViewRowPrePaintEventArgs方法相同DrawFocus。 此示例是如何:自定义 DataGridView 控件中行的外观中提供的更大示例的 Windows 窗体一部分。
// 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
注解
DrawFocus自行绘制 DataGridViewRow 及其单元格的内容时,请使用 方法。 如果手动绘制整行及其单元格的内容,请将 HandledEventArgs.Handled 属性设置为 true
。 当 为 true
时HandledEventArgs.Handled,CellPainting不会发生 和 RowPostPaint 事件。