本文介绍了如何嵌入在Visual Basic项目中使用的字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图嵌入一个字体,使用下面的代码,我发现从这个网站,但正如我试图建立,我不断遇到一个错误'DIGITALDREAMNARROW'是不是成员'资源'。
。任何人都可以帮助我应该这样做:
即使我已将字体添加到资源文件夹。有什么我失踪?
Imports System.Drawing.Text
Imports System.Runtime.InteropServices
模块CustomFont
'PRIVATE FONT COLLECTION来保存动态字体
私人_pfc作为PrivateFontCollection =没有
公共ReadOnly属性GetInstance(ByVal大小为单一,_
ByVal样式为FontStyle)作为字体
获取
'如果这是第一次得到一个实例
'从资源
加载字体如果_pfc没有那么LoadFont()
'返回一个新的字体对象基于大小和风格通过
返回n新字体(_pfc.Families(0),Size,style)
结束获取
结束属性
Private Sub LoadFont()
Try
'INIT字体集合
_pfc = New PrivateFontCollection
'字体资源的加载内存指针
Dim fontMemPointer As IntPtr = _
Marshal.AllocCoTaskMem(_
My.Resources.DIGITALDREAMNARROW.Length)
'将数据复制到内存位置
Marshal.Copy(My.Resources.DIGITALDREAMNARROW,_
0,fontMemPointer ,_
My.Resources.DIGITALDREAMNARROW.Length)
'将内存字体载入私有字体集合
_pfc.AddMemoryFont(fontMemPointer,_
My.Resources .DIGITALDREAMNARROW.Length)
'自由不记忆
Marshal.FreeCoTaskMem(fontMemPointer)
作为例外捕获
'错误L OADING字体。 HANDLE EXCEPTION HERE
结束尝试
结束Sub
结束模块
我下载了他的程序源代码,并且工作正常,但是当我在我的终端上重试时,出现错误。任何人都可以帮忙吗?
解决方案要将文件作为资源添加到项目中,请使用
- 在解决方案资源管理器或您的项目属性下的我的项目项目菜单项。
- 从项目属性中选择资源标签。您可以通过从添加资源下拉菜单中选择添加现有文件... 添加ttf文件,或者从Windows资源管理器中拖放。注意:如果您使用添加现有文件... 添加ttf资源,则需要更改过滤器以显示所有文件(* 。 *)。
()
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?
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?
解决方案
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 (*.*).
这篇关于如何嵌入在Visual Basic项目中使用的字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-03 07:30