Redigeeri

Jagamisviis:


TextRenderer.DrawText Method

Definition

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

Overloads

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Point, Color)

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

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

DrawText(IDeviceContext, String, Font, Point, Color, Color, TextFormatFlags)

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

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Point, Color, Color, TextFormatFlags)

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

DrawText(IDeviceContext, String, Font, Rectangle, Color, TextFormatFlags)

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

DrawText(IDeviceContext, String, Font, Rectangle, Color, Color)

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

DrawText(IDeviceContext, String, Font, Point, Color, TextFormatFlags)

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

DrawText(IDeviceContext, String, Font, Point, Color, Color)

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

DrawText(IDeviceContext, ReadOnlySpan<Char>, 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.

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Rectangle, Color, Color)

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

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Point, Color, TextFormatFlags)

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

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Point, Color, Color)

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

DrawText(IDeviceContext, String, Font, Rectangle, Color)

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

DrawText(IDeviceContext, String, Font, Point, Color)

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

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Rectangle, Color)

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

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Rectangle, Color, TextFormatFlags)

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

Remarks

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.

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Point, Color)

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

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::Point pt, System::Drawing::Color foreColor);
public static void DrawText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font font, System.Drawing.Point pt, System.Drawing.Color foreColor);
static member DrawText : System.Drawing.IDeviceContext * ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Point * System.Drawing.Color -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As ReadOnlySpan(Of Char), font As Font, pt As Point, foreColor As Color)

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
ReadOnlySpan<Char>

The text to draw.

font
Font

The Font to apply to the drawn text.

pt
Point

The Point that represents the upper-left corner of the drawn text.

foreColor
Color

The Color to apply to the drawn text.

Exceptions

dc is null.

Applies to

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

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Rectangle bounds, System::Drawing::Color foreColor, System::Drawing::Color backColor, System::Windows::Forms::TextFormatFlags flags);
public static void DrawText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor, System.Drawing.Color backColor, System.Windows.Forms.TextFormatFlags flags);
public static void DrawText (System.Drawing.IDeviceContext dc, string? text, System.Drawing.Font? font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor, System.Drawing.Color backColor, System.Windows.Forms.TextFormatFlags flags);
static member DrawText : System.Drawing.IDeviceContext * string * System.Drawing.Font * System.Drawing.Rectangle * System.Drawing.Color * System.Drawing.Color * System.Windows.Forms.TextFormatFlags -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As String, font As Font, bounds As Rectangle, foreColor As Color, backColor As Color, flags As TextFormatFlags)

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
String

The text to draw.

font
Font

The Font to apply to the drawn text.

bounds
Rectangle

The Rectangle that represents the bounds of the text.

foreColor
Color

The Color to apply to the text.

backColor
Color

The Color to apply to the area represented by bounds.

flags
TextFormatFlags

A bitwise combination of the TextFormatFlags values.

Exceptions

dc is null.

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 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);
}
Private Sub RenderText8(ByVal e As PaintEventArgs)
    Dim flags As TextFormatFlags = _
        TextFormatFlags.Bottom Or TextFormatFlags.WordBreak
    TextRenderer.DrawText(e.Graphics, _
        "This is some text that will display on multiple lines.", _
        Me.Font, New Rectangle(10, 10, 100, 50), _
        SystemColors.ControlText, SystemColors.ControlDark, flags)

End Sub

Remarks

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.

Applies to

DrawText(IDeviceContext, String, Font, Point, Color, Color, TextFormatFlags)

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

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Point pt, System::Drawing::Color foreColor, System::Drawing::Color backColor, System::Windows::Forms::TextFormatFlags flags);
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, System.Windows.Forms.TextFormatFlags flags);
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, System.Windows.Forms.TextFormatFlags flags);
static member DrawText : System.Drawing.IDeviceContext * string * System.Drawing.Font * System.Drawing.Point * System.Drawing.Color * System.Drawing.Color * System.Windows.Forms.TextFormatFlags -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As String, font As Font, pt As Point, foreColor As Color, backColor As Color, flags As TextFormatFlags)

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
String

The text to draw.

font
Font

The Font to apply to the drawn text.

pt
Point

The Point that represents the upper-left corner of the drawn text.

foreColor
Color

The Color to apply to the text.

backColor
Color

The Color to apply to the background area of the drawn text.

flags
TextFormatFlags

A bitwise combination of the TextFormatFlags values.

Exceptions

dc is null.

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 RenderText7 from the form's Paint event handler, passing e as PaintEventArgs.

private void RenderText7(PaintEventArgs e)
{
    TextRenderer.DrawText(e.Graphics, "This is some text.", this.Font,
        new Point(10, 10), Color.White, Color.SteelBlue, TextFormatFlags.Default);
}
Private Sub RenderText7(ByVal e As PaintEventArgs)
    TextRenderer.DrawText(e.Graphics, "This is some text.", _
        Me.Font, New Point(10, 10), Color.White, Color.SteelBlue, _
        TextFormatFlags.Default)

End Sub

Remarks

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

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.

Note

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.

Applies to

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Point, Color, Color, TextFormatFlags)

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

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::Point pt, System::Drawing::Color foreColor, System::Drawing::Color backColor, System::Windows::Forms::TextFormatFlags flags);
public static void DrawText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Point pt, System.Drawing.Color foreColor, System.Drawing.Color backColor, System.Windows.Forms.TextFormatFlags flags);
static member DrawText : System.Drawing.IDeviceContext * ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Point * System.Drawing.Color * System.Drawing.Color * System.Windows.Forms.TextFormatFlags -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As ReadOnlySpan(Of Char), font As Font, pt As Point, foreColor As Color, backColor As Color, flags As TextFormatFlags)

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
ReadOnlySpan<Char>

The text to draw.

font
Font

The Font to apply to the drawn text.

pt
Point

The Point that represents the upper-left corner of the drawn text.

foreColor
Color

The Color to apply to the drawn text.

backColor
Color

The Color to apply to the background area of the drawn text.

flags
TextFormatFlags

A bitwise combination of the TextFormatFlags values.

Exceptions

dc is null.

Applies to

DrawText(IDeviceContext, String, Font, Rectangle, Color, TextFormatFlags)

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

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Rectangle bounds, System::Drawing::Color foreColor, System::Windows::Forms::TextFormatFlags flags);
public static void DrawText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor, System.Windows.Forms.TextFormatFlags flags);
public static void DrawText (System.Drawing.IDeviceContext dc, string? text, System.Drawing.Font? font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor, System.Windows.Forms.TextFormatFlags flags);
static member DrawText : System.Drawing.IDeviceContext * string * System.Drawing.Font * System.Drawing.Rectangle * System.Drawing.Color * System.Windows.Forms.TextFormatFlags -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As String, font As Font, bounds As Rectangle, foreColor As Color, flags As TextFormatFlags)

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
String

The text to draw.

font
Font

The Font to apply to the drawn text.

bounds
Rectangle

The Rectangle that represents the bounds of the text.

foreColor
Color

The Color to apply to the drawn text.

flags
TextFormatFlags

A bitwise combination of the TextFormatFlags values.

Exceptions

dc is null.

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 RenderText6 from the form's Paint event handler, passing e as PaintEventArgs.

private void RenderText6(PaintEventArgs e)
{
    TextFormatFlags flags = TextFormatFlags.Bottom | TextFormatFlags.EndEllipsis;
    TextRenderer.DrawText(e.Graphics, "This is some text that will be clipped at the end.", this.Font,
        new Rectangle(10, 10, 100, 50), SystemColors.ControlText, flags);
}
Private Sub RenderText6(ByVal e As PaintEventArgs)
    Dim flags As TextFormatFlags = TextFormatFlags.Bottom Or _
        TextFormatFlags.EndEllipsis
    TextRenderer.DrawText(e.Graphics, _
    "This is some text that will be clipped at the end.", _
    Me.Font, New Rectangle(10, 10, 100, 50), SystemColors.ControlText, flags)

End Sub

Remarks

If font or color 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.

Applies to

DrawText(IDeviceContext, String, Font, Rectangle, Color, Color)

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

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Rectangle bounds, System::Drawing::Color foreColor, System::Drawing::Color backColor);
public static void DrawText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor, System.Drawing.Color backColor);
public static void DrawText (System.Drawing.IDeviceContext dc, string? text, System.Drawing.Font? font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor, System.Drawing.Color backColor);
static member DrawText : System.Drawing.IDeviceContext * string * System.Drawing.Font * System.Drawing.Rectangle * System.Drawing.Color * System.Drawing.Color -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As String, font As Font, bounds As Rectangle, foreColor As Color, backColor As Color)

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
String

The text to draw.

font
Font

The Font to apply to the drawn text.

bounds
Rectangle

The Rectangle that represents the bounds of the text.

foreColor
Color

The Color to apply to the drawn text.

backColor
Color

The Color to apply to the area represented by bounds.

Exceptions

dc is null.

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 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);
}
Private Sub RenderText4(ByVal e As PaintEventArgs)
    TextRenderer.DrawText(e.Graphics, "Regular Text.", _
        Me.Font, New Rectangle(10, 10, 70, 70), _
        SystemColors.ControlText, SystemColors.ControlDark)

End Sub

Remarks

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.

Applies to

DrawText(IDeviceContext, String, Font, Point, Color, TextFormatFlags)

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

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Point pt, System::Drawing::Color foreColor, System::Windows::Forms::TextFormatFlags flags);
public static void DrawText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Point pt, System.Drawing.Color foreColor, System.Windows.Forms.TextFormatFlags flags);
public static void DrawText (System.Drawing.IDeviceContext dc, string? text, System.Drawing.Font? font, System.Drawing.Point pt, System.Drawing.Color foreColor, System.Windows.Forms.TextFormatFlags flags);
static member DrawText : System.Drawing.IDeviceContext * string * System.Drawing.Font * System.Drawing.Point * System.Drawing.Color * System.Windows.Forms.TextFormatFlags -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As String, font As Font, pt As Point, foreColor As Color, flags As TextFormatFlags)

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
String

The text to draw.

font
Font

The Font to apply to the drawn text.

pt
Point

The Point that represents the upper-left corner of the drawn text.

foreColor
Color

The Color to apply to the drawn text.

flags
TextFormatFlags

A bitwise combination of the TextFormatFlags values.

Exceptions

dc is null.

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 RenderText5 from the form's Paint event handler, passing e as PaintEventArgs.

private void RenderText5(PaintEventArgs e)
{
    TextRenderer.DrawText(e.Graphics, "Some text.",
        this.Font, new Point(10, 10), SystemColors.ControlText, TextFormatFlags.Bottom);
}
Private Sub RenderText5(ByVal e As PaintEventArgs)
    TextRenderer.DrawText(e.Graphics, "Some text.", _
    Me.Font, New Point(10, 10), SystemColors.ControlText, _
    TextFormatFlags.Bottom)

End Sub

Remarks

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 backColor parameter is applied to the area within the bounds parameter. If font or forecolor is null or Color.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.

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.

Note

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.

Applies to

DrawText(IDeviceContext, String, Font, Point, Color, Color)

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

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Point pt, System::Drawing::Color foreColor, System::Drawing::Color backColor);
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);
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);
static member DrawText : System.Drawing.IDeviceContext * string * System.Drawing.Font * System.Drawing.Point * System.Drawing.Color * System.Drawing.Color -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As String, font As Font, pt As Point, foreColor As Color, backColor As Color)

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
String

The text to draw.

font
Font

The Font to apply to the drawn text.

pt
Point

The Point that represents the upper-left corner of the drawn text.

foreColor
Color

The Color to apply to the drawn text.

backColor
Color

The Color to apply to the background area of the drawn text.

Exceptions

dc is null.

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 RenderText3 from the form's Paint event handler, passing e as PaintEventArgs.

private void RenderText3(PaintEventArgs e)
{
    TextRenderer.DrawText(e.Graphics, "Regular Text", this.Font,
        new Point(10, 10), Color.Red, Color.PowderBlue);
}
Private Sub RenderText3(ByVal e As PaintEventArgs)
    TextRenderer.DrawText(e.Graphics, "Regular Text", Me.Font, _
        New Point(10, 10), Color.Red, Color.PowderBlue)

End Sub

Remarks

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

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.

Note

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.

Applies to

DrawText(IDeviceContext, ReadOnlySpan<Char>, 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.

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::Rectangle bounds, System::Drawing::Color foreColor, System::Drawing::Color backColor, System::Windows::Forms::TextFormatFlags flags);
public static void DrawText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor, System.Drawing.Color backColor, System.Windows.Forms.TextFormatFlags flags);
static member DrawText : System.Drawing.IDeviceContext * ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Rectangle * System.Drawing.Color * System.Drawing.Color * System.Windows.Forms.TextFormatFlags -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As ReadOnlySpan(Of Char), font As Font, bounds As Rectangle, foreColor As Color, backColor As Color, flags As TextFormatFlags)

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
ReadOnlySpan<Char>

The text to draw.

font
Font

The Font to apply to the drawn text.

bounds
Rectangle

The Rectangle that represents the bounds of the text.

foreColor
Color

The Color to apply to the drawn text.

backColor
Color

The Color to apply to the background area of the drawn text.

flags
TextFormatFlags

A bitwise combination of the TextFormatFlags values.

Exceptions

dc is null.

Applies to

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Rectangle, Color, Color)

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

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::Rectangle bounds, System::Drawing::Color foreColor, System::Drawing::Color backColor);
public static void DrawText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor, System.Drawing.Color backColor);
static member DrawText : System.Drawing.IDeviceContext * ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Rectangle * System.Drawing.Color * System.Drawing.Color -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As ReadOnlySpan(Of Char), font As Font, bounds As Rectangle, foreColor As Color, backColor As Color)

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
ReadOnlySpan<Char>

The text to draw.

font
Font

The Font to apply to the drawn text.

bounds
Rectangle

The Rectangle that represents the bounds of the text.

foreColor
Color

The Color to apply to the drawn text.

backColor
Color

The Color to apply to the background area of the drawn text.

Exceptions

dc is null.

Applies to

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Point, Color, TextFormatFlags)

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

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::Point pt, System::Drawing::Color foreColor, System::Windows::Forms::TextFormatFlags flags);
public static void DrawText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Point pt, System.Drawing.Color foreColor, System.Windows.Forms.TextFormatFlags flags);
static member DrawText : System.Drawing.IDeviceContext * ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Point * System.Drawing.Color * System.Windows.Forms.TextFormatFlags -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As ReadOnlySpan(Of Char), font As Font, pt As Point, foreColor As Color, flags As TextFormatFlags)

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
ReadOnlySpan<Char>

The text to draw.

font
Font

The Font to apply to the drawn text.

pt
Point

The Point that represents the upper-left corner of the drawn text.

foreColor
Color

The Color to apply to the drawn text.

flags
TextFormatFlags

A bitwise combination of the TextFormatFlags values.

Exceptions

dc is null.

Applies to

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Point, Color, Color)

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

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::Point pt, System::Drawing::Color foreColor, System::Drawing::Color backColor);
public static void DrawText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font font, System.Drawing.Point pt, System.Drawing.Color foreColor, System.Drawing.Color backColor);
static member DrawText : System.Drawing.IDeviceContext * ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Point * System.Drawing.Color * System.Drawing.Color -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As ReadOnlySpan(Of Char), font As Font, pt As Point, foreColor As Color, backColor As Color)

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
ReadOnlySpan<Char>

The text to draw.

font
Font

The Font to apply to the drawn text.

pt
Point

The Point that represents the upper-left corner of the drawn text.

foreColor
Color

The Color to apply to the drawn text.

backColor
Color

The Color to apply to the background area of the drawn text.

Exceptions

dc is null.

Applies to

DrawText(IDeviceContext, String, Font, Rectangle, Color)

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

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Rectangle bounds, System::Drawing::Color foreColor);
public static void DrawText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor);
public static void DrawText (System.Drawing.IDeviceContext dc, string? text, System.Drawing.Font? font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor);
static member DrawText : System.Drawing.IDeviceContext * string * System.Drawing.Font * System.Drawing.Rectangle * System.Drawing.Color -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As String, font As Font, bounds As Rectangle, foreColor As Color)

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
String

The text to draw.

font
Font

The Font to apply to the drawn text.

bounds
Rectangle

The Rectangle that represents the bounds of the text.

foreColor
Color

The Color to apply to the drawn text.

Exceptions

dc is null.

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 RenderText2 from the form's Paint event handler, passing e as PaintEventArgs.

private void RenderText2(PaintEventArgs e)
{
    TextRenderer.DrawText(e.Graphics, "Regular Text", this.Font,
        new Rectangle(10, 10, 100, 100), SystemColors.ControlText);
}
Private Sub RenderText2(ByVal e As PaintEventArgs)
    TextRenderer.DrawText(e.Graphics, "Regular Text", _
        Me.Font, New Rectangle(10, 10, 100, 100), _
        SystemColors.ControlText)

End Sub

Remarks

If font or forecolor is null or Color.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.

See also

Applies to

DrawText(IDeviceContext, String, Font, Point, Color)

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

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Point pt, System::Drawing::Color foreColor);
public static void DrawText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Point pt, System.Drawing.Color foreColor);
public static void DrawText (System.Drawing.IDeviceContext dc, string? text, System.Drawing.Font? font, System.Drawing.Point pt, System.Drawing.Color foreColor);
static member DrawText : System.Drawing.IDeviceContext * string * System.Drawing.Font * System.Drawing.Point * System.Drawing.Color -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As String, font As Font, pt As Point, foreColor As Color)

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
String

The text to draw.

font
Font

The Font to apply to the drawn text.

pt
Point

The Point that represents the upper-left corner of the drawn text.

foreColor
Color

The Color to apply to the drawn text.

Exceptions

dc is null.

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.

private void RenderText1(PaintEventArgs e)
{
    TextRenderer.DrawText(e.Graphics, "Regular Text", this.Font, 
        new Point(10, 10), SystemColors.ControlText);
}
Private Sub RenderText1(ByVal e As PaintEventArgs)
    TextRenderer.DrawText(e.Graphics, "Regular Text", _
        Me.Font, New Point(10, 10), SystemColors.ControlText)

End Sub

Remarks

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

If font is null 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.

Note

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.

See also

Applies to

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Rectangle, Color)

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

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::Rectangle bounds, System::Drawing::Color foreColor);
public static void DrawText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor);
static member DrawText : System.Drawing.IDeviceContext * ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Rectangle * System.Drawing.Color -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As ReadOnlySpan(Of Char), font As Font, bounds As Rectangle, foreColor As Color)

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
ReadOnlySpan<Char>

The text to draw.

font
Font

The Font to apply to the drawn text.

bounds
Rectangle

The Rectangle that represents the bounds of the text.

foreColor
Color

The Color to apply to the drawn text.

Exceptions

dc is null.

Applies to

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Rectangle, Color, TextFormatFlags)

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

public:
 static void DrawText(System::Drawing::IDeviceContext ^ dc, ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::Rectangle bounds, System::Drawing::Color foreColor, System::Windows::Forms::TextFormatFlags flags);
public static void DrawText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor, System.Windows.Forms.TextFormatFlags flags);
static member DrawText : System.Drawing.IDeviceContext * ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Rectangle * System.Drawing.Color * System.Windows.Forms.TextFormatFlags -> unit
Public Shared Sub DrawText (dc As IDeviceContext, text As ReadOnlySpan(Of Char), font As Font, bounds As Rectangle, foreColor As Color, flags As TextFormatFlags)

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
ReadOnlySpan<Char>

The text to draw.

font
Font

The Font to apply to the drawn text.

bounds
Rectangle

The Rectangle that represents the bounds of the text.

foreColor
Color

The Color to apply to the drawn text.

flags
TextFormatFlags

A bitwise combination of the TextFormatFlags values.

Exceptions

dc is null.

Applies to