System.Windows.Forms.TextRenderer.DrawText Method

Draws the specified text at the specified location using the specified device context, font, color, back color, and formatting instructions

Syntax

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, 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.
pt
The System.Drawing.Point that represents the upper-left corner of the drawn text.
foreColor
The System.Drawing.Color to apply to the text.
backColor
The System.Drawing.Color to apply to the background area of the drawn text.
flags
A bitwise combination of the System.Windows.Forms.TextFormatFlags values.

Remarks

The backcolor parameter is applied the rectangular area containing the drawn text. If font or forecolor 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.

Note:

The erload:System.Windows.Forms.TextRenderer.DrawText methods that specify a System.Drawing.Point as the upper-left corner of the drawn text do not render correctly on Windows 2000. If your application is intended for use on machines running Windows 2000, you should use one of the erload:System.Windows.Forms.TextRenderer.DrawText methods that specify a System.Drawing.Rectangle for the bounds of the drawn text.

Requirements

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