Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Install a barcode font at runtime under Windows XP

How-to

How to Install a barcode font at runtime under Windows XP

by  Pampers  Posted    (Edited  )
I needed a barcode font installed at runtime for my VB2005 project. This is the way/code that worked for me (from Peter Huang).
1. I downloaded a free true type (code 3 of 9), scalable barcode font at: http://www.idautomation.com/free.html
2. Added the barcode (IDAutomationHC39M.ttf) to my project (Add Existing Item)
3. Embeded the font in my project (select the font in your project, rightclick, choose Properties, set BuildAction to Embedded Resource).
4. Created a new Form with 2 buttons and a label.
5. Pasted the following code into the new form - alter the font path's if necesarry...


Code:
Private Declare Function CreateScalableFontResource Lib "gdi32" _
    Alias "CreateScalableFontResourceA" (ByVal fHidden As Integer, ByVal _
    lpszResourceFile As String, ByVal lpszFontFile As String, ByVal _
    lpszCurrentPath As String) As Integer

    Private Declare Function AddFontResource Lib "gdi32" Alias _
"AddFontResourceA" (ByVal lpFileName As String) As Integer

    Private Declare Function RemoveFontResource Lib "gdi32" Alias _
"RemoveFontResourceA" (ByVal lpFileName As String) As Integer

    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
ByVal lParam As Integer) As Integer

    Private Const WM_FONTCHANGE = &H1D

    Private Const HWND_BROADCAST = &HFFFF&

    Public Shared Sub InstallFonts()
        Dim fi As System.IO.FileInfo
        Try
            Dim res As Integer
            fi = New System.IO.FileInfo("IDAutomationHC39M.ttf")
            Dim fiTarget As New System.IO.FileInfo("C:\Windows\Fonts\IDAutomationHC39M.ttf")
            If fiTarget.Exists Then Return
            ' copy the font
            fi.CopyTo("C:\Windows\Fonts\IDAutomationHC39M.ttf")
            ' add the font
            res = CreateScalableFontResource(0, "c:\IDAutomationHC39M.fot", "c:\windows\fonts\IDAutomationHC39M.ttf", String.Empty)
            Dim rt As Integer = System.Runtime.InteropServices.Marshal.GetLastWin32Error()
            res = AddFontResource("C:\IDAutomationHC39M.ttf")
            If res > 0 Then
                ' alert all windows that a font was added
                SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Public Shared Sub UninstallFonts()
        RemoveFontResource("C:\Windows\Fonts\IDAutomationHC39M.ttf")
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        InstallFonts()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Label1.Font = New Font("IDAutomationHC39M", 25)
    End Sub
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top