2010-06-12 108 views

回答

5

如果该字体在用户的系统上不可用,则可以在应用程序中嵌入字体并使用它。

您只需创建一个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 

我的两种字体我从资源的应用(60系列ZDigi,从我的诺基亚手机的字体和时代NR语音2,从我的字典应用程序的字体)使用装入专用字体集合在Sub New()
然后我打电话GetFontFamily方法来获得所需的字体绘制到窗体上。

把它应用到你的应用程序中应该不会太难。

干杯。

0

你不能。对于标准Winforms应用程序中的嵌入字体,目前没有跨平台标准。您可以做的最好的做法是嵌入个人,特定于操作系统的字体文件,检测您正在使用的操作系统,然后以编程方式安装字体。另一方面,VB.net中的WPF应用程序可能适合您的需求,但我有一种感觉,这不是你要去的地方。有关如何使用WPF应用程序打包字体的信息,请参阅this MSDN article

+1

我不想破坏你的泡泡,但**它可以完成**。它可能不适用于所有操作系统,但它可以在Win2k以上使用。看到我的答案是如何完成的。 – 2011-06-19 06:23:06

相关问题