The Font Class in GDI+ using VB.NET

In this article I will explain about Font Class in GDI+.
  • 2219
The font class combines a font and methods and properties to define functionalities such as a font size, style, name and conversion. Table 5.8 describes the properties of the Font class.
 
The Following code creates a font object of font family Arial with size 16 and uses the Font class properties to find out the details of the Font object.

Dim arialFont As New Font(" Arial ", 16, FontStyle.Bold Or FontStyle.Underline Or FontStyle.Italic)
MessageBox.Show(((((" Font Properties = Name: " & arialFont.Name & " Size: ") + arialFont.Size.ToString() & " Style :") + arialFont.Style.ToString() & " Default Unit:") + arialFont.Unit.ToString() & "Size in points:") + arialFont.SizeInPoints.ToString())
 

The Font class provide three static methods: FromHdc, FromHfont, and FromLogFont. These methods create a Font object from a window handle to a device context, a window handle and GDI LOGFONT structure, respectively. The GetHeight method returns the height of a Font object. The ToHfont and ToLogFont methods convert a Font object to window handler and GDI LOGFONT structure, respectively.
 
TABLE 5.8 Font properties
 

Property

Description

Bold

Return true if the font is bold.

FontFamily

Every font belongs to s font family. This property return the FontFamily object associated with a Font object.

GdiCharSet

Return a string containing all characters.

GdiVerticalFont

Returns true if a font is derived from a GDI vertical font; Otherwise return false.

Height

Return the height of a font.

Italic

Return true if font is italic.

Name

Return the face name off font.

Size

Return the em size of a font in font design units.

SizeInPoints

Return size, in points of a font.

Strickout

Return true if a font specifies a horizontal line through the font.

Style

Return style information for a font, which is a type FontStyle enumeration.

Underline

Return true if font is underlined.

Unit

Return the unit of measure for a font.


In the following example, you must import the GDI library by adding the following code at the beginning of your class before using any GDI fonts, because we will be using GetStockObject:

    <System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")> _
    Private Shared Function GetStockobject(ByVal fnobj As Integer) As IntPtrn
    End Function

Listing 5.7 creates a font from a GDI handle and draws a string on the form. The FromHfont method creates a Font object from a GDI handle.
 
LISTING 5.7 Using the FromHfont method 

   Private Sub FromHfontmenu_Click(ByVal sender As Object, ByVal e As System.EventArgs)

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

        'Create a brush
        Dim brush As New SolidBrush(Color.Red)

        'Get a handle
        Dim hFont__1 As Intptr = GetStockobject(o)

        'Create a font from the handle A

        Dim hfontFont As Font = Font.FromHfont(hfont)

        'Draw text
        g.DrawString("GDI HFONT", hfontFont, brush, 20, 20)

        'Dipose of objects
        hfontFont.Dispose()
        g.Dispose()
    End Sub

Figure 5.12 shows the output from Listing 5.7


Figure-5_12.jpg
 
FIGURE 5.12 Using the FromHFont method

Conclusion

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

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.