Clipping and Wrapping Text
The StringFormat object's
FormatFlags property controls how
DrawString wraps and clips text within a formatting rectangle. Table 1 shows the
FormatFlags values that you can use to control clipping and wrapping.
 | |
Figure 5. Clipping and Wrapping Text: The StringFormat object's FormatFlags property determines how text is wrapped and clipped. |
Table 1. FormatFlags Values: The table lists the possible FormatFlags values and provides a brief description of the way each value affects text output. |
Value |
Result |
FitBlackBox |
Lines are wrapped normally. Lines that extend below the formatting rectangle are clippedso they may be only partially drawn. |
LineLimit |
Most lines are wrapped normally. If the text contains more lines than will fit, the final line is clipped on a character boundary so a partial word may be visible. |
NoClip |
Lines are wrapped as usual and may continue below the bottom of the formatting rectangle. |
NoWrap |
The text is not wrapped. Instead, the method clips any lines that extend beyond the edge of the formatting rectangle. |
Figure 5 shows the effects of the
FormatFlags values.
The StringFormat object's
Trimming property also plays a role in how DrawString clips text. The property accepts a StringTrimming enumeration value. Table 2 lists the possible values of the StringTrimming enumeration.
 | |
Figure 6. Trimming Strings: Setting the StringFormat.Trimming property to EllipsisCharacter draws the text trimmed and adds an ellipsis at the end. |
Table 2. StringTrimming Enumeration Values: The table lists the possible StringTrimming values and provides a brief description of the way each value affects text output. |
Value |
Result |
Character |
Text is trimmed after the last character that fits. |
EllipsisCharacter |
Text is trimmed after the last character that fits and is then followed by an ellipsis. |
EllipsisPath |
Text in the middle of the string is replaced with an ellipsis to make the string fit. This is particularly useful for file paths where you want to show the beginning and end of the path. |
EllipsisWord |
Text is trimmed after the last word that fits and is then followed by an ellipsis. |
None |
Text is not trimmed. |
Word |
Text is trimmed after the last word that fits. |
For example, the following code makes the StringFormat object clip the text after the last word that will fit and add an ellipsis at the end.
string_format.Trimming = StringTrimming.EllipsisWord
The example program
Ellipses demonstrates each of the
StringTrimming values.
Figure 6 shows the sample program output with
StringFormat.Trimming set to
EllipsisCharacter.