Question

Je veux écrire la fonction suivante

bool IsFontExistInSystem(const CString& fontStyle) const
{

}

Existe-t-il une API dans Windows pour le faire? Merci beaucoup!

Était-ce utile?

La solution

Voici un ancien code que j'ai creusé pour vérifier si une police est installée. Cela pourrait être fait d’être rangé, mais vous avez l’idée:

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;
}

Autres conseils

Vous pouvez utiliser EnumFontFamiliesEx pour déterminer s’il existe une police réelle.

UPD: Je viens d’apprendre qu’il est recommandé par MS d’utiliser EnumFontFamiliesEx au lieu d’EnumFontFamilies.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top