RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Master .NET's Text Tricks : Page 2

Learn how to use the Graphics object's methods to draw text in different fonts—clipped, wrapped, aligned, stretched, and rotated in all sorts of ways.

Aligning Text
The previous examples passed the DrawString method X and Y coordinates to mark the upper left corner of the drawn text (that's the way Visual Basic 6 worked, too) but you can also pass a Point instead of separate X and Y values, or you can pass a Rectangle in which DrawString will draw the text.

Unfortunately passing the upper left corner's coordinates into the routine makes it hard to align text. For example, if you want to right-justify text, you would need to figure out how wide the string would be when drawn and then set the X coordinate to the right margin minus the string's width. To center the text, you would need to subtract half of the string's drawn width from the coordinate where you wanted the text centered. You would need to perform similar calculations to align or center the text vertically.

While many of us have been performing these calculations for years, the DrawString method provides an easier way to align text. Several overloaded versions of DrawString take a StringFormat object as a parameter. The StringFormat object's Alignment and LineAlignment properties determine the string's horizontal and vertical alignment respectively. You can set Alignment and LineAlignment to Near, Center, or Far to make DrawGraphics put the text on the left/top, center, or right/bottom of the starting point.

Figure 3. Alignment Using StringFormat: The StringFormat object determines how text is aligned with respect to its reference point.
As an example, the following code draws three lines of text centered horizontally. It starts by defining X and Y coordinates for a point and calling FillEllipse so you can see the text's reference point. Next it creates a StringFormat object in a Using statement, setting its Alignment property to Center to center the text horizontally, and its LineAlignment property to Near to draw the text below the point. Finally it calls DrawString, passing it parameters similar to those used in the previous examples with the StringFormat object added at the end. Figure 3 shows the result.

   Dim x As Integer = 150
   Dim y As Integer = 20
   e.Graphics.FillEllipse(Brushes.Red, x - 2, y - 2, 5, 5)
   Using string_format As New StringFormat()
      string_format.Alignment = StringAlignment.Center
      string_format.LineAlignment = StringAlignment.Near
      e.Graphics.DrawString( _
         "Visual Basic" & vbCrLf & _
         "Makes string formatting easy" & vbCrLf & _
         "C# does also", _
         Me.Font, Brushes.Blue, x, y, string_format)
   End Using
Perhaps a more intuitive way to draw text is to pass DrawString a formatting rectangle rather than X and Y coordinates or a Point. Then you can use the StringFormat object's Alignment and LineAlignment properties to control the text's position within the rectangle. The following code draws text aligned in the lower right corner of a rectangle with upper left corner (10, 10) that is 175 pixels wide and 70 pixels tall.

Figure 4. Alignment Using Rectangles: The StringFormat object can align text within a formatting rectangle.
   layout_rect = New Rectangle(10, 10, 175, 70)
   string_format.Alignment = StringAlignment.Far
   string_format.LineAlignment = StringAlignment.Far
   e.Graphics.DrawString("Near,Near", Me.Font, _
       Brushes.Blue, layout_rect, string_format)
You'll find the complete code in the example program StringAlignment in the downloadable code. Figure 4 shows the results of drawing sample strings showing each of the nine combinations of Alignment and LineAlignment values. The sample text gives the Alignment and LineAlignment values. For example, the text in the upper right was drawn with Alignment = Far and LineAlignment = Near.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date