Tom
Tom

Reputation: 4577

How do I install a font on a user's machine in VB.NET so applications like Word can use it?

Need to install a font on all user's machines that will use the application I am writing which creates custom documents in Word. It's a barcode font.

I've successfully added the font to my solution and set its build action to Embedded Resource and have also successfully written code to check if the font is already installed on the user's system.

Now I just need to figure out how to extract the font from my solution and install it onto the user's machine as if they installed the font themselves for use in Office applications, etc.

Most of the examples I've found out there are for using the font within the VB.NET application instead of outside it and the one's I have found which seem to fit my purpose aren't working for me.

Upvotes: 6

Views: 19001

Answers (4)

Jared Knipp
Jared Knipp

Reputation: 5950

I used the installer project to install the fonts I needed with my application and followed this guide

Upvotes: 0

Fermin
Fermin

Reputation: 36111

First you copy the font to the windows font folder then call AddFontResource using p/invoke.

Here is an example, it's in C# but you should be able to work it out:

UPDATE

New URL

http://brutaldev.com/post/2009/03/26/Installing-and-removing-fonts-using-C.aspx

Upvotes: 3

Hans Olsson
Hans Olsson

Reputation: 55059

Copy the font into the Windows font folder and then you need to get the font added to the registry. I've not tried this myself, but I think it's possible to do this by opening the font using the ShellExecuteA api in a similar way to as seen here.

Here's a vbscript way of doing it that might be useful as a starting point since you might be able to use similar syntax and functions in VB.Net: Hey, Scripting Guy! How Can I Install Fonts Using a Script?

Upvotes: 2

Cody Gray
Cody Gray

Reputation: 244981

First, you need to copy the font to the Windows\Fonts directory (you'll want to make sure to use the Environment.GetFolderPath method provided by the .NET Framework instead of hard-coding the typical path to the Windows directory, just in case something is different in one of your users' environments).

Then, you need to call the AddFontResource function to add the font to the system font table. Since AddFontResource is provided by the Windows API, you'll need to P/Invoke to call it from VB.NET code. The declaration looks something like this (the lpszFilename parameter is the path to the font file that you want to add):

<DllImport("gdi32.dll"), CharSet := CharSet.Auto> _
Public Shared Function AddFontResource(ByVal lpszFilename As String) As Integer

Finally, if Word (or whatever application you intend to use the font in) is running at the time you call the AddFontResource function from your code, you need to inform it that the available fonts have changed. You do this by sending a WM_FONTCHANGE message to all top-level windows using the SendMessage function and setting the hWnd parameter toHWND_BROADCAST. Again, you'll need to P/Invoke; the declarations look like this:

Public Const HWND_BROADCAST As Integer = &HFFFF
Public Const WM_FONTCHANGE As Integer = &H1D

<DllImport("user32.dll"), CharSet := CharSet.Auto> _
Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer

And you would call it to send the broadcast message like this:

SendMessage(New IntPtr(HWND_BROADCAST), WM_FONTCHANGE, IntPtr.Zero, IntPtr.Zero)

NOTE: The above steps only install the font for the current Windows session. If you need the font to be available on subsequent restarts, you need to add it to the registry. The key to modify is this one:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts

Upvotes: 11

Related Questions