The FormatFlags Property in GDI+


This article has been excerpted from book "Graphics Programming with GDI+".

The FormatFlags property is useful when an application needs to draw text strings in different layouts- such as drawing vertical text. FormatFlags takes a value of the StringFormatFlags enumeration. Table 5.11 describes the members of the StringFormatFlags enumeration.

Note: An application can apply more than one StringFormatFlags member by using bitwise combinations.

As Listing 5.11 shows, our sample code draws two strings. One string is drawn from right to left, and the other is vertical. Using FormatFlags is pretty simple. An application creates a StringFormat object in the DrawString method. Note that an application can use more than one instance of FormatFlags for the same StringFormat object.

TABLE 5.11: StringFormatFlags members

Member

Description

DirectionRightToLeft

Draws text right to left in a given rectangle using the DrawString method.

DirectionVertical

Draws vertical text in a given rectangle using the DrawString method. The default alignment is left (use the Alignment property to change the text alignment).

DisplayFormatControl

Causes control characters such as the paragraph mark to be shown in the output with a representative glyph.

FitBlackBox

Specifies that no part of any glyph will overhang the bounding rectangle.

LineLimit

Specifies that only complete lines will be laid out in the formatting rectangle.

MeasureTrailingSpaces

By default, the boundary rectangle returned by the MeaureString method excludes any space at the end of each line. Set this flag to include that space in the measurement.

NoClip

By default, clipping is on, which means that any text outside of the formatting rectangle is not displayed. NoClip disable clipping.

NoFontFallback

By default, if the specified font is not found, an alternative font will be used. NoFontFallback disables that option and displays an open square for the missing character(s).

NoWrap

By default, wrapping is on. NoWrap disable wrapping.

LISTING 5.11: Using FormatFlags to format string text

using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.IO;
using
System.Text;
using
System.Windows.Forms;

namespace
WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {

            //Create graphics object
            Graphics g = this.CreateGraphics();

            //Create a rectangle
            Rectangle rect = new Rectangle(the-formatflags-property-in-gdi, the-formatflags-property-in-gdi, 3the-formatflags-property-in-gdi, 2the-formatflags-property-in-gdi);

            //Create two StringFormat objects
            StringFormat strFormat1 = new StringFormat();
            StringFormat strFormat2 = new StringFormat();

            //Set format flags of StringFormat object
            //with direction right to left
            strFormat1.FormatFlags =
            StringFormatFlags.DirectionRightToLeft;

            //Set direction vertical
            strFormat2.FormatFlags =
            StringFormatFlags.DirectionVertical;

            //Set alignment
            strFormat2.Alignment = StringAlignment.Far;

            //Draw rectangle
            g.DrawRectangle(new Pen(Color.Blue), rect);
            string str = "Horizontal Text: This is horizontal"
                         + "text inside a rectangle";

            //Draw strings
            g.DrawString(str, new Font("Verdana", 10, FontStyle.Bold),
            new SolidBrush(Color.Green), rect, strFormat1);
            g.DrawString("Vertical: Text String", new Font("Arial", 14),
            new SolidBrush(Color.Red), rect, strFormat2);

            //Dispose of GDI+ objects
            g.Dispose();
        }
    }
}


Figure 5.16 shows the output from Listing 5.11. One text string is drawn from right to left (aligned right) in the drawing rectangle, and the other text string is drawn vertically on the left-hand side. An application can even use Alignment, Trimming, and other properties to align and trim text.

Note: Using the Alignment property will remove the effect of StringFormatFlags.DirectionRightToLeft;

Figure 5.16.gif

FIGURE 5.16: Using FormatFlags to draw vertical and right-to-left text

Conclusion


Hope the article would have helped you in understanding the FormatFlags Property in GDI+. Read other articles on GDI+ on the website.

bookGDI.jpg This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows.

Up Next
    Ebook Download
    View all
    Learn
    View all