ARTICLE

The FormatFlags Property in GDI+

Posted by Dinesh Beniwal Articles | GDI+ in VB.NET March 08, 2010
In this article I will explain about the FormatFlags Property in GDI+.
Download Files:
 
Reader Level:

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

Imports System
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
        g.Dispose()
    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.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.

share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Diagram
Become a Sponsor