DrawListViewSubItemEventArgs.DrawText 方法        
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
使用 ListViewItem.ListViewSubItem 的当前前景色和默认格式设置绘制它的文本。
重载
| DrawText() | 
						 使用 ListViewItem.ListViewSubItem 的当前前景色绘制它的文本。  | 
        	
| DrawText(TextFormatFlags) | 
						 使用 ListViewItem.ListViewSubItem 的当前前景色以及由指定的 TextFormatFlags 值设置的格式绘制它的文本。  | 
        	
DrawText()
使用 ListViewItem.ListViewSubItem 的当前前景色绘制它的文本。
public:
 void DrawText();
	public void DrawText();
	member this.DrawText : unit -> unit
	Public Sub DrawText ()
    	注解
使用此方法使用 和 ListViewItem.ListViewSubItem.Font 属性的ListViewItem.ListViewSubItem.ForeColor当前值绘制子项文本。 文本在 属性指定的 Bounds 区域内绘制。
注意
              UseItemStyleForSubItems父ListViewItem项的 属性必须设置为 ,false以防止ListViewItem.ForeColor父项的 和 ListViewItem.Font 值重写子项值。
另请参阅
适用于
DrawText(TextFormatFlags)
使用 ListViewItem.ListViewSubItem 的当前前景色以及由指定的 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 值的按位组合。
示例
下面的代码示例演示如何在为控件提供自定义绘图ListView的应用程序中使用 DrawText 方法。 在此示例中,事件的处理程序 ListView.DrawSubItem 绘制子项文本值,以及具有负值的子项的文本和背景。
有关完整示例,请参阅 DrawListViewSubItemEventArgs 概述参考主题。
// Draws subitem text and applies content-based formatting.
private void listView1_DrawSubItem(object sender,
    DrawListViewSubItemEventArgs e)
{
    TextFormatFlags flags = TextFormatFlags.Left;
    using (StringFormat sf = new StringFormat())
    {
        // Store the column text alignment, letting it default
        // to Left if it has not been set to Center or Right.
        switch (e.Header.TextAlign)
        {
            case HorizontalAlignment.Center:
                sf.Alignment = StringAlignment.Center;
                flags = TextFormatFlags.HorizontalCenter;
                break;
            case HorizontalAlignment.Right:
                sf.Alignment = StringAlignment.Far;
                flags = TextFormatFlags.Right;
                break;
        }
        // Draw the text and background for a subitem with a 
        // negative value. 
        double subItemValue;
        if (e.ColumnIndex > 0 && Double.TryParse(
            e.SubItem.Text, NumberStyles.Currency,
            NumberFormatInfo.CurrentInfo, out subItemValue) &&
            subItemValue < 0)
        {
            // Unless the item is selected, draw the standard 
            // background to make it stand out from the gradient.
            if ((e.ItemState & ListViewItemStates.Selected) == 0)
            {
                e.DrawBackground();
            }
            // Draw the subitem text in red to highlight it. 
            e.Graphics.DrawString(e.SubItem.Text,
                listView1.Font, Brushes.Red, e.Bounds, sf);
            return;
        }
        // Draw normal text for a subitem with a nonnegative 
        // or nonnumerical value.
        e.DrawText(flags);
    }
}
' Draws subitem text and applies content-based formatting.
Private Sub listView1_DrawSubItem(ByVal sender As Object, _
    ByVal e As DrawListViewSubItemEventArgs) _
    Handles listView1.DrawSubItem
    Dim flags As TextFormatFlags = TextFormatFlags.Left
    Dim sf As New StringFormat()
    Try
        ' Store the column text alignment, letting it default
        ' to Left if it has not been set to Center or Right.
        Select Case e.Header.TextAlign
            Case HorizontalAlignment.Center
                sf.Alignment = StringAlignment.Center
                flags = TextFormatFlags.HorizontalCenter
            Case HorizontalAlignment.Right
                sf.Alignment = StringAlignment.Far
                flags = TextFormatFlags.Right
        End Select
        ' Draw the text and background for a subitem with a 
        ' negative value. 
        Dim subItemValue As Double
        If e.ColumnIndex > 0 AndAlso _
            Double.TryParse(e.SubItem.Text, NumberStyles.Currency, _
            NumberFormatInfo.CurrentInfo, subItemValue) AndAlso _
            subItemValue < 0 Then
            ' Unless the item is selected, draw the standard 
            ' background to make it stand out from the gradient.
            If (e.ItemState And ListViewItemStates.Selected) = 0 Then
                e.DrawBackground()
            End If
            ' Draw the subitem text in red to highlight it. 
            e.Graphics.DrawString(e.SubItem.Text, _
                Me.listView1.Font, Brushes.Red, e.Bounds, sf)
            Return
        End If
        ' Draw normal text for a subitem with a nonnegative 
        ' or nonnumerical value.
        e.DrawText(flags)
    Finally
        sf.Dispose()
    End Try
End Sub
    	注解
使用此方法使用 和 ListViewItem.ListViewSubItem.Font 属性的ListViewItem.ListViewSubItem.ForeColor当前值绘制子项文本。 文本在 属性指定的 Bounds 区域内绘制。 参数 TextFormatFlags 中指定的 flags 值允许你为节点标签提供格式属性,例如文本对齐方式。
注意
              UseItemStyleForSubItems父ListViewItem项的 属性必须设置为 ,false以防止ListViewItem.ForeColor父项的 和 ListViewItem.Font 值重写子项值。
另请参阅
- Bounds
 - ListView
 - DrawSubItem
 - UseItemStyleForSubItems
 - ListViewItem.ListViewSubItem
 - Font
 - ForeColor
 - StringFormat