2010-06-12 104 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

相關問題