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
 
 
 
 
 
-->
TitleInstall a font
Description
Keywordsfonts, install font, create font
CategoriesUtilities, Windows, Graphics
 
By Andy Fielding.

It's not reliable to install a font by simply copying it to the user's Windows\Fonts folder. The system may require rebooting---or the user may even have to view the font via the Control Panel---before the font becomes active.

Instead, create a BAS (or CLS) module and add the following code to it. At run-time, copy the font to the user's Fonts folder, then call this sub.

 
Declare Function WriteProfileString Lib "Kernel" (ByVal _
    lpApplicationName As String, ByVal lpKeyName As String, _
    ByVal lpString As String) As Integer

Declare Function CreateScalableFontResource% Lib "GDI" _
    (ByVal fHidden%, ByVal lpszResourceFile$, ByVal _
    lpszFontFile$, ByVal lpszCurrentPath$) Declare Function _
    AddFontResource Lib "GDI" (ByVal lpFilename As Any) As _
    Integer

Declare Function SendMessage Lib "User" (ByVal hWnd As _
    Integer, ByVal wMsg As Integer, ByVal wParam As _
    Integer, lParam As Any) As Long
'  This sub installs a TrueType font and makes it available
' to
'  all Windows apps. It takes these arguments:
'
'  FontName$ is the font's name (e.g. "Goudy Old Style")
' 
'  FontFileName$ is the font's filename (e.g. "GOUDOS.TTF")
'
'  WinSysDir$ is the user's System folder (e.g.
' "C:\WINDOWS\SYSTEM"
'  or "C:\WINDOWS\SYSTEM32")
'
'  ** Before calling this sub, your code must copy the font
' file
'  to the user's Fonts folder. **
'
Sub Install_TTF (FontName$, FontFileName$, WinSysDir$)
Dim Ret%, Res&, FontPath$, FontRes$
Const WM_FONTCHANGE = &H1D
Const HWND_BROADCAST = &HFFFF

    FontPath$ = WinSysDir$ + "\" + FontFileName$
    FontRes$ = Left$(FontPath$, Len(FontPath$) - 3) + "FOT"

    Ret% = CreateScalableFontResource(0, FontRes$, _
        FontFileName$, WinSysDir$)
    Ret% = AddFontResource(FontRes$)
    Res& = SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0)
    Ret% = WriteProfileString("fonts", FontName + " " & _
        "(TrueType)", FontRes$)
End Sub
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated