我使用的是 Windows 8.1、Visual Studio 2017。
我正在我正在开发的 directx 项目中使用这种 Markdown 字体。
我用 AddFontResourceEx
加载它并使用D3DXCreateFont
为其创建字体.
当我点击“本地 Windows 调试器”时,一切正常,字体呈现。无论是在 Release模式还是 Debug模式。 当我执行任何可执行文件时,就会出现问题,无论是发布还是调试,它都不会呈现所述字体。
所以我去读了,我读了msdn上的文章,this one以及其他需要的时候。
我不认为我做错了什么,我的资源 View 如下所示:
,IDR_FONT1 看起来像这样:
该文件会自动加载到解决方案资源管理器中(我没有添加它,VS 从 Resource.rc 文件添加),如下所示:
具有这些特性:
我这样添加:
AddFontResourceEx("pricedown.ttf", FR_PRIVATE, 0);
this->createFont("Pricedown", 60, true, false);
createfont 是我添加字体的函数(精简后,它有数组):
bool D3D9Render::createFont(char *name, int size, bool bold, bool italic)
{
D3DXCreateFont(m_pD3dDev, size, 0, (bold) ? FW_BOLD : FW_NORMAL, 0, (italic) ? 1 : 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH, name, &m_pFont);
return true;
}
我正在将其编译为 x64 版本。
正如我所说,当我按“本地 Windows 调试器”(在任何模式下,包括 x64 版本)时,它会工作并呈现字体,但是当我转到 project/x64/Release
时,它会工作并呈现字体。 ,它只是不会渲染字体。甚至可执行文件的大小也足够了。
GetLastError
关于AddFontResource
是 2(ERROR_FILE_NOT_FOUND)
我做错了什么?
最佳答案
(阅读答案直到最后,否则你会浪费很多时间。)
我明白了。我读过 this博客文章。
Here is an example on how to use AddFontMemResourceEx on a font file embedded in the resource.
HINSTANCE hResInstance = AfxGetResourceHandle( ); //Read the edit
HRSRC res = FindResource(
hResInstance,
MAKEINTRESOURCE(IDR_MYFONT),
L"BINARY" //Read The Edit
);
if (res)
{
HGLOBAL mem = LoadResource(hResInstance, res);
void *data = LockResource(mem);
size_t len = SizeofResource(hResInstance, res);
DWORD nFonts;
m_fonthandle = AddFontMemResourceEx(
data, // font resource
len, // number of bytes in font resource
NULL, // Reserved. Must be 0.
&nFonts // number of fonts installed
);
if(m_fonthandle==0)
{
MessageBox(L"Font add fails", L"Error");
}
}
虽然你需要afxwin.h,并且来自here :
afxwin.h is MFC and MFC is not included in the free version of VC++ (Express Edition)
编辑:
您不需要使用AfxGetResourceHandle
(为什么您需要afxwin.h
),您可以简单地执行以下操作:
HINSTANCE hResInstance = (HINSTANCE)GetModuleHandle(NULL);
在FindResource
中,第三个参数应该是RT_FONT
,所以你会得到:
HRSRC res = FindResource(hResInstance, MAKEINTRESOURCE(IDR_FONT1), RT_FONT);
关于c++ - 使用字体资源编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44928781/