我想写下面这个函数

bool IsFontExistInSystem(const CString& fontStyle) const
{

}

Windows中是否有任何API可以执行此操作? 非常感谢!

有帮助吗?

解决方案

这是我挖出的一些旧代码,它将检查是否安装了字体。它可以通过整理来实现,但你明白了:

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

其他提示

您可以使用 EnumFontFamiliesEx 查找是否存在实际字体。

UPD:我刚刚了解到MS建议使用EnumFontFamiliesEx而不是EnumFontFamilies。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top