Windows application use only those fonts which are registered into the registry and copied to the Fonts directory. But what if you have to use font for your application which is not installed yet.
This code will show you how you can add font entry in to windows font table which is just temperory entry.
To run this demo -> Create standard exe project -> Add one Label control, textbox and command button on the form |
Click here to copy the following block | Private Declare Function AddFontResource Lib "gdi32" Alias "AddFontResourceA" (ByVal lpFileName As String) As Long Private Declare Function RemoveFontResource Lib "gdi32" Alias "RemoveFontResourceA" (ByVal lpFileName As String) As Long Dim AppPath As String Private Declare Function CreateScalableFontResource Lib "gdi32" _ Alias "CreateScalableFontResourceA" _ (ByVal fHidden As Long, _ ByVal lpszResourceFile As String, _ ByVal lpszFontFile As String, _ ByVal lpszCurrentPath As String) As Long
Public Function GetFontName(FileNameTTF As String) As String Dim hFile As Integer Dim Buffer As String Dim FontName As String Dim TempName As String Dim iPos As Integer TempName = App.Path & "\~TEMP.FOT"
If CreateScalableFontResource(1, _ TempName, _ FileNameTTF, _ vbNullString) Then hFile = FreeFile
Open TempName For Binary Access Read As hFile
Buffer = Space(LOF(hFile)) Get hFile, , Buffer iPos = InStr(Buffer, "FONTRES:") + 8 FontName = Mid(Buffer, iPos, _ InStr(iPos, Buffer, vbNullChar) - iPos)
Close hFile
Kill TempName End If GetFontName = FontName End Function
Private Sub Command1_Click() AddFontResource Text1 Label1 = GetFontName(Text1) Me.AutoRedraw = True Me.FontName = lblName Me.FontSize = 15 Me.FontBold = True Me.CurrentX = 1000 Me.CurrentY = Me.Height / 2 - 1000 Me.Print Label1 & " font demo" End Sub
Private Sub Form_Load() AppPath = App.Path If Right$(AppPath, 1) <> "\" Then AppPath = AppPath + "\" txtFontPath = AppPath + "abduction2002.ttf" End Sub Private Sub Form_Unload(Cancel As Integer) ret = RemoveFontResource(txtFontPath) End Sub |
|