¿Cómo encontrar si el sistema tiene la fuente que necesitaba en MFC?
Pregunta
Quiero escribir la siguiente función
bool IsFontExistInSystem(const CString& fontStyle) const
{
}
¿Hay alguna API en Windows para hacer esto? ¡Muchas gracias!
Solución
Aquí hay un código antiguo que busqué para verificar si hay una fuente instalada. Podría ser ordenado pero tienes la 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;
}
Otros consejos
Puede usar EnumFontFamiliesEx
para determinar si existe una fuente real.
UPD: Acabo de enterarme de que MS me recomienda usar EnumFontFamiliesEx en lugar de EnumFontFamilies.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow