Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

vb.net - How can I embed font in Visual Basic .Net application?

How can I embed font in Visual Basic .Net application? which should valid at every operating system.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is possible to embed a font in an application and use it if that font is not available on the user's system.

You simply create a PrivateFontCollection and populate it with your fonts then you can use them as you please. According to MSDN, this method does not apply to operating systems before Windows 2000.

From Remarks section of PrivateFontCollection.AddFontFile method:

When using a private font on operating systems before Windows 2000, the default font, typically Microsoft Sans Serif, will be substituted.

If you intend your application to be used on Windows 2000 and newer, you can follow this code I wrote to see how to implement private fonts.

Public Class Form1
    Dim pfc As System.Drawing.Text.PrivateFontCollection
    Dim ifc As System.Drawing.Text.InstalledFontCollection

    Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        pfc = New System.Drawing.Text.PrivateFontCollection()
        ifc = New System.Drawing.Text.InstalledFontCollection()

        LoadPrivateFonts({My.Resources.Series_60_ZDigi, My.Resources.Times_NR_Phonetics_2})
    End Sub

    ''' <summary>Loads the private fonts.</summary>
    ''' <param name="fonts">The fonts to be loaded into the private font collection.</param>
    Private Sub LoadPrivateFonts(ByVal fonts As IEnumerable(Of Byte()))
        For Each resFont In fonts
            pfc.AddMemoryFont(Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(resFont, 0), resFont.Length)
        Next
    End Sub

    ''' <summary>Gets the FontFamily whose name matches the one specified.</summary>
    ''' <param name="fontName">Name of the FontFamily to be returned.</param>
    ''' <param name="defaultFamily">
    ''' Optional. The default font family to be returned if the specified font is not found
    ''' </param>
    Private Function GetFontFamily(ByVal fontName As String, Optional ByVal defaultFamily As FontFamily = Nothing) As FontFamily
        If String.IsNullOrEmpty(fontName) Then
            Throw New ArgumentNullException("fontName", "The name of the font cannont be null.")
        End If

        Dim foundFonts = From font In ifc.Families.Union(pfc.Families) Where font.Name.ToLower() = fontName.ToLower()

        If foundFonts.Any() Then
            Return foundFonts.First()
        Else
            Return If(defaultFamily, FontFamily.GenericSansSerif)
        End If
    End Function

    Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        'free the resources used by the font collections
        pfc.Dispose()
        ifc.Dispose()
    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim g = e.Graphics

        Using br As Brush = Brushes.Black
            g.DrawString("1234567890ABCDEF", New Font(GetFontFamily("Series 60 ZDigi"), 18), br, New Point(20, 20))
            g.DrawString("ABCDEFGHIJKLMNOP", New Font(GetFontFamily("Times NR Phonetics 2"), 18), br, New Point(20, 100))
        End Using
    End Sub
End Class

I load the two fonts I use in the application (Series 60 ZDigi, a font from my Nokia phone, and Times NR Phonetics 2, a font from my dictionary application) from the resources into the private font collection in the Sub New().
I then call the GetFontFamily method to get the desired font to paint onto the form.

It shouldn't be too hard to incorporate this into your applications.

Cheers.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...