簡體   English   中英

如何在Visual Basic .Net應用程序中嵌入字體?

[英]How can I embed font in Visual Basic .Net application?

如何在Visual Basic .Net應用程序中嵌入字體? 哪個應該適用於每個操作系統。

可以在應用程序中嵌入字體,如果該字體在用戶系統上不可用,則可以使用它。

您只需創建一個PrivateFontCollection並使用您的字體填充它,然后您可以隨意使用它們。 根據MSDN ,此方法不適用於Windows 2000之前的操作系統。

PrivateFontCollection.AddFontFile方法的備注部分:

在Windows 2000之前在操作系統上使用私有字體時,將替換默認字體,通常為Microsoft Sans Serif。

如果您打算在Windows 2000及更高版本上使用您的應用程序,您可以按照我編寫的代碼來查看如何實現私有字體。

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

我將我在應用程序中使用的兩種字體(Series 60 ZDigi,我的諾基亞手機中的字體,Times NR Phonetics 2,我的字典應用程序中的字體)從資源加載到Sub New()的私有字體集合中。
然后我調用GetFontFamily方法來獲取所需的字體以在表單上繪制。

將它合並到您的應用程序中應該不會太難。

干杯。

你不能。 此時,標准Winforms應用程序中的嵌入字體沒有跨平台標准。 您可以做的最好的事情是嵌入單獨的,特定於操作系統的字體文件,檢測您所在的操作系統,然后以編程方式安裝該字體。

另一方面,VB.net中的WPF應用程序可能會滿足您的需求,但我覺得這不是您要去的地方。 有關如何使用WPF應用程序打包字體的信息,請參閱此MSDN文章

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM