如何嵌入在Visual Basic项目中使用的字体?

时间:2012-03-06 18:45:30

标签: vb.net winforms visual-studio-2010 fonts embedded-resource

我正在尝试使用我在此站点中找到的以下代码嵌入字体,但是当我正在尝试构建时,我一直遇到错误'DIGITALDREAMNARROW' is not a member of 'Resources'.。任何人都可以帮助我应该这样做:

即使我将字体添加到Resources文件夹中。有什么我想念的吗?

http://zerosandtheone.com/blogs/vb/archive/2009/11/20/vb-net-include-a-font-as-an-embedded-resource-in-your-application.aspx

Imports System.Drawing.Text
Imports System.Runtime.InteropServices

Module CustomFont

'PRIVATE FONT COLLECTION TO HOLD THE DYNAMIC FONT
Private _pfc As PrivateFontCollection = Nothing


Public ReadOnly Property GetInstance(ByVal Size As Single, _
                                     ByVal style As FontStyle) As Font
    Get
        'IF THIS IS THE FIRST TIME GETTING AN INSTANCE
        'LOAD THE FONT FROM RESOURCES
        If _pfc Is Nothing Then LoadFont()

        'RETURN A NEW FONT OBJECT BASED ON THE SIZE AND STYLE PASSED IN
        Return New Font(_pfc.Families(0), Size, style)

    End Get
End Property

Private Sub LoadFont()
    Try
        'INIT THE FONT COLLECTION
        _pfc = New PrivateFontCollection

        'LOAD MEMORY POINTER FOR FONT RESOURCE
        Dim fontMemPointer As IntPtr = _
            Marshal.AllocCoTaskMem( _
            My.Resources.DIGITALDREAMNARROW.Length)

        'COPY THE DATA TO THE MEMORY LOCATION
        Marshal.Copy(My.Resources.DIGITALDREAMNARROW, _
                     0, fontMemPointer, _
                     My.Resources.DIGITALDREAMNARROW.Length)

        'LOAD THE MEMORY FONT INTO THE PRIVATE FONT COLLECTION
        _pfc.AddMemoryFont(fontMemPointer, _
                           My.Resources.DIGITALDREAMNARROW.Length)

        'FREE UNSAFE MEMORY
        Marshal.FreeCoTaskMem(fontMemPointer)
    Catch ex As Exception
        'ERROR LOADING FONT. HANDLE EXCEPTION HERE
    End Try

End Sub

End Module

我下载了他的程序源代码并且工作正常,但是当我在我的最后重试时,我得到了错误。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

要将文件作为资源添加到项目中,

  • 双击解决方案资源管理器中的我的项目项目菜单项下的项目属性。< / LI>
  • 从项目属性中选择资源标签。您可以通过从“添加资源”下拉菜单中选择添加现有文件... 来添加ttf文件,或者只是从Windows资源管理器中拖放。注意:如果您使用添加现有文件... 添加ttf资源,则需要更改过滤器以显示所有文件(* *)即可。

reference