שתף באמצעות


Installing fonts via vb.net

Question

Thursday, October 2, 2008 8:00 PM

I'm looking for code that installs true type fonts:

Language:  vb.net

Operating System:  Windows XP

I don't want to install the fonts as part of an installer/setup program.

All replies (3)

Wednesday, October 8, 2008 9:14 AM ✅Answered | 1 vote

msbair said:

I'm looking for code that installs True Type fonts:

Language:  vb.net

Operating System:  Windows XP

Hi msbair,

Welcome to MSDN forums!

Please check these similar issues: How to install Font in .NET?

How To Install Font in Windows

Installing Fonts in C#

Install Font with VB

Code sample:

Imports System.Runtime.InteropServices  

 

Public Class Form1  

 

    <DllImport("gdi32")> _  

    Public Shared Function AddFontResource(ByVal lpFileName As String) As Integer 

    End Function 

 

    <DllImport("user32.dll")> _  

Public Shared Function SendMessage(ByVal hWnd As Integer, ByVal Msg As UInteger, ByVal wParam As Integer, ByVal lParam As Integer) As Integer 

    End Function 

 

    <DllImport("kernel32.dll", SetLastError:=True)> _  

Shared Function WriteProfileString(ByVal lpszSection As String, ByVal lpszKeyName As String, ByVal lpszString As String) As Integer 

    End Function 

 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load  

        Dim Ret As Integer 

        Dim Res As Integer 

        Dim FontPath As String 

        Const WM_FONTCHANGE As Integer = &H1D  

        Const HWND_BROADCAST As Integer = &HFFFF  

        FontPath = WinFontDir & "\ & FontFileName  

        Ret = AddFontResource(FontPath)  

        Res = SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0)  

        Ret = WriteProfileString("fonts", FontName & " (TrueType)", FontFileName)  

    End Sub 

 

End Class 

Best regards,
Martin Xie


Friday, November 26, 2010 2:40 PM

Does this font install code require that the font file be copied to the windows font folder?

If so,

This VB.Net 2008 file copy statement on a windows 7 computer:

filecopy ("Brushsci.ttf","c:\winsows\fonts\Brushsci.ttf")

generates illegal function error = 5.


Friday, November 26, 2010 3:21 PM

Does this font install code require that the font file be copied to the windows font folder?

If so,

This VB.Net 2008 file copy statement on a windows 7 computer:

filecopy ("Brushsci.ttf","c:\winsows\fonts\Brushsci.ttf")

generates illegal function error = 5.

You cannot installed font to font folder by just do copy file. it has to be done using AddFontResources API as martin shown in his post.

kaymaf

CODE CONVERTER SITE

http://www.carlosag.net/Tools/CodeTranslator/.

http://www.developerfusion.com/tools/convert/csharp-to-vb/.