DrawListViewItemEventArgs.DrawFocusRectangle 方法

定义

如果 ListViewItem 具有焦点,则为其绘制聚焦框。

public:
 void DrawFocusRectangle();
public void DrawFocusRectangle ();
member this.DrawFocusRectangle : unit -> unit
Public Sub DrawFocusRectangle ()

示例

下面的代码示例演示如何在为控件提供自定义绘图ListView的应用程序中使用 DrawFocusRectangle 方法。 在此示例中,事件的处理程序 ListView.DrawItem 绘制整个项的背景。 在除详细信息视图以外的所有视图中,此处理程序还会绘制前景文本。 在详细信息视图中,将在 事件中 ListView.DrawSubItem 绘制前景文本。

有关完整示例,请参阅 DrawListViewItemEventArgs 概述参考主题。

// Draws the backgrounds for entire ListView items.
private void listView1_DrawItem(object sender,
    DrawListViewItemEventArgs e)
{
    if ((e.State & ListViewItemStates.Selected) != 0)
    {
        // Draw the background and focus rectangle for a selected item.
        e.Graphics.FillRectangle(Brushes.Maroon, e.Bounds);
        e.DrawFocusRectangle();
    }
    else
    {
        // Draw the background for an unselected item.
        using (LinearGradientBrush brush =
            new LinearGradientBrush(e.Bounds, Color.Orange,
            Color.Maroon, LinearGradientMode.Horizontal))
        {
            e.Graphics.FillRectangle(brush, e.Bounds);
        }
    }

    // Draw the item text for views other than the Details view.
    if (listView1.View != View.Details)
    {
        e.DrawText();
    }
}
' Draws the backgrounds for entire ListView items.
Private Sub listView1_DrawItem(ByVal sender As Object, _
    ByVal e As DrawListViewItemEventArgs) _
    Handles listView1.DrawItem

    If Not (e.State And ListViewItemStates.Selected) = 0 Then

        ' Draw the background for a selected item.
        e.Graphics.FillRectangle(Brushes.Maroon, e.Bounds)
        e.DrawFocusRectangle()

    Else

        ' Draw the background for an unselected item.
        Dim brush As New LinearGradientBrush(e.Bounds, Color.Orange, _
            Color.Maroon, LinearGradientMode.Horizontal)
        Try
            e.Graphics.FillRectangle(brush, e.Bounds)
        Finally
            brush.Dispose()
        End Try

    End If

    ' Draw the item text for views other than the Details view.
    If Not Me.listView1.View = View.Details Then
        e.DrawText()
    End If

End Sub

注解

使用此方法在项周围绘制标准焦点矩形。 焦点矩形通常在 属性指定的 Bounds 区域内绘制。 但是,如果控件位于详细信息视图中,并且 ListView.FullRowSelect 属性值为 false,则围绕项第一列的默认文本区域绘制焦点矩形。

仅当项具有焦点时才绘制焦点矩形,因此在调用此方法之前,无需检查项的焦点状态。

适用于

另请参阅