TextRenderer.DrawText 方法

定义

使用指定的设备上下文、颜色和字体在指定位置绘制指定文本。Draws the specified text at the specified location, using the specified device context, color, and font.

重载

DrawText(IDeviceContext, String, Font, Point, Color)

使用指定的设备上下文、字体和颜色在指定位置绘制指定文本。Draws the specified text at the specified location using the specified device context, font, and color.

DrawText(IDeviceContext, String, Font, Rectangle, Color)

使用指定的设备上下文、字体和颜色在指定界限中绘制指定文本。Draws the specified text within the specified bounds, using the specified device context, font, and color.

DrawText(IDeviceContext, String, Font, Point, Color, Color)

使用指定的设备上下文、字体、颜色和背景色在指定位置绘制指定文本。Draws the specified text at the specified location, using the specified device context, font, color, and back color.

DrawText(IDeviceContext, String, Font, Point, Color, TextFormatFlags)

使用指定的设备上下文、字体、颜色和格式说明在指定位置绘制指定文本。Draws the specified text at the specified location using the specified device context, font, color, and formatting instructions.

DrawText(IDeviceContext, String, Font, Rectangle, Color, Color)

使用指定的设备上下文、字体、颜色和背景色在指定界限中绘制指定文本。Draws the specified text within the specified bounds using the specified device context, font, color, and back color.

DrawText(IDeviceContext, String, Font, Rectangle, Color, TextFormatFlags)

使用指定的设备上下文、字体、颜色和格式说明在指定界限中绘制指定文本。Draws the specified text within the specified bounds using the specified device context, font, color, and formatting instructions.

DrawText(IDeviceContext, String, Font, Point, Color, Color, TextFormatFlags)

使用指定的设备上下文、字体、颜色、背景色和格式说明在指定位置绘制指定文本。Draws the specified text at the specified location using the specified device context, font, color, back color, and formatting instructions.

DrawText(IDeviceContext, String, Font, Rectangle, Color, Color, TextFormatFlags)

使用指定的设备上下文、字体、颜色、背景色和格式说明在指定界限中绘制指定文本。Draws the specified text within the specified bounds using the specified device context, font, color, back color, and formatting instructions.

注解

TextRenderer 类提供的文本呈现基于 GDI 文本呈现,不支持从 Windows 窗体进行打印。The text rendering offered by the TextRenderer class is based on GDI text rendering and is not supported for printing from Windows Forms. 请改用 Graphics 类的 DrawString 方法。Instead, use the DrawString methods of the Graphics class.

DrawText(IDeviceContext, String, Font, Point, Color)

使用指定的设备上下文、字体和颜色在指定位置绘制指定文本。Draws the specified text at the specified location using the specified device context, font, and color.

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Point pt, System::Drawing::Color foreColor);
public static void DrawText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Point pt, System.Drawing.Color foreColor);
static member DrawText : System.Drawing.IDeviceContext * string * System.Drawing.Font * System.Drawing.Point * System.Drawing.Color -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As String, font As Font, pt As Point, foreColor As Color)

参数

dc
IDeviceContext

绘制文本所使用的设备上下文。The device context in which to draw the text.

text
String

要绘制的文本。The text to draw.

font
Font

要应用于所绘制文本的 FontThe Font to apply to the drawn text.

pt
Point

表示所绘制文本左上角的 PointThe Point that represents the upper-left corner of the drawn text.

foreColor
Color

要应用于所绘制文本的 ColorThe Color to apply to the drawn text.

例外

dcnulldc is null.

示例

下面的代码示例演示如何使用 DrawText 方法。The following code example demonstrates how to use the DrawText method. 若要运行此示例,请将代码粘贴到 Windows 窗体中,并从窗体的 Paint 事件处理程序调用 RenderText1,并将 e 作为 PaintEventArgs传递。To run this example, paste the code into a Windows Form and call RenderText1 from the form's Paint event handler, passing e as PaintEventArgs.

private void RenderText1(PaintEventArgs e)
{
    TextRenderer.DrawText(e.Graphics, "Regular Text", this.Font, 
        new Point(10, 10), SystemColors.ControlText);
}
Private Sub RenderText1(ByVal e As PaintEventArgs)
    TextRenderer.DrawText(e.Graphics, "Regular Text", _
        Me.Font, New Point(10, 10), SystemColors.ControlText)

End Sub


注解

您可以通过使用带有 DrawText 参数的 TextFormatFlags 重载之一,控制文本的绘制方式。You can manipulate how the text is drawn by using one of the DrawText overloads that takes a TextFormatFlags parameter.

如果 font nullforecolorColor.Empty,则 DrawText 方法将使用 dc指定的设备上下文中当前选定的字体或颜色绘制文本。If font is null or forecolor is Color.Empty, the DrawText method will draw the text in the font or color currently selected in the device context specified by dc. 如果 Transparentforecolor,则不会绘制文本。If forecolor is Transparent, the text will not be drawn.

TextRenderer 类提供的文本呈现基于 GDI 文本呈现,不支持从 Windows 窗体进行打印。The text rendering offered by the TextRenderer class is based on GDI text rendering and is not supported for printing from Windows Forms. 请改用 Graphics 类的 DrawString 方法。Instead, use the DrawString methods of the Graphics class.

备注

指定 PointDrawText 方法在 Windows 2000 上不会正确呈现为所绘制文本的左上角。The DrawText methods that specify a Point as the upper-left corner of the drawn text do not render correctly on Windows 2000. 如果你的应用程序在运行 Windows 2000 的计算机上使用,则应使用指定所绘制文本边界的 RectangleDrawText 方法之一。If your application is intended for use on machines running Windows 2000, you should use one of the DrawText methods that specify a Rectangle for the bounds of the drawn text.

另请参阅

DrawText(IDeviceContext, String, Font, Rectangle, Color)

使用指定的设备上下文、字体和颜色在指定界限中绘制指定文本。Draws the specified text within the specified bounds, using the specified device context, font, and color.

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Rectangle bounds, System::Drawing::Color foreColor);
public static void DrawText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor);
static member DrawText : System.Drawing.IDeviceContext * string * System.Drawing.Font * System.Drawing.Rectangle * System.Drawing.Color -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As String, font As Font, bounds As Rectangle, foreColor As Color)

参数

dc
IDeviceContext

绘制文本所使用的设备上下文。The device context in which to draw the text.

text
String

要绘制的文本。The text to draw.

font
Font

要应用于所绘制文本的 FontThe Font to apply to the drawn text.

bounds
Rectangle

表示文本界限的 RectangleThe Rectangle that represents the bounds of the text.

foreColor
Color

要应用于所绘制文本的 ColorThe Color to apply to the drawn text.

例外

dcnulldc is null.

示例

下面的代码示例演示如何使用 DrawText 方法。The following code example demonstrates how to use the DrawText method. 若要运行此示例,请将代码粘贴到 Windows 窗体中,并从窗体的 Paint 事件处理程序调用 RenderText2,并将 e 作为 PaintEventArgs传递。To run this example, paste the code into a Windows Form and call RenderText2 from the form's Paint event handler, passing e as PaintEventArgs.

private void RenderText2(PaintEventArgs e)
{
    TextRenderer.DrawText(e.Graphics, "Regular Text", this.Font,
        new Rectangle(10, 10, 100, 100), SystemColors.ControlText);
}
Private Sub RenderText2(ByVal e As PaintEventArgs)
    TextRenderer.DrawText(e.Graphics, "Regular Text", _
        Me.Font, New Rectangle(10, 10, 100, 100), _
        SystemColors.ControlText)

End Sub


注解

如果 fontforecolor 分别为 nullColor.Empty,则为;DrawText 方法将以当前在 dc指定的设备上下文中选择的字体或颜色绘制文本。If font or forecolor is null or Color.Empty, respectively; the DrawText method will draw the text in the font or color currently selected in the device context specified by dc. 如果 Transparentforecolor,则不会绘制文本。If forecolor is Transparent, the text will not be drawn.

此方法将导致在 bounds 参数指定的矩形中水平和垂直居中的文本。This method will result in text that is horizontally and vertically centered in the rectangle specified by the bounds parameter. 若要更改文本的绘制方式,请使用采用 TextFormatFlags 参数类型的 DrawText 版本。To change how the text is drawn, use a version of DrawText that takes a TextFormatFlags parameter type.

TextRenderer 类提供的文本呈现基于 GDI 文本呈现,不支持从 Windows 窗体进行打印。The text rendering offered by the TextRenderer class is based on GDI text rendering and is not supported for printing from Windows Forms. 请改用 Graphics 类的 DrawString 方法。Instead, use the DrawString methods of the Graphics class.

另请参阅

DrawText(IDeviceContext, String, Font, Point, Color, Color)

使用指定的设备上下文、字体、颜色和背景色在指定位置绘制指定文本。Draws the specified text at the specified location, using the specified device context, font, color, and back color.

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Point pt, System::Drawing::Color foreColor, System::Drawing::Color backColor);
public static void DrawText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Point pt, System.Drawing.Color foreColor, System.Drawing.Color backColor);
static member DrawText : System.Drawing.IDeviceContext * string * System.Drawing.Font * System.Drawing.Point * System.Drawing.Color * System.Drawing.Color -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As String, font As Font, pt As Point, foreColor As Color, backColor As Color)

参数

dc
IDeviceContext

绘制文本所使用的设备上下文。The device context in which to draw the text.

text
String

要绘制的文本。The text to draw.

font
Font

要应用于所绘制文本的 FontThe Font to apply to the drawn text.

pt
Point

表示所绘制文本左上角的 PointThe Point that represents the upper-left corner of the drawn text.

foreColor
Color

要应用于所绘制文本的 ColorThe Color to apply to the drawn text.

backColor
Color

要应用于所绘制文本的背景区域的 ColorThe Color to apply to the background area of the drawn text.

例外

dcnulldc is null.

示例

下面的代码示例演示如何使用 DrawText 方法。The following code example demonstrates how to use the DrawText method. 若要运行此示例,请将代码粘贴到 Windows 窗体中,并从窗体的 Paint 事件处理程序调用 RenderText3,并将 e 作为 PaintEventArgs传递。To run this example, paste the code into a Windows Form and call RenderText3 from the form's Paint event handler, passing e as PaintEventArgs.

private void RenderText3(PaintEventArgs e)
{
    TextRenderer.DrawText(e.Graphics, "Regular Text", this.Font,
        new Point(10, 10), Color.Red, Color.PowderBlue);
}
Private Sub RenderText3(ByVal e As PaintEventArgs)
    TextRenderer.DrawText(e.Graphics, "Regular Text", Me.Font, _
        New Point(10, 10), Color.Red, Color.PowderBlue)

End Sub

注解

backColor 参数将应用于包含所绘制文本的矩形区域。The backColor parameter is applied to the rectangular area containing the drawn text. 如果 fontbackcolorforecolor 分别为 nullColor.Empty;DrawText 方法将以当前在 dc指定的设备上下文中选择的字体或颜色绘制文本。If font, backcolor or forecolor is null or Color.Empty, respectively; the DrawText method will draw the text in the font or color currently selected in the device context specified by dc. 如果 Transparentforecolor,则不会绘制文本。If forecolor is Transparent, the text will not be drawn.

您可以通过使用带有 DrawText 参数的 TextFormatFlags 重载之一,控制文本的绘制方式。You can manipulate how the text is drawn by using one of the DrawText overloads that takes a TextFormatFlags parameter.

TextRenderer 类提供的文本呈现基于 GDI 文本呈现,不支持从 Windows 窗体进行打印。The text rendering offered by the TextRenderer class is based on GDI text rendering and is not supported for printing from Windows Forms. 请改用 Graphics 类的 DrawString 方法。Instead, use the DrawString methods of the Graphics class.

备注

指定 PointDrawText 方法在 Windows 2000 上不会正确呈现为所绘制文本的左上角。The DrawText methods that specify a Point as the upper-left corner of the drawn text do not render correctly on Windows 2000. 如果你的应用程序在运行 Windows 2000 的计算机上使用,则应使用指定所绘制文本边界的 RectangleDrawText 方法之一。If your application is intended for use on machines running Windows 2000, you should use one of the DrawText methods that specify a Rectangle for the bounds of the drawn text.

DrawText(IDeviceContext, String, Font, Point, Color, TextFormatFlags)

使用指定的设备上下文、字体、颜色和格式说明在指定位置绘制指定文本。Draws the specified text at the specified location using the specified device context, font, color, and formatting instructions.

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Point pt, System::Drawing::Color foreColor, System::Windows::Forms::TextFormatFlags flags);
public static void DrawText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Point pt, System.Drawing.Color foreColor, System.Windows.Forms.TextFormatFlags flags);
static member DrawText : System.Drawing.IDeviceContext * string * System.Drawing.Font * System.Drawing.Point * System.Drawing.Color * System.Windows.Forms.TextFormatFlags -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As String, font As Font, pt As Point, foreColor As Color, flags As TextFormatFlags)

参数

dc
IDeviceContext

绘制文本所使用的设备上下文。The device context in which to draw the text.

text
String

要绘制的文本。The text to draw.

font
Font

要应用于所绘制文本的 FontThe Font to apply to the drawn text.

pt
Point

表示所绘制文本左上角的 PointThe Point that represents the upper-left corner of the drawn text.

foreColor
Color

要应用于所绘制文本的 ColorThe Color to apply to the drawn text.

flags
TextFormatFlags

TextFormatFlags 值的按位组合。A bitwise combination of the TextFormatFlags values.

例外

dcnulldc is null.

示例

下面的代码示例演示如何使用 DrawText 方法。The following code example demonstrates how to use the DrawText method. 若要运行此示例,请将代码粘贴到 Windows 窗体中,并从窗体的 Paint 事件处理程序调用 RenderText5,并将 e 作为 PaintEventArgs传递。To run this example, paste the code into a Windows Form and call RenderText5 from the form's Paint event handler, passing e as PaintEventArgs.

private void RenderText5(PaintEventArgs e)
{
    TextRenderer.DrawText(e.Graphics, "Some text.",
        this.Font, new Point(10, 10), SystemColors.ControlText, TextFormatFlags.Bottom);
}
Private Sub RenderText5(ByVal e As PaintEventArgs)
    TextRenderer.DrawText(e.Graphics, "Some text.", _
    Me.Font, New Point(10, 10), SystemColors.ControlText, _
    TextFormatFlags.Bottom)

End Sub

注解

您可以通过使用带有 DrawText 参数的 TextFormatFlags 重载之一,控制文本的绘制方式。You can manipulate how the text is drawn by using one of the DrawText overloads that takes a TextFormatFlags parameter. 例如,TextRenderer 的默认行为是向所绘制文本的边框添加空白以符合延伸标志符号。For example, the default behavior of the TextRenderer is to add padding to the bounding rectangle of the drawn text to accommodate overhanging glyphs. 如果绘制一行文本时不需要这些额外空白,您应当使用带有 DrawTextMeasureText 参数的 SizeTextFormatFlags 版本。If you need to draw a line of text without these extra spaces you should use the versions of DrawText and MeasureText that take a Size and TextFormatFlags parameter. 有关示例,请参见 MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)For an example, see MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags).

backColor 参数将应用于 bounds 参数内的区域。The backColor parameter is applied to the area within the bounds parameter. 如果 fontforecolor 分别为 nullColor.Empty,则为;DrawText 方法将以当前在 dc指定的设备上下文中选择的字体或颜色绘制文本。If font or forecolor is null or Color.Empty, respectively; the DrawText method will draw the text in the font or color currently selected in the device context specified by dc. 如果 Transparentforecolor,则不会绘制文本。If forecolor is Transparent, the text will not be drawn.

TextRenderer 类提供的文本呈现基于 GDI 文本呈现,不支持从 Windows 窗体进行打印。The text rendering offered by the TextRenderer class is based on GDI text rendering and is not supported for printing from Windows Forms. 请改用 Graphics 类的 DrawString 方法。Instead, use the DrawString methods of the Graphics class.

虽然您可以使用 ExpandTabs 标志展开现有制表位,但是 TextRenderer 不支持向绘制文本添加制表位。The TextRenderer does not support adding tab stops to drawn text, although you can expand existing tab stops using the ExpandTabs flag.

备注

指定 PointDrawText 方法在 Windows 2000 上不会正确呈现为所绘制文本的左上角。The DrawText methods that specify a Point as the upper-left corner of the drawn text do not render correctly on Windows 2000. 如果你的应用程序在运行 Windows 2000 的计算机上使用,则应使用指定所绘制文本边界的 RectangleDrawText 方法之一。If your application is intended for use on machines running Windows 2000, you should use one of the DrawText methods that specify a Rectangle for the bounds of the drawn text.

DrawText(IDeviceContext, String, Font, Rectangle, Color, Color)

使用指定的设备上下文、字体、颜色和背景色在指定界限中绘制指定文本。Draws the specified text within the specified bounds using the specified device context, font, color, and back color.

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Rectangle bounds, System::Drawing::Color foreColor, System::Drawing::Color backColor);
public static void DrawText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor, System.Drawing.Color backColor);
static member DrawText : System.Drawing.IDeviceContext * string * System.Drawing.Font * System.Drawing.Rectangle * System.Drawing.Color * System.Drawing.Color -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As String, font As Font, bounds As Rectangle, foreColor As Color, backColor As Color)

参数

dc
IDeviceContext

绘制文本所使用的设备上下文。The device context in which to draw the text.

text
String

要绘制的文本。The text to draw.

font
Font

要应用于所绘制文本的 FontThe Font to apply to the drawn text.

bounds
Rectangle

表示文本界限的 RectangleThe Rectangle that represents the bounds of the text.

foreColor
Color

要应用于所绘制文本的 ColorThe Color to apply to the drawn text.

backColor
Color

要应用于 Color 所表示区域的 boundsThe Color to apply to the area represented by bounds.

例外

dcnulldc is null.

示例

下面的代码示例演示如何使用 DrawText 方法。The following code example demonstrates how to use the DrawText method. 若要运行此示例,请将代码粘贴到 Windows 窗体中,并从窗体的 Paint 事件处理程序调用 RenderText4,并将 e 作为 PaintEventArgs传递。To run this example, paste the code into a Windows Form and call RenderText4 from the form's Paint event handler, passing e as PaintEventArgs.

private void RenderText4(PaintEventArgs e)
{
    TextRenderer.DrawText(e.Graphics, "Regular Text.", this.Font,
        new Rectangle(10, 10, 70, 70), SystemColors.ControlText, 
        SystemColors.ControlDark);
}
Private Sub RenderText4(ByVal e As PaintEventArgs)
    TextRenderer.DrawText(e.Graphics, "Regular Text.", _
        Me.Font, New Rectangle(10, 10, 70, 70), _
        SystemColors.ControlText, SystemColors.ControlDark)

End Sub

注解

backColor 参数将应用于 bounds 参数内的区域。The backColor parameter is applied to the area within the bounds parameter. 如果 fontbackcolorforecolor 分别为 nullEmpty;DrawText 方法将以当前在 dc指定的设备上下文中选择的字体或颜色绘制文本。If font, backcolor or forecolor is null or Empty, respectively; the DrawText method will draw the text in the font or color currently selected in the device context specified by dc. 如果 Transparentforecolor,则不会绘制文本。If forecolor is Transparent, the text will not be drawn.

此方法将导致在 bounds 参数指定的矩形中水平和垂直居中的文本。This method will result in text that is horizontally and vertically centered in the rectangle specified by the bounds parameter. 若要更改文本的绘制方式,请使用采用 TextFormatFlags 参数类型的 DrawText 版本。To change how the text is drawn, use a version of DrawText that takes a TextFormatFlags parameter type.

TextRenderer 类提供的文本呈现基于 GDI 文本呈现,不支持从 Windows 窗体进行打印。The text rendering offered by the TextRenderer class is based on GDI text rendering and is not supported for printing from Windows Forms. 请改用 Graphics 类的 DrawString 方法。Instead, use the DrawString methods of the Graphics class.

DrawText(IDeviceContext, String, Font, Rectangle, Color, TextFormatFlags)

使用指定的设备上下文、字体、颜色和格式说明在指定界限中绘制指定文本。Draws the specified text within the specified bounds using the specified device context, font, color, and formatting instructions.

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Rectangle bounds, System::Drawing::Color foreColor, System::Windows::Forms::TextFormatFlags flags);
public static void DrawText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor, System.Windows.Forms.TextFormatFlags flags);
static member DrawText : System.Drawing.IDeviceContext * string * System.Drawing.Font * System.Drawing.Rectangle * System.Drawing.Color * System.Windows.Forms.TextFormatFlags -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As String, font As Font, bounds As Rectangle, foreColor As Color, flags As TextFormatFlags)

参数

dc
IDeviceContext

绘制文本所使用的设备上下文。The device context in which to draw the text.

text
String

要绘制的文本。The text to draw.

font
Font

要应用于所绘制文本的 FontThe Font to apply to the drawn text.

bounds
Rectangle

表示文本界限的 RectangleThe Rectangle that represents the bounds of the text.

foreColor
Color

要应用于所绘制文本的 ColorThe Color to apply to the drawn text.

flags
TextFormatFlags

TextFormatFlags 值的按位组合。A bitwise combination of the TextFormatFlags values.

例外

dcnulldc is null.

示例

下面的代码示例演示如何使用 DrawText 方法。The following code example demonstrates how to use the DrawText method. 若要运行此示例,请将代码粘贴到 Windows 窗体中,并从窗体的 Paint 事件处理程序调用 RenderText6,并将 e 作为 PaintEventArgs传递。To run this example, paste the code into a Windows Form and call RenderText6 from the form's Paint event handler, passing e as PaintEventArgs.

private void RenderText6(PaintEventArgs e)
{
    TextFormatFlags flags = TextFormatFlags.Bottom | TextFormatFlags.EndEllipsis;
    TextRenderer.DrawText(e.Graphics, "This is some text that will be clipped at the end.", this.Font,
        new Rectangle(10, 10, 100, 50), SystemColors.ControlText, flags);
}
Private Sub RenderText6(ByVal e As PaintEventArgs)
    Dim flags As TextFormatFlags = TextFormatFlags.Bottom Or _
        TextFormatFlags.EndEllipsis
    TextRenderer.DrawText(e.Graphics, _
    "This is some text that will be clipped at the end.", _
    Me.Font, New Rectangle(10, 10, 100, 50), SystemColors.ControlText, flags)

End Sub

注解

如果 fontcolor 分别为 nullEmpty,则为;DrawText 方法将以当前在 dc指定的设备上下文中选择的字体或颜色绘制文本。If font or color is null or Empty, respectively; the DrawText method will draw the text in the font or color currently selected in the device context specified by dc. 如果 Transparentforecolor,则不会绘制文本。If forecolor is Transparent, the text will not be drawn.

您可以通过使用带有 DrawText 参数的 TextFormatFlags 重载之一,控制文本的绘制方式。You can manipulate how the text is drawn by using one of the DrawText overloads that takes a TextFormatFlags parameter. 例如,TextRenderer 的默认行为是向所绘制文本的边框添加空白以符合延伸标志符号。For example, the default behavior of the TextRenderer is to add padding to the bounding rectangle of the drawn text to accommodate overhanging glyphs. 如果绘制一行文本时不需要这些额外空白,您应当使用带有 DrawTextMeasureText 参数的 SizeTextFormatFlags 版本。If you need to draw a line of text without these extra spaces you should use the versions of DrawText and MeasureText that take a Size and TextFormatFlags parameter. 有关示例,请参见 MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)For an example, see MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags).

TextRenderer 类提供的文本呈现基于 GDI 文本呈现,不支持从 Windows 窗体进行打印。The text rendering offered by the TextRenderer class is based on GDI text rendering and is not supported for printing from Windows Forms. 请改用 Graphics 类的 DrawString 方法。Instead, use the DrawString methods of the Graphics class.

虽然您可以使用 ExpandTabs 标志展开现有制表位,但是 TextRenderer 不支持向绘制文本添加制表位。The TextRenderer does not support adding tab stops to drawn text, although you can expand existing tab stops using the ExpandTabs flag.

DrawText(IDeviceContext, String, Font, Point, Color, Color, TextFormatFlags)

使用指定的设备上下文、字体、颜色、背景色和格式说明在指定位置绘制指定文本。Draws the specified text at the specified location using the specified device context, font, color, back color, and formatting instructions.

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Point pt, System::Drawing::Color foreColor, System::Drawing::Color backColor, System::Windows::Forms::TextFormatFlags flags);
public static void DrawText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Point pt, System.Drawing.Color foreColor, System.Drawing.Color backColor, System.Windows.Forms.TextFormatFlags flags);
static member DrawText : System.Drawing.IDeviceContext * string * System.Drawing.Font * System.Drawing.Point * System.Drawing.Color * System.Drawing.Color * System.Windows.Forms.TextFormatFlags -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As String, font As Font, pt As Point, foreColor As Color, backColor As Color, flags As TextFormatFlags)

参数

dc
IDeviceContext

绘制文本所使用的设备上下文。The device context in which to draw the text.

text
String

要绘制的文本。The text to draw.

font
Font

要应用于所绘制文本的 FontThe Font to apply to the drawn text.

pt
Point

表示所绘制文本左上角的 PointThe Point that represents the upper-left corner of the drawn text.

foreColor
Color

要应用于文本的 ColorThe Color to apply to the text.

backColor
Color

要应用于所绘制文本的背景区域的 ColorThe Color to apply to the background area of the drawn text.

flags
TextFormatFlags

TextFormatFlags 值的按位组合。A bitwise combination of the TextFormatFlags values.

例外

dcnulldc is null.

示例

下面的代码示例演示如何使用 DrawText 方法。The following code example demonstrates how to use the DrawText method. 若要运行此示例,请将代码粘贴到 Windows 窗体中,并从窗体的 Paint 事件处理程序调用 RenderText7,并将 e 作为 PaintEventArgs传递。To run this example, paste the code into a Windows Form and call RenderText7 from the form's Paint event handler, passing e as PaintEventArgs.

private void RenderText7(PaintEventArgs e)
{
    TextRenderer.DrawText(e.Graphics, "This is some text.", this.Font,
        new Point(10, 10), Color.White, Color.SteelBlue, TextFormatFlags.Default);
}
Private Sub RenderText7(ByVal e As PaintEventArgs)
    TextRenderer.DrawText(e.Graphics, "This is some text.", _
        Me.Font, New Point(10, 10), Color.White, Color.SteelBlue, _
        TextFormatFlags.Default)

End Sub

注解

backcolor 参数将应用于包含所绘制文本的矩形区域。The backcolor parameter is applied the rectangular area containing the drawn text. 如果 fontforecolor 分别为 nullEmpty,则为;DrawText 方法将以当前在 dc指定的设备上下文中选择的字体或颜色绘制文本。If font or forecolor is null or Empty, respectively; the DrawText method will draw the text in the font or color currently selected in the device context specified by dc. 如果 Transparentforecolor,则不会绘制文本。If forecolor is Transparent, the text will not be drawn.

您可以通过使用带有 DrawText 参数的 TextFormatFlags 重载之一,控制文本的绘制方式。You can manipulate how the text is drawn by using one of the DrawText overloads that takes a TextFormatFlags parameter. 例如,TextRenderer 的默认行为是向所绘制文本的边框添加空白以符合延伸标志符号。For example, the default behavior of the TextRenderer is to add padding to the bounding rectangle of the drawn text to accommodate overhanging glyphs. 如果绘制一行文本时不需要这些额外空白,您应当使用带有 DrawTextMeasureText 参数的 SizeTextFormatFlags 版本。If you need to draw a line of text without these extra spaces you should use the versions of DrawText and MeasureText that take a Size and TextFormatFlags parameter. 有关示例,请参见 MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)For an example, see MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags).

TextRenderer 类提供的文本呈现基于 GDI 文本呈现,不支持从 Windows 窗体进行打印。The text rendering offered by the TextRenderer class is based on GDI text rendering and is not supported for printing from Windows Forms. 请改用 Graphics 类的 DrawString 方法。Instead, use the DrawString methods of the Graphics class.

虽然您可以使用 ExpandTabs 标志展开现有制表位,但是 TextRenderer 不支持向绘制文本添加制表位。The TextRenderer does not support adding tab stops to drawn text, although you can expand existing tab stops using the ExpandTabs flag.

备注

指定 PointDrawText 方法在 Windows 2000 上不会正确呈现为所绘制文本的左上角。The DrawText methods that specify a Point as the upper-left corner of the drawn text do not render correctly on Windows 2000. 如果你的应用程序在运行 Windows 2000 的计算机上使用,则应使用指定所绘制文本边界的 RectangleDrawText 方法之一。If your application is intended for use on machines running Windows 2000, you should use one of the DrawText methods that specify a Rectangle for the bounds of the drawn text.

DrawText(IDeviceContext, String, Font, Rectangle, Color, Color, TextFormatFlags)

使用指定的设备上下文、字体、颜色、背景色和格式说明在指定界限中绘制指定文本。Draws the specified text within the specified bounds using the specified device context, font, color, back color, and formatting instructions.

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Rectangle bounds, System::Drawing::Color foreColor, System::Drawing::Color backColor, System::Windows::Forms::TextFormatFlags flags);
public static void DrawText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor, System.Drawing.Color backColor, System.Windows.Forms.TextFormatFlags flags);
static member DrawText : System.Drawing.IDeviceContext * string * System.Drawing.Font * System.Drawing.Rectangle * System.Drawing.Color * System.Drawing.Color * System.Windows.Forms.TextFormatFlags -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As String, font As Font, bounds As Rectangle, foreColor As Color, backColor As Color, flags As TextFormatFlags)

参数

dc
IDeviceContext

绘制文本所使用的设备上下文。The device context in which to draw the text.

text
String

要绘制的文本。The text to draw.

font
Font

要应用于所绘制文本的 FontThe Font to apply to the drawn text.

bounds
Rectangle

表示文本界限的 RectangleThe Rectangle that represents the bounds of the text.

foreColor
Color

要应用于文本的 ColorThe Color to apply to the text.

backColor
Color

要应用于 Color 所表示区域的 boundsThe Color to apply to the area represented by bounds.

flags
TextFormatFlags

TextFormatFlags 值的按位组合。A bitwise combination of the TextFormatFlags values.

例外

dcnulldc is null.

示例

下面的代码示例演示如何使用 DrawText 方法。The following code example demonstrates how to use the DrawText method. 若要运行此示例,请将代码粘贴到 Windows 窗体中,并从窗体的 Paint 事件处理程序调用 RenderText8,并将 e 作为 PaintEventArgs传递。To run this example, paste the code into a Windows Form and call RenderText8 from the form's Paint event handler, passing e as PaintEventArgs.

private void RenderText8(PaintEventArgs e)
{
    TextFormatFlags flags = TextFormatFlags.Bottom | TextFormatFlags.WordBreak;
    TextRenderer.DrawText(e.Graphics, "This is some text that will display on multiple lines.", this.Font,
        new Rectangle(10, 10, 100, 50), SystemColors.ControlText, SystemColors.ControlDark, flags);
}

Private Sub RenderText8(ByVal e As PaintEventArgs)
    Dim flags As TextFormatFlags = _
        TextFormatFlags.Bottom Or TextFormatFlags.WordBreak
    TextRenderer.DrawText(e.Graphics, _
        "This is some text that will display on multiple lines.", _
        Me.Font, New Rectangle(10, 10, 100, 50), _
        SystemColors.ControlText, SystemColors.ControlDark, flags)

End Sub


注解

backcolor 参数将应用于 bounds 参数中的区域. 如果 fontforecolorbackcolor 分别为 nullEmpty,则为;DrawText 方法将以当前在 dc指定的设备上下文中选择的字体或颜色绘制文本。The backcolor parameter is applied to the area within the bounds parameter. If font, forecolor or backcolor is null or Empty, respectively; the DrawText method will draw the text in the font or color currently selected in the device context specified by dc. 如果 Transparentforecolor,则不会绘制文本。If forecolor is Transparent, the text will not be drawn.

您可以通过使用带有 DrawText 参数的 TextFormatFlags 重载之一,控制文本的绘制方式。You can manipulate how the text is drawn by using one of the DrawText overloads that takes a TextFormatFlags parameter. 例如,TextRenderer 的默认行为是向所绘制文本的边框添加空白以符合延伸标志符号。For example, the default behavior of the TextRenderer is to add padding to the bounding rectangle of the drawn text to accommodate overhanging glyphs. 如果绘制一行文本时不需要这些额外空白,您应当使用带有 DrawTextMeasureText 参数的 SizeTextFormatFlags 版本。If you need to draw a line of text without these extra spaces you should use the versions of DrawText and MeasureText that take a Size and TextFormatFlags parameter. 有关示例,请参见 MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)For an example, see MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags).

TextRenderer 类提供的文本呈现基于 GDI 文本呈现,不支持从 Windows 窗体进行打印。The text rendering offered by the TextRenderer class is based on GDI text rendering and is not supported for printing from Windows Forms. 请改用 Graphics 类的 DrawString 方法。Instead, use the DrawString methods of the Graphics class.

虽然您可以使用 ExpandTabs 标志展开现有制表位,但是 TextRenderer 不支持向绘制文本添加制表位。The TextRenderer does not support adding tab stops to drawn text, although you can expand existing tab stops using the ExpandTabs flag.

适用于