Discussion:
VB6 - use a custom, non-installed font?
(too old to reply)
Brigand
2011-12-08 14:53:21 UTC
Permalink
So I want to include a custom font with a program I am working on in
VB6. The program will simply be distributed as an executable in a
folder of the resources it uses - I really want to avoid forcing the
user to install the code.

So I want to have a form use a custom font that I include in the
folder. How do I do this at runtime?

I tried

Set frmMain.Font.Name = App.path & "\resources\customfont.ttf"

and other various permutations, with no luck. The only way I can get
it to work is to actually install the font in question on the users
machine, which is simply an extra level of unnecessary burden.

Any advice? Is it even possible to point to a custom font at runtime
and use it?

Thanks for you help!!
Thorsten Albers
2011-12-08 15:43:17 UTC
Permalink
Post by Brigand
Any advice? Is it even possible to point to a custom font at runtime
and use it?
Code - not tested, written online:

----- Common declarations section
Private Declare Function AddFontResourceEx _
Lib "gdi32" _
Alias "AddFontResourceExA" _
( _
ByVal sFileName As String, _
ByVal lFlags As Long, _
ByVal lReserved As Long _
) As Long

Private Declare Function RemoveFontResourceEx _
Lib "gdi32" -
Alias "RemoveFontResourceExA" _
( _
ByVal sFileName As String, _
ByVal lFlags As Long, _
ByVal lReserved As Long _
) As Long

Const FR_PRIVATE As Long = &H10

Dim gsFontPrev As String


----- In e.g. MyForm_Load:
Dim sMyFont As String
Dim lCountToAdd As Long, lCount As Long


sMyFont = "My wonderful truetype font"
lCountFonts = 1

lCount = AddFontResourceEx(App.path & "\resources\customfont.ttf", _
FR_PRIVATE, _
0))
If lCount <> lCountFonts Then

' Error: At least one font couldn't be added!

If lCount Then
Call RemoveFontResourceEx(App.path & "\resources\customfont.ttf", _
FR_PRIVATE, _
0))
End If

Else
gsFontPrev = frmMain.Font.Name
frmMain.Font.Name = sMyFont
End If


----- In e.g. MyForm_Unload:
Dim fSuccess As Boolean


If Len(gsFontPrev) Then

frmMain.Font.Name = gsFontPrev
gsFontPrev = vbNullString

fSuccess = CBool(RemoveFontResourceEx(App.path &
"\resources\customfont.ttf", _
FR_PRIVATE, _
0))
If Not fSuccess Then
' Error: At least one font couldn't be removed - presumably
' because it still is in use somewhere in you app
End If

End If
--
Thorsten Albers

gudea at gmx.de
Brigand
2011-12-08 18:28:35 UTC
Permalink
This appears to do exactly what I want. Thank you so much - it is very
appreciated :)
Brigand
2011-12-08 19:41:38 UTC
Permalink
Post by Brigand
This appears to do exactly what I want. Thank you so much - it is very
appreciated :)
Actually, its missing something. How is sMyFont associated with the
font you just added? To reference the font in VB, you use
frmMain.Font.Name = sMyFont, but I dont see how you are assigned the
name to the added resource. The code just assigns the name of the font
to a variable and does nothing with it. When I run the code, it adds
the font, but then loads the first one in the list.
Mike Williams
2011-12-08 21:31:37 UTC
Permalink
[In response to code posted by Thorsten Albers] Actually, its missing
something.
something. The code just assigns the name of the font to a variable and
does
nothing with it.
You need to specify the filename of the font in the call to
AddFontResourceEx but you need to use the actual name of the font itself
when you assign it to your Form. The filename is often the same as the font
name, but this is not always the case. You clearly already know the filename
of the .ttf file and I assume that you also know the font name contained in
that file, so you just need to make sure you use the correct names in both
cases. For example, if the filename is "CustomFont.ttf" (and if it resides
in a folder called Resources within whatever folder your compiled exe lives
in, as would appear from your initial post) and if its actual font name is
"Boulders" then you would need something like the following:

Option Explicit
Private Declare Function AddFontResourceEx Lib "gdi32.dll" _
Alias "AddFontResourceExA" (ByVal lpcstr As String, _
ByVal dword As Long, ByRef DESIGNVECTOR) As Long
Private Const FR_PRIVATE As Long = &H10

Private Sub Form_Load()
AddFontResourceEx App.Path & "\Resources\CustomFont.ttf", FR_PRIVATE, 0&
End Sub

Private Sub Command1_Click()
Me.Font.Name = "Boulders"
Me.Font.Size = 36
Me.Print "This should be in Boulders font"
End Sub

I'm fairly sure that you actually do know the name of the font contained in
your CustomFont.ttf file, but if you don't then you can easily find it by
double clicking the file so that Windows opens it. Otherwise, you can if you
wish add code to extract the actual name from the .ttf file at run time for
you. It's highly uinlikely that you will need that, but post again if you
do.

Mike
Brigand
2011-12-09 14:42:20 UTC
Permalink
That makes sense; I mistakenly assumed you were assigning a name to
reference the font you just loaded, not actually referring to the font
by it's creator's name. And double clicking does indeed give me the
correct name to reference, so no need for further code.

Thank you both for your help, I have been struggling to come up with a
solution to thei problem for a while, and this works perfecly. Now if
I can just find a square terminal font....
Thorsten Albers
2011-12-09 22:08:59 UTC
Permalink
Post by Brigand
That makes sense; I mistakenly assumed you were assigning a name to
reference the font you just loaded, not actually referring to the font
by it's creator's name. And double clicking does indeed give me the
correct name to reference, so no need for further code.
Thank you both for your help, I have been struggling to come up with a
solution to thei problem for a while, and this works perfecly. Now if
I can just find a square terminal font....
This sounds to me as if you only need a screen font and not a font for
printing. AddFontResourceEx() is able to add *.FON/*.FNT screen (AKA
raster) fonts. *.FNT screen fonts can be edited with FontEdit which is
shipped with VB 6/VS 6 (you have to search on the CDs). With this it should
be possible for you to make your own font.
--
Thorsten Albers

gudea at gmx.de
Jim Mack
2011-12-10 01:14:11 UTC
Permalink
Now if I can just find a square terminal font....
Will this do?

http://www.searchfreefonts.com/free/video-terminal-screen.htm
--
Jim
Thorsten Albers
2011-12-09 17:43:36 UTC
Permalink
Post by Brigand
That makes sense; I mistakenly assumed you were assigning a name to
reference the font you just loaded, not actually referring to the font
by it's creator's name. And double clicking does indeed give me the
correct name to reference, so no need for further code.
Thank you both for your help, I have been struggling to come up with a
solution to thei problem for a while, and this works perfecly. Now if
I can just find a square terminal font....
This sounds to me as if you only need a screen font and not a font for
printing. AddFontResourceEx() is able to add *.FON/*.FNT screen (AKA
raster) fonts. *.FNT screen fonts can be edited with FontEdit which is
shipped with VB 6/VS 6 (you have to search on the CDs). With this it should
be possible for you to make your own font.
--
Thorsten Albers

gudea at gmx.de


.
Loading...