ARTICLE

The Font Class in GDI+

Posted by Dinesh Beniwal Articles | GDI+ in VB.NET February 10, 2010
In this article I will explain about Font Class in GDI+.
 
Reader Level:

 

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.

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    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.
Nevron Diagram
Become a Sponsor