如何在Visual Basic .Net应用程序中嵌入字体? [英] How can I embed font in Visual Basic .Net application?

查看:138
本文介绍了如何在Visual Basic .Net应用程序中嵌入字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Visual Basic .Net应用程序中嵌入字体?它应该在每个操作系统上都有效。

解决方案

如果字体是在用户系统上不可用。

您只需创建一个 PrivateFontCollection 并填充字体,然后您可以随意使用它们。根据 MSDN ,这种方法确实不适用于Windows 2000之前的操作系统。


部分 PrivateFontCollection.AddFontFile 方法:


在Windows 2000之前的操作系统上使用专用字体时,默认字体(通常为Microsoft Sans Serif)将被替换。

$ b如果你打算在Windows 2000及更新版本上使用你的应用程序,你可以按照我写的代码来看看如何实施私人字体。

 公共类Form1中
尺寸PFC作为System.Drawing.Text.PrivateFontCollection
尺寸ifc As System.Drawi ng.Text.InstalledFontCollection

Sub New()

'这个调用是设计者需要的。
InitializeComponent()

在InitializeComponent()调用之后添加任何初始化。
PFC =新System.Drawing.Text.PrivateFontCollection()
IFC =新System.Drawing.Text.InstalledFontCollection()

LoadPrivateFonts({My.Resources.Series_60_ZDigi,我.Resources.Times_NR_Phonetics_2})
结束Sub

'''< summary>载入专用字体。< / summary>
'''< param name =fonts>要加载到私人字体集合中的字体< / param>
私人小组LoadPrivateFonts(BYVAL字体为IEnumerable(字节()))
。对于每一个resFont在字体
pfc.AddMemoryFont(Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(resFont,0),resFont .Length)
Next
End Sub
$ b $''< summary>获取名称与指定名称相匹配的FontFamily< / summary>
'''< param name =fontName>要返回的FontFamily的名称< / param>
'''< param name =defaultFamily>
'''可选。如果找不到指定字体,则返回缺省字体族
'''< / param>
专用功能GetFontFamily(BYVAL的fontName作为字符串,可选BYVAL defaultFamily例如fontFamily = Nothing)作为的FontFamily
。如果String.IsNullOrEmpty(的fontName)然后
抛出新的ArgumentNullException( 的fontName,这个名字)
结束如果

Dim foundFonts =从字体在ifc.Families.Union(pfc.Families)其中font.Name.ToLower()= fontName。 ToLower将()

。如果foundFonts.Any(),然后
返回foundFonts.First()
,否则
返回如果(defaultFamily,FontFamily.GenericSansSerif)
end如果
端功能

私人小组Form1_Disposed(BYVAL发件人为对象,BYVALË作为System.EventArgs)把手Me.Disposed
释放由该字体的集合$所使用的资源b $ b pfc.Dispose()
ifc.Dispose()
结束子

私人小组Form1_Paint(BYVAL发件人为对象,BYVALË作为System.Windows.Forms的。 PaintEventArgs的)把手Me.Paint
尺寸克= e.Graphics

使用BR作为刷= Brushes.Black
g.DrawString( 1234567890ABCDEF,New字体(GetFontFamily( 60系列ZDigi ),18),BR,新点(20,20))
g.DrawString( ABCDEFGHIJKLMNOP,新字体(GetFontFamily( 时报NR语音2),18),BR,新Point(20,100))
End使用
End Sub
End Class

我从资源中将我在应用程序中使用的两种字体(我的诺基亚手机的60系列ZDigi和字体应用程序中的字体Times NR Phonetics 2)加载到 Sub New()

然后调用 GetFontFamily 方法来获取所需的字体到这个表单上。



把这个加入到你的应用程序中应该不会太难。

干杯。

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

解决方案

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.

这篇关于如何在Visual Basic .Net应用程序中嵌入字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆