Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleInteractively change a font's height, width, weight, and font name
DescriptionThis example shows how to interactively change a font's height, width, weight, and font name in Visual Basic 6. It uses the CreateFont API function to make the appropriate font.
Keywordsfont, text, CreateFont
CategoriesGraphics, API
 
When you change the font's height, width, weight, font name, or text, the appropriate control's event handler calls subroutine ShowFont. That routine calls the CustomFont function to make a new font. It uses SelectObject to install the font on the form, draws some text, uninstals the font, and uses DeleteObject to free the font's resources.
 
Private Sub ShowFont()
Dim wid As Long
Dim hgt As Long
Dim wgt As Long
Dim newfont As Long
Dim oldfont As Long

    On Error Resume Next

    ' Select the new font.
    wid = CLng(txtWidth.Text)
    hgt = CLng(txtHeight.Text)
    wgt = cboWeight.ItemData(cboWeight.ListIndex)
    newfont = CustomFont(hgt, wid, 0, 0, _
        wgt, False, False, False, cboFont.Text)
    oldfont = SelectObject(hdc, newfont)

    ' Display the text.
    Line (0, 0)-(1000, 1000), BackColor, BF
    CurrentX = 10
    CurrentY = cboFont.Height + cboFont.Top + 10
    Print txtText.Text
    Picture = Image

    ' Restore the original font.
    newfont = SelectObject(hdc, oldfont)

    ' Free font resources (important!)
    DeleteObject newfont
End Sub
 
Function CustomFont uses the CreateFont API function to make a new font and return the font's handle.
 
' Make a customized font and return its handle.
Private Function CustomFont(ByVal hgt As Long, ByVal wid As _
    Long, ByVal escapement As Long, ByVal orientation As _
    Long, ByVal wgt As Long, ByVal is_italic As Long, ByVal _
    is_underscored As Long, ByVal is_striken_out As Long, _
    ByVal face As String) As Long
Const CLIP_LH_ANGLES = 16   ' Needed for tilted fonts.

    CustomFont = CreateFont( _
        hgt, wid, escapement, orientation, wgt, _
        is_italic, is_underscored, is_striken_out, _
        0, 0, CLIP_LH_ANGLES, 0, 0, face)
End Function
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated