INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How-to

How to Install a barcode font at runtime under Windows XP by Pampers
Posted: 9 Jul 07

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

Back to Visual Basic (Microsoft) VB.NET FAQ Index
Back to Visual Basic (Microsoft) VB.NET Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close