System.Windows.Forms.TextRenderer.DrawText Method

Draws the specified text within the specified bounds using the specified device context, font, color, and formatting instructions.

Syntax

public static void DrawText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor, TextFormatFlags flags)

Parameters

dc
The device context in which to draw the text.
text
The text to draw.
font
The System.Drawing.Font to apply to the drawn text.
bounds
The System.Drawing.Rectangle that represents the bounds of the text.
foreColor
The System.Drawing.Color to apply to the drawn text.
flags
A bitwise combination of the System.Windows.Forms.TextFormatFlags values.

Remarks

If font or color is null or System.Drawing.Color.Empty, respectively; the erload:System.Windows.Forms.TextRenderer.DrawText method will draw the text in the font or color currently selected in the device context specified by dc. If forecolor is System.Drawing.Color.Transparent, the text will not be drawn.

You can manipulate how the text is drawn by using one of the TextRenderer.DrawText(System.Drawing.IDeviceContext, string, System.Drawing.Font, System.Drawing.Rectangle, System.Drawing.Color, TextFormatFlags) overloads that takes a System.Windows.Forms.TextFormatFlags parameter. For example, the default behavior of the System.Windows.Forms.TextRenderer is to add padding to the bounding rectangle of the drawn text to accommodate overhanging glyphs. If you need to draw a line of text without these extra spaces you should use the versions of TextRenderer.DrawText(System.Drawing.IDeviceContext, string, System.Drawing.Font, System.Drawing.Point, System.Drawing.Color) and TextRenderer.MeasureText(System.Drawing.IDeviceContext, string, System.Drawing.Font) that take a System.Drawing.Size and System.Windows.Forms.TextFormatFlags parameter. For an example, see TextRenderer.MeasureText(System.Drawing.IDeviceContext, string, System.Drawing.Font, System.Drawing.Size, TextFormatFlags).

The text rendering offered by the System.Windows.Forms.TextRenderer class is based on GDI text rendering and is not supported for printing from Windows Forms. Instead, use the erload:System.Drawing.Graphics.DrawString methods of the System.Drawing.Graphics class.

The System.Windows.Forms.TextRenderer does not support adding tab stops to drawn text, although you can expand existing tab stops using the TextFormatFlags.ExpandTabs flag.

Requirements

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Assembly Versions: 2.0.0.0
Since: .NET 2.0