HOWTO: Add a Scalable Font to Windows from Visual BasicID: Q112672
|
This article describes how to add a scalable font resource to Windows
from Visual Basic for Windows by calling four Windows Application
Programming Interface (Windows API) functions:
To make changes to the Windows font table, call CreateScalableFontResource
to create a font resource file. Then call AddFontResource to add the
resource you just created to the Windows font table. Next, make it
permanent by calling WriteProfileString to make a change to the WIN.INI
file. Finally, call SendMessage to tell all applications currently running
that a change has been made to the file.
' Enter each of the following Declare statements on one, single line:
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 WriteProfileString Lib "Kernel"
(ByVal lpApplicationName As String, ByVal lpKeyName As String,
ByVal lpString As String) As Integer
Declare Function SendMessage Lib "User" (ByVal hWnd As Integer,
ByVal wMsg As Integer, ByVal wParam As Integer, lParam As Any)
As Long
Sub Command1_Click()
' Initialize variables for calls to APIs.
' Set name of font to show up in font list:
keyname$ = "Bookman Old Style Bold (TrueType)"
' Set name of font resource file:
font$ = "C:\WINDOWS\SYSTEM\BOOKOSB.FOT"
TTF_Font$ = "bookosb.ttf"
ResPath$ = "c:\WINDOWS\SYSTEM"
' Initialize variables for SendMessage call:
HWND_BROADCAST = &HFFFF
WM_FONTCHANGE = &H1D
' Create the font resource file:
result& = CreateScalableFontResource%(0, font$, TTF_Font$, ResPath$)
If result& Then
' Add resource to Windows font table:
result& = AddFontResource(font$)
If result& Then
' Make changes to WIN.INI to reflect new font:
result& = WriteProfileString("Fonts", keyname$, font$)
If result& Then
' Let other applications know of the change:
result& = SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0&)
Else
' Report error:
MsgBox "Error Adding Entry to Win.Ini: " + Format$(result&)
End If
Else
' Report error:
MsgBox "Error Adding Font: " + Format$(result&)
End If
Else
' Report error:
MsgBox "Error Creating Scalable font: " + Format$(result&)
End If
End Sub
it is because the font is already loaded. Go into the Font List in the Control Panel and remove the font "Bookman Old Style Bold (TrueType)." Then run the program again.Error Creating Scalable font:0
Keywords :
Version :
Platform :
Issue type :
Last Reviewed: June 11, 1999