HOWTO: Add a Scalable Font to Windows from Visual Basic

ID: Q112672


The information in this article applies to:
  • Microsoft Visual Basic Standard and Professional Editions for Windows, version 3.0


SUMMARY

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:

  • CreateScalableFontResource


  • AddFontResource


  • WriteProfileString


  • SendMessage



MORE INFORMATION

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.

Step-by-Step Example

This example shows how to add a scalable font resource to Windows from Visual Basic.
  1. Start a new project in Visual Basic. Form1 is created by default.


  2. Add the following to the general declarations section of Form1:
    
        ' 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
     


  3. Add a command button (Command1) to Form1.


  4. Add the following code to the Command1_Click event:
    
        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
     


  5. Run the program. Click the Command1 button to end the program. If no errors occurred, you should now be able to see the font "Bookman Old Style Bold (TrueType)" listed in the font list for Windows. To look at this list, choose the Fonts Panel in the Windows Control Panel. You should see the name listed in the list of installed fonts.

    If you receive this error:
    Error Creating Scalable font:0
    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.


Keywords :
Version :
Platform :
Issue type :


Last Reviewed: June 11, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.