DrawListViewItemEventArgs.DrawText 方法

定義

使用 ListViewItem 目前的前景色彩和預設格式來繪製它的文字。

多載

DrawText()

使用 ListViewItem 目前的前景色彩來繪製它的文字。

DrawText(TextFormatFlags)

使用 ListViewItem 目前的前景色彩繪製它的文字,並利用指定的 TextFormatFlags 值設定格式。

DrawText()

使用 ListViewItem 目前的前景色彩來繪製它的文字。

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

範例

下列程式碼範例示範如何在提供控制項自訂繪圖 ListView 的應用程式中使用 DrawText 方法。 在此範例中 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

備註

使用這個方法,使用 和 ListViewItem.Font 屬性的目前值 ListViewItem.ForeColor 來繪製專案文字。 文字會繪製在 屬性所 Bounds 指定的區域內。 您也可以使用 Graphics 透過 屬性擷取的 Graphics 手動繪製文字。

當 屬性設定為 以外的 View.Details 值時 ListView.View ,此方法 DrawText 最有用。 在詳細資料檢視中,此方法會繪製 的 ListViewItem 文字,但不會繪製子專案的文字。 因此,在詳細資料檢視中,在事件處理常式 ListView.DrawSubItem 中繪製所有 ListView 文字通常比較方便。

另請參閱

適用於

DrawText(TextFormatFlags)

使用 ListViewItem 目前的前景色彩繪製它的文字,並利用指定的 TextFormatFlags 值設定格式。

public:
 void DrawText(System::Windows::Forms::TextFormatFlags flags);
public void DrawText (System.Windows.Forms.TextFormatFlags flags);
member this.DrawText : System.Windows.Forms.TextFormatFlags -> unit
Public Sub DrawText (flags As TextFormatFlags)

參數

flags
TextFormatFlags

TextFormatFlags 值的位元組合。

備註

使用這個方法,使用 和 ListViewItem.Font 屬性的目前值 ListViewItem.ForeColor 來繪製專案文字。 參數 TextFormatFlags 中指定的 flags 值可讓您提供節點標籤的格式屬性,例如文字對齊方式。 文字會繪製在 屬性所 Bounds 指定的區域內。 您也可以使用 Graphics 透過 屬性擷取的 Graphics 手動繪製文字。

當 屬性設定為 以外的 View.Details 值時 ListView.View ,此方法 DrawText 最有用。 在詳細資料檢視中,此方法會繪製 的 ListViewItem 文字,但不會繪製子專案的文字。 因此,在詳細資料檢視中,在事件處理常式 ListView.DrawSubItem 中繪製所有 ListView 文字通常比較方便。

另請參閱

適用於