.NET Framework Class Library
TextRenderer..::.DrawText Method (IDeviceContext, String, Font, Point, Color)

Draws the specified text at the specified location using the specified device context, font, and color.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
Syntax

Visual Basic (Declaration)
Public Shared Sub DrawText ( _
    dc As IDeviceContext, _
    text As String, _
    font As Font, _
    pt As Point, _
    foreColor As Color _
)
Visual Basic (Usage)
Dim dc As IDeviceContext
Dim text As String
Dim font As Font
Dim pt As Point
Dim foreColor As Color

TextRenderer.DrawText(dc, text, font, pt, foreColor)
C#
public static void DrawText(
    IDeviceContext dc,
    string text,
    Font font,
    Point pt,
    Color foreColor
)
Visual C++
public:
static void DrawText(
    IDeviceContext^ dc, 
    String^ text, 
    Font^ font, 
    Point pt, 
    Color foreColor
)
JScript
public static function DrawText(
    dc : IDeviceContext, 
    text : String, 
    font : Font, 
    pt : Point, 
    foreColor : Color
)

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.
pt
Type: System.Drawing..::.Point
The Point that represents the upper-left corner of the drawn text.
foreColor
Type: System.Drawing..::.Color
The Color to apply to the drawn text.
Exceptions

ExceptionCondition
ArgumentNullException

dc is nullNothingnullptra null reference (Nothing in Visual Basic).

Remarks

You can manipulate how the text is drawn by using one of the DrawText overloads that takes a TextFormatFlags parameter.

If font is nullNothingnullptra null reference (Nothing in Visual Basic) 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. If forecolor is Transparent, the text will not be drawn.

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.

NoteNote:

The DrawText methods that specify a 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 DrawText methods that specify a Rectangle for the bounds of the drawn text.

Examples

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

Visual Basic
Private Sub RenderText1(ByVal e As PaintEventArgs)
    TextRenderer.DrawText(e.Graphics, "Regular Text", _
        Me.Font, New Point(10, 10), SystemColors.ControlText)

End Sub


C#
private void RenderText1(PaintEventArgs e)
{
    TextRenderer.DrawText(e.Graphics, "Regular Text", this.Font, 
        new Point(10, 10), SystemColors.ControlText);

}
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0
See Also

Reference

Tags :


Page view tracker