Files
docs-desktop/dotnet-desktop-guide/framework/winforms/advanced/how-to-align-drawn-text.md
T
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.1 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
How to: Align Drawn Text 03/30/2017
csharp
vb
text [Windows Forms], aligning
Windows Forms, aligning drawn text
83c10a81-1a90-4b5c-98aa-2c6c4b280079

How to: Align Drawn Text

When you perform custom drawing, you may often want to center drawn text on a form or control. You can easily align text drawn with the xref:System.Drawing.Graphics.DrawString%2A or xref:System.Windows.Forms.TextRenderer.DrawText%2A methods by creating the correct formatting object and setting the appropriate format flags.

To draw centered text with GDI+ (DrawString)

  1. Use a xref:System.Drawing.StringFormat with the appropriate xref:System.Drawing.Graphics.DrawString%2A method to specify centered text.

    [!code-csharpSystem.Drawing.AlignDrawnText#10] [!code-vbSystem.Drawing.AlignDrawnText#10]

To draw centered text with GDI (DrawText)

  1. Use the xref:System.Windows.Forms.TextFormatFlags enumeration for wrapping as well as vertically and horizontally centering text with the appropriate xref:System.Windows.Forms.TextRenderer.DrawText%2A method.

    [!code-csharpSystem.Drawing.AlignDrawnText#20] [!code-vbSystem.Drawing.AlignDrawnText#20]

Compiling the Code

The preceding code examples are designed for use with Windows Forms, and they require xref:System.Windows.Forms.PaintEventArgs e, which is a parameter of xref:System.Windows.Forms.PaintEventHandler.

See also