* 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
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 |
|
|
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)
-
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)
-
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.