[C# Helper]
Index Books FAQ Contact About Rod
[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[Beginning Software Engineering]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Align text drawn by DrawString in C#


This example shows how to use StringFormat objects and Rectangles to align text drawn in in C#.

This example's Paint event handler draws nine strings inside a rectangle, aligning them vertically and horizontally in the nine possible combinations of top/left, top/center, bottom/right, and so forth.

The StringFormat object's LineAlignment property determines how the text is aligned vertically. It can take the values Near (top), Center (middle), or Far (bottom).

The Alignment property determines how the text is aligned horizontally. It can take the same values Near (left), Center (middle), or Far (right).

(This is a good example of overusing a piece of code, in this case, an enumeration. Instead of creating separate enumerations Top/Center/Bottom and Left/Middle/Right, .NET uses the single StringAlignment enumeration to represent both. Mmicrosoft is trying to make a single enumeration serve two purposes and that makes it harder to figure out what the values mean. That and their names also makes it harder to remember which of Alignment and LineAlignment represents horizontal alignment and which represents vertical alignment. This would all be much more straightforward if Microsoft had used VerticalAlignment and HorizontalAlignment for the property names and separate enumerations for their values.)

The following code shows how the program draws its text aligned in the rectangle's upper left corner.

// Draw text aligned in various ways. private void Form1_Paint(object sender, PaintEventArgs e) { Rectangle rect = new Rectangle(5, 5, ClientSize.Width - 10, ClientSize.Height - 10); e.Graphics.DrawRectangle(Pens.Red, rect); using (Font font = new Font("Times New Roman", 16, GraphicsUnit.Pixel)) { using (StringFormat sf = new StringFormat()) { // Top. sf.LineAlignment = StringAlignment.Near; // Top/Left. sf.Alignment = StringAlignment.Near; e.Graphics.DrawString("Top/Left", font, Brushes.Black, rect, sf); ... } } }

This code creates a Rectangle slightly smaller than the form and draws it in red. It then creates a Font and a StringFormat object.

To draw the text at the top of the rectangle, the code sets the StringFormat object's LineAlignment property to Near. To draw the text in the upper left corner, it also sets the StringFormat's Alignment property to Near.

It then uses the Graphics object's DrawString method to draw the text "Top/Left" using the StringFormat object as a parameter.

The rest of the code is similar. It uses different Alignment and LineAlignment values to align text in various ways.

Download the example to experiment with it and to see additional details.

© 2009-2022 Rocky Mountain Computer Consulting, Inc. All rights reserved.