TextRenderer.DrawText Method (IDeviceContext, String, Font, Rectangle, Color, Color)

 

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

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
)

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 drawn text.

backColor
Type: System.Drawing.Color

The Color to apply to the area represented by bounds.

Exception Condition
ArgumentNullException

dc is null.

The backColor parameter is applied to the area within the bounds parameter. 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. If forecolor is Transparent, the text will not be drawn.

This method will result in text that is horizontally and vertically centered in the rectangle specified by the bounds parameter. To change how the text is drawn, use a version of DrawText that takes a TextFormatFlags parameter type.

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

.NET Framework
Available since 2.0
Return to top
Show: