Come scoprire se il sistema ha il font di cui avevo bisogno in MFC?
Domanda
Voglio scrivere la seguente funzione
bool IsFontExistInSystem(const CString& fontStyle) const
{
}
C'è qualche API in Windows per farlo? Molte grazie!
Soluzione
Ecco un vecchio codice che ho estratto che verificherà se è installato un font. Potrebbe avere a che fare con il riordino ma si ottiene l'idea:
static int CALLBACK CFontHelper::EnumFontFamExProc(ENUMLOGFONTEX* /*lpelfe*/, NEWTEXTMETRICEX* /*lpntme*/, int /*FontType*/, LPARAM lParam)
{
LPARAM* l = (LPARAM*)lParam;
*l = TRUE;
return TRUE;
}
bool Font::IsInstalled(LPCTSTR lpszFont)
{
// Get the screen DC
CDC dc;
if (!dc.CreateCompatibleDC(NULL))
{
return false;
}
LOGFONT lf = { 0 };
// Any character set will do
lf.lfCharSet = DEFAULT_CHARSET;
// Set the facename to check for
_tcscpy(lf.lfFaceName, lpszFont);
LPARAM lParam = 0;
// Enumerate fonts
::EnumFontFamiliesEx(dc.GetSafeHdc(), &lf, (FONTENUMPROC)EnumFontFamExProc, (LPARAM)&lParam, 0);
return lParam ? true : false;
}
Altri suggerimenti
È possibile utilizzare EnumFontFamiliesEx
per scoprire se esiste un carattere reale.
UPD: ho appena appreso che MS consiglia di utilizzare EnumFontFamiliesEx anziché EnumFontFamilies.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow