GDI+ FormatFlags Property in VB.NET

In this article I will explain about the FormatFlags Property in GDI+.
  • 2413

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




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


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).


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


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


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


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.


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


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).


By default, wrapping is on. NoWrap disable wrapping.

LISTING 5.11: Using FormatFlags to format string text

Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.IO
Imports System.Text
Imports System.Windows.Forms
Public Class Form1

    Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

        'Create graphics object
        Dim g As Graphics = Me.CreateGraphics()

        'Create a rectangle
        Dim rect As New Rectangle(50, 50, 350, 250)

        'Create two StringFormat objects
        Dim strFormat1 As New StringFormat()
        Dim strFormat2 As 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)
        Dim str As String = "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
    End Sub
End Class

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: Using FormatFlags to draw vertical and right-to-left text


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


More Articles

© 2020 DotNetHeaven. All rights reserved.