Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 856
Tom Avatar asked Jan 18 '11 15:01

Tom


People also ask

How do I import a font into Visual Studio?

In your VS code editor Go to File > Preferences > settings and search font. Insert the name of the newly downloaded font("Fira Code") before other default fonts and voila!! you have successfully installed a new font for your vscode editor ...


1 Answers

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
like image 103
Cody Gray Avatar answered Oct 31 '22 19:10

Cody Gray