在 Windows CE 上创建和使用字体
我面临着创建字体(除了 Windows 文件夹中默认找到的字体之外)的问题,并使用此字体在屏幕上绘制一些文本。
我所做的只是将此字体添加到 windows 文件夹,然后在代码中,我使用 CreateFontIndirect 函数创建字体,传递所需的 LOGFONT 结构,该结构具有适当的变量,尤其是 lfFaceName 变量,然后选择此字体到设备上下文中,最后绘制一些字符串,但没有任何效果,唯一真正起作用的字体是 arial、cour、times 和 tahoma,这是默认系统字体,即使使用这些默认字体,您也无法真正掌握它们之间的真正区别。
那么有没有什么具体的方法可以实现呢?或者由于 Windows CE 的某些限制,这件事无法完成,或者到底是什么?
谢谢你们,非常感谢任何帮助:)
更新:为了清楚起见,这里有一些代码。 。 。
BOOL OnInitDialog()
{
. . . .
if (AddFontResource(L"\\windows\\spaceage.ttf") == 1)
{
OutputDebugString(L"Font added successfully");
::SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
}
. . . .
}
void OnPaint()
{
CPaintDC dc(this);
HDC hdcMem = ::CreateCompatibleDC(dc);
HBITMAP hbmpMem = ::CreateCompatibleBitmap(dc, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
HBITMAP hbmpMemOld = (HBITMAP)::SelectObject(hdcMem, hbmpMem);
int iPointSize = 12;
wstring strFontName = L"spaceage";
HFONT hFontOld;
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT));
int iFontHeight = -1 * (iPointSize * ::GetDeviceCaps(hdcMem, LOGPIXELSY) / 72);
memset(&lf, 0, sizeof(LOGFONT));
lf.lfHeight = iFontHeight;
lf.lfWeight = FW_NORMAL;
lstrcpy(lf.lfFaceName, m_strFontName.c_str());
HFONT hFont = ::CreateFontIndirect(&lf);
if(hFont != NULL)
{
hFontOld = (HFONT)::SelectObject(hdcMem, hFont);
}
. . . . .
// do some stuff here
// draw some text here
. . . . .
::BitBlt(dc, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), hdcMem, 0, 0, SRCCOPY);
::SelectFont(hdcMem, hOldFont);
::SelectObject(hdcMem, hbmpMemOld);
::DeleteObject(hbmpMem);
::DeleteDC(hdcMem);
}
I am facing this problem of creating a font, other than the ones found by default in windows folder, and using this font to draw some text on the screen.
What I am doing is simply adding this font to the windows folder, then within the code, I create the font using CreateFontIndirect function, passing the required LOGFONT struct which have the appropriate variables especially the lfFaceName variable, then selecting this font into a device context and last thing drawing some strings, but nothing is working, the only fonts that really work are arial, cour, times and tahoma which is the default system font, and even when using these default fonts, you can't really grasp a real difference between them.
So is there any specific way to do that? or this thing can't be done due to some limitation in Windows CE, or what exactly?
Thank you guys and really appreciate any help :)
Update: here is some code for the sake of clarity . . .
BOOL OnInitDialog()
{
. . . .
if (AddFontResource(L"\\windows\\spaceage.ttf") == 1)
{
OutputDebugString(L"Font added successfully");
::SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
}
. . . .
}
void OnPaint()
{
CPaintDC dc(this);
HDC hdcMem = ::CreateCompatibleDC(dc);
HBITMAP hbmpMem = ::CreateCompatibleBitmap(dc, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
HBITMAP hbmpMemOld = (HBITMAP)::SelectObject(hdcMem, hbmpMem);
int iPointSize = 12;
wstring strFontName = L"spaceage";
HFONT hFontOld;
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT));
int iFontHeight = -1 * (iPointSize * ::GetDeviceCaps(hdcMem, LOGPIXELSY) / 72);
memset(&lf, 0, sizeof(LOGFONT));
lf.lfHeight = iFontHeight;
lf.lfWeight = FW_NORMAL;
lstrcpy(lf.lfFaceName, m_strFontName.c_str());
HFONT hFont = ::CreateFontIndirect(&lf);
if(hFont != NULL)
{
hFontOld = (HFONT)::SelectObject(hdcMem, hFont);
}
. . . . .
// do some stuff here
// draw some text here
. . . . .
::BitBlt(dc, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), hdcMem, 0, 0, SRCCOPY);
::SelectFont(hdcMem, hOldFont);
::SelectObject(hdcMem, hbmpMemOld);
::DeleteObject(hbmpMem);
::DeleteDC(hdcMem);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仅将字体放入 Fonts 文件夹中是不够的。 CE 不会像桌面操作系统那样将它们自动加载到文件副本上。您必须手动加载它。 此博客条目中概述了加载字体。
Just putting the Font in the Fonts folder isn't enough. CE doesn't auto-load them on a file copy like the desktop OS. You have to manually load it. Loading Fonts is outlined in this blog entry.
问题只是字体名称,我使用的是字体文件本身的名称,而不是字体名称,这确实不同,但我不知道。
我完全偶然发现了这一点,当我调试枚举设备上可用字体的代码时,我发现字体的文件名和字体名称之间存在差异,所以我尝试了由结构 ENUMLOGFONT 的成员变量 elfFullName ,英语字体一切正常,但阿拉伯字体不起作用:D
所以我想我会继续寻找为什么阿拉伯字体不起作用工作中,如果你们有任何建议,我真的很感激。
The problem was simply the font face name, I was using the name of the font file itself, rather than the font face name, which really differs and I didn't know that.
I discovered this by total chance, when I was debugging the code that enumerates the fonts available on the device, I saw that there is a difference between the file names of the fonts and the font face name, so I tried the face name given by the member variable elfFullName of the struct ENUMLOGFONT and everything went fine with English fonts, but Arabic fonts are not working :D
So I think I will continue looking why Arabic fonts are not working and if you have any suggestions guys, I really will appreciate them.