String Format Constructors

StringFormat()

StringFormat(StringFormat strfmt) StringFormat(StringFormatFlags sff)

StringFormat(StringFormatFlags sff, int iLanguage)

The second version essentially clones an existing StringFormat object; the third and fourth versions create a StringFormat object based on a combination of StringFormatFlags enumeration values. The StringFormatFlags enumeration is also used in setting the FormatFlags property of StringFormat: StringFormat Properties (selection)

The second version essentially clones an existing StringFormat object; the third and fourth versions create a StringFormat object based on a combination of StringFormatFlags enumeration values. The StringFormatFlags enumeration is also used in setting the FormatFlags property of StringFormat: StringFormat Properties (selection)

Type

Property

Accessibility

StringFormatFlags

FormatFlags

get/set

The StringFormatFlags enumeration is a series of bit flags:

StringFormatFlags Enumeration

Member

Value

DirectionRightToLeft

1

DirectionVertical

2

FitBlackBox

4

DisplayFormatControl

32

NoFontFallback

1024

MeasureTrailingSpaces

2048

NoWrap

4096

LineLimit

8192

NoClip

16384

I discuss some of these flags in this book in connection with certain programs. I use MeasureTrailingSpaces in the TypeAway program in Chapter 6, and in the BoldAndItalicTighter program coming up shortly. I demonstrate NoWrap and NoClip in the TrimmingTheText program later in this chapter.

When you create a new StringFormat object using the default constructor, the FormatFlags property is set to 0. Notice that these enumeration values are single bits, so you can combine them with the C# logical OR operator. For example,

StringFormat strfmt = new StringFormat(StringFormatFlags.DirectionVertical

StringFormatFlags.NoClip);

When you're setting the FormatFlags property, I strongly recommend that you get into the habit of using the |= operator:

strfmt.FormatFlags |= StringFormatFlags.NoWrap;

That way, you'll never accidentally turn off one of the other flags that you may have set earlier.

Besides using one of the constructors, you can obtain a StringFormat object by using one of the following static properties: StringFormat Static Properties

Type

Property

Accessibility

StringFormat

GenericDefault

get

StringFormat

GenericTypographic

get

If you examine the properties of the StringFormat objects returned by these static properties, you'll discover that GenericDefault returns a StringFormat object that is the same as that created by the default constructor. The GenericTypographic property returns an object that has the FitBlackBox, LineLimit, and NoClip flags set as well as a different value for the Trimming property, which I'll discuss later in this chapter.

However, the StringFormat object that GenericTypographic returns has an additional effect on DrawString and MeasureString that is not revealed by the public StringFormat properties and flags. This additional effect is what I'll tackle next.

+1 0

Post a comment