How to embed fonts for Use in a Visual Basic Project?

限于喜欢 提交于 2019-12-11 18:58:35

问题


I am trying to embed a font using the following code that I found from this site, however as I'm trying to build, I keep running into an error 'DIGITALDREAMNARROW' is not a member of 'Resources'.. Can anyone help with where I'm supposed to do this:

Even though I have the font added to a Resources folder. Is there something I'm missing?

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

I downloaded his program source code and it works fine but when I retry it on my end, I get errors. Can anyone help?


回答1:


To add a file as a resource to your project,

  • Double-click My Project in Solution Explorer or your project Properties under the Project menu item.
  • Select the Resources tab from your project Properties. You can add the ttf file by either choosing Add Existing File... from the Add Resources drop down menu, or just drag and drop from Windows Explorer. Note: if you add the ttf resource using Add Existing File..., you will need to change the filter to show All Files (*.*).

(reference)



来源:https://stackoverflow.com/questions/9589932/how-to-embed-fonts-for-use-in-a-visual-basic-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!