Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

TextRenderer.DrawText Method (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.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
public static void DrawText(
	IDeviceContext dc,
	string text,
	Font font,
	Rectangle bounds,
	Color foreColor,
	Color backColor,
	TextFormatFlags flags
)

Parameters

dc
Type: System.Drawing.IDeviceContext

The device context in which to draw the text.

text
Type: System.String

The text to draw.

font
Type: System.Drawing.Font

The Font to apply to the drawn text.

bounds
Type: System.Drawing.Rectangle

The Rectangle that represents the bounds of the text.

foreColor
Type: System.Drawing.Color

The Color to apply to the text.

backColor
Type: System.Drawing.Color

The Color to apply to the area represented by bounds.

flags
Type: System.Windows.Forms.TextFormatFlags

A bitwise combination of the TextFormatFlags values.

ExceptionCondition
ArgumentNullException

dc is null.

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. If forecolor is Transparent, the text will not be drawn.

You can manipulate how the text is drawn by using one of the DrawText overloads that takes a TextFormatFlags parameter. For example, the default behavior of the 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 DrawText and MeasureText that take a Size and TextFormatFlags parameter. For an example, see MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags).

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

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

The following code example demonstrates how to use the DrawText method. 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);
        }

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.