如何在Visual Basic .Net应用程序中嵌入字体?这应该在每个操作系统上都有效。如何在Visual Basic .Net应用程序中嵌入字体?
3
A
回答
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. Visual Basic .Net应用程序部署
- 2. 在.NET应用程序中嵌入/部署自定义字体
- 3. 如何嵌入用于Visual Basic项目的字体?
- 4. 在Visual Basic 2010中嵌入自定义字体fot文本框
- 5. Visual Basic 6:应用程序
- 6. 如何使用C#应用程序嵌入字体? (使用Visual Studio 2005)
- 7. 单击.NET中的Visual Basic应用程序中的按钮
- 8. 如何在.NET Compact Framework应用程序中嵌入广告?
- 9. 如何在.NET应用程序中嵌入IronPython
- 10. 如何在Visual Basic应用程序中添加t-sql代码
- 11. 如何在另一个android应用程序中嵌入android中使用字体?
- 12. 打印到Windows窗体应用程序中的文件--- Visual Basic
- 13. 如何在SQL窗体应用程序中嵌入SQLite?
- 14. 如何在.NET中控制字体DPI WinForms应用程序
- 15. 在React应用程序中嵌入字体的选项?
- 16. 在Visual Basic .NET中的AddressOf
- 17. 在VB6中嵌入ASP.NET应用程序
- 18. 在COM中引用未签名的.NET程序集(Visual Basic 6)
- 19. Visual Basic .NET调度线程
- 20. 云应用程序API对于Visual Basic?
- 21. Visual Basic应用程序动画文本
- 22. Visual Basic:强制应用程序停止
- 23. Visual Basic应用程序帮助
- 24. Visual Basic中MsgBox的粗体字体
- 25. 将mac字体嵌入到flex应用程序中
- 26. 问题,我的ASP.NET的Visual Basic Web窗体应用程序
- 27. Visual Basic窗体应用程序自定义关闭按钮
- 28. 在.net应用程序中嵌入流程图设计表面
- 29. 视频嵌入在Visual Basic上
- 30. 如何在Visual Basic .NET中声明Char文字?
我不想破坏你的泡泡,但**它可以完成**。它可能不适用于所有操作系统,但它可以在Win2k以上使用。看到我的答案是如何完成的。 – 2011-06-19 06:23:06