views:

114

answers:

2

I want to write the following function

bool IsFontExistInSystem(const CString& fontStyle) const
{

}

Is there any API in windows to do this? Many Thanks!

+4  A: 

You could use EnumFontFamiliesEx to find whether exist actual font.

UPD: I've just learned that it is recommended by MS to use EnumFontFamiliesEx instead of EnumFontFamilies.

Kirill V. Lyadvinsky
Many thanks!But I think this API is designed poorly....
I hate it when you beat me. :[
GMan
@GMan, it is a symmetric process :)
Kirill V. Lyadvinsky
Actually, I think this function can return data, simply instead of provide a call_back function...Why dose the engineer give such a simple question complex design.... It is not comvenient to use at all.
+2  A: 

Here's some old code I dug out that will check if a font is installed. It could do with being tidied up but you get the 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;
}
Rob
Many thanks! It works!