MeasureString


The Graphics object’s MeasureString method returns a SizeF structure holding the string’s width and height drawn in a particular font. You can use that information to arrange the text and other drawn objects on the form.

The following code shows how a program might center text on its form. It starts by defining the text it will draw and the font it will use (in this case, a bold, 40-pixel-tall, Times New Roman font). Next, the program uses the Graphics object’s MeasureString method to get the string’s size in that font. It uses the size to determine where it needs to draw the text to center it, and then draws the text. The code then makes a Rectangle object using the text’s position and the size it got from MeasureString. It finishes by drawing the rectangle around the string.

  Private Sub Form1_Paint(ByVal sender As Object, _   ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint     Dim the_string As String = "MeasureString"     ' Define the font and text we will use.     Using the_font As New Font("Times New Roman", 40, _         FontStyle.Bold, GraphicsUnit.Pixel)         ' Get the text's size.         Dim string_size As SizeF = _             e.Graphics.MeasureString("MeasureString", the_font)         ' Draw the text centered on the form.         Dim x As Integer = (Me.ClientSize.Width - CInt(string_size.Width)) \ 2         Dim y As Integer = (Me.ClientSize.Height - CInt(string_size.Height)) \ 2         e.Graphics.DrawString(the_string, the_font, Brushes.Black, x, y)         ' Draw a rectangle around the text.         Dim string_rect As New Rectangle(x, y, _             CInt(string_size.Width), CInt(string_size.Height))         e.Graphics.DrawRectangle(Pens.Black, string_rect)     End Using End Sub 

Figure 22-9 shows the result. Notice that the rectangle includes some extra space above, below, and to the sides of the string. The section “Font Metrics” later in this chapter has more to say about this extra space.

image from book
Figure 22-9: You can use the Graphics object’s MeasureString method to see how big a string will be when drawn in a particular font.

Occasionally, it is useful to know where parts of a string will be drawn. For example, you might want to draw a box around certain words or know when the user has clicked on a particular letter.

The Graphics object provides a MeasureCharacterRanges method that returns an array of Regions representing the positions of ranges of characters within a string. To use MeasureCharacterRanges, the program must first create an array of CharacterRange objects defining the ranges of interest. It calls a StringFormat object’s SetMeasurableCharacterRanges method, passing it this array. Finally, it calls MeasureCharacterRanges.

The following code uses MeasureCharacterRanges to show the positions of all of the characters in a string. It begins by defining its text, layout rectangle, font, and StringFormat object as usual. It then creates an array of CharacterRange objects, one for each character in the string. It loops through this array, filling it with new CharacterRange objects, each of which represents a single character. When it has filled the array, the code passes it to the StringFormat object’s SetMeasurableCharacterRanges method. The program then calls the Graphics object’s MeasureCharacterRanges method to get Region objects representing the characters’ positions. It loops through this array, calling each Region object’s GetBounds method to convert the region into a RectangleF structure. It transforms the RectangleF into a Rectangle and draws it. Finally, the program draws the string.

  Private Sub Form1_Paint(ByVal sender As Object, _  ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint     Dim txt As String = "Great Galloping Giraffes"     Dim layout_rect As New RectangleF(0, 0, _         Me.ClientSize.Width, Me.ClientSize.Height)     e.Graphics.TextRenderingHint = _         System.Drawing.Text.TextRenderingHint.AntiAliasGridFit     Using the_font As New Font("Times New Roman", 50, _         FontStyle.Bold, GraphicsUnit.Pixel)         Using string_format As New StringFormat             string_format.LineAlignment = StringAlignment.Center             string_format.Alignment = StringAlignment.Center             ' Define an array of CharacterRange objects,              ' one for each character.             Dim character_ranges(txt.Length - 1) As CharacterRange             For i As Integer = 0 To txt.Length - 1                 character_ranges(i) = New CharacterRange(i, 1)             Next i             ' Set the ranges in the StringFormat object.             string_format.SetMeasurableCharacterRanges(character_ranges)             ' Get the character range regions.             Dim character_regions() As Region = _                 e.Graphics.MeasureCharacterRanges(txt, _                 the_font, layout_rect, string_format)             ' Draw each region's bounds             For Each rgn As Region In character_regions                 ' Convert the region into a Rectangle.                 Dim character_bounds As RectangleF = rgn.GetBounds(e.Graphics)                 Dim character_rect As Rectangle = _                     Rectangle.Round(character_bounds)                 ' Draw the bounds.                 e.Graphics.DrawRectangle(Pens.White, character_rect)             Next rgn             ' Draw the text.             e.Graphics.DrawString(txt, the_font, Brushes.Black, _                 layout_rect, string_format)         End Using ' string_format     End Using ' the_font End Sub 

Figure 22-10 shows the result.

image from book
Figure 22-10: The Graphics object’s MeasureCharacterRanges method shows where ranges of characters will be drawn in a string.

For some reason, the array of CharacterRange objects you pass to the SetMeasurableCharacterRanges method can hold at most 32 items. If the array is larger, SetMeasurableCharacterRanges raises an overflow error. Microsoft says this behavior is by design and they don’t plan to change it. If you need to measure the positions of individual characters in a longer string, you should break the string into pieces smaller than 32 characters, probably at word boundaries, and arrange the pieces yourself.

Note that the characters do not necessarily actually stay within their assigned regions. Depending on the font, they may stick out slightly. Figure 22-11 shows the program used in the previous example with an italic bold font.

image from book
Figure 22-11: Characters do not always stay within their assigned regions.

Many of the characters in this figure stray outside of their assigned regions. The ff pair is particularly shameless in its trespassing, overlapping both the previous and following characters.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net