Files
Andy De George c0ba284473 Initial winforms content migrated (#18)
* Merge winforms framework content to working branch (#14)

* Breadcrumb / TOC / Move net5 to net folder (#15)

* change path from net5 to net

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Add .net 5 winforms placeholder article (#16)

* added some metadata and adjusted net5 placeholder

* corrections

* corrections

* corrections

* Test1

* Swapping landing page vs concept

* fix links

* Fix links

* Fix desc
2020-09-01 16:26:21 -07:00

2.6 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
How to: Draw Text with GDI 03/30/2017
csharp
vb
GDI [Windows Forms], drawing text [Windows Forms]
text [Windows Forms], drawing with TextRenderer
drawing [Windows Forms], text
Windows Forms, drawing text with GDI
2a19fe5d-2ace-451c-94db-01cb1118ef7b

How to: Draw Text with GDI

With the xref:System.Windows.Forms.TextRenderer.DrawText%2A method in the xref:System.Windows.Forms.TextRenderer class, you can access GDI functionality for drawing text on a form or control. GDI text rendering typically offers better performance and more accurate text measuring than GDI+.

Note

The xref:System.Windows.Forms.TextRenderer.DrawText%2A methods of the xref:System.Windows.Forms.TextRenderer class are not supported for printing. When printing, always use the xref:System.Drawing.Graphics.DrawString%2A methods of the xref:System.Drawing.Graphics class.

Example

The following code example demonstrates how to draw text on multiple lines within a rectangle using the xref:System.Windows.Forms.TextRenderer.DrawText%2A method.

[!code-csharpSystem.Windows.Forms.TextRendererExamples#7] [!code-vbSystem.Windows.Forms.TextRendererExamples#7]

To render text with the xref:System.Windows.Forms.TextRenderer class, you need an xref:System.Drawing.IDeviceContext, such as a xref:System.Drawing.Graphics and a xref:System.Drawing.Font, a location to draw the text, and the color in which it should be drawn. Optionally, you can specify the text formatting by using the xref:System.Windows.Forms.TextFormatFlags enumeration.

For more information about obtaining a xref:System.Drawing.Graphics, see How to: Create Graphics Objects for Drawing. For more information about constructing a xref:System.Drawing.Font, see How to: Construct Font Families and Fonts.

Compiling the Code

The preceding code example is designed for use with Windows Forms, and it requires the xref:System.Windows.Forms.PaintEventArgs e, which is a parameter of xref:System.Windows.Forms.PaintEventHandler.

See also