Creating and using a font on Windows CE
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 Crea开发者_StackOverflow中文版teFontIndirect 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);
}
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.
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.
精彩评论