1. set font from file
Assume there is a font file "ladderkk.ttf" under work dir and declare member variable 「CFont m_font」 in class private section。
C++:
-
if (AddFontResource("ladderkk.ttf") == 0) {
-
AfxMessageBox("add font fail");
-
return FALSE;
-
}
-
LOGFONT lf;
-
memset(&lf, 0, sizeof(LOGFONT)); // Clear out structure.
-
lf.lfHeight = 20; // Request a 20-pixel-high font
-
-
strcpy(lf.lfFaceName, "LadderKK"); // with face name "Arial".
-
m_font.CreateFontIndirect(&lf); // Create the font.
-
-
GetDlgItem(IDC_EDIT1)->SetFont(&m_font);
2. set font from resource
in your rc file , add
4 FONT "ladderkk.ttf"
in your stdafx.h add
#define _WIN32_WINNT 0x0500
#define WINVER 0x0500
in your cpp file
C++:
-
HINSTANCE h=AfxGetInstanceHandle();
-
ULONG nFont = 1;
-
-
HRSRC hr=FindResource(h, "#4" , RT_FONT);
-
HGLOBAL hg=LoadResource(h,hr);
-
LPSTR lp=(LPSTR)LockResource(hg);
-
DWORD dwSize = SizeofResource(h, hr);
-
-
HANDLE handle = AddFontMemResourceEx(lp, dwSize, 0, &nFont);
-
-
// TODO: Add extra initialization here
-
LOGFONT lf;
-
memset(&lf, 0, sizeof(LOGFONT)); // Clear out structure.
-
lf.lfHeight = 20; // Request a 20-pixel-high font
-
-
strcpy(lf.lfFaceName, "LadderKK"); // with face name "Arial".
-
m_font.CreateFontIndirect(&lf); // Create the font.
-
-
GetDlgItem(IDC_EDIT1)->SetFont(&m_font);
Attension
- if no define 「_WIN32_WINNT」and 「WINVER」, vc6 compiler can't find 「AddFontMemResourceEx」
- When exiting the app, call 「RemoveFontResource」or 「RemoveFontMemResourceEx」