Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find whether system has the font I needed in MFC?

I want to write the following function

bool IsFontExistInSystem(const CString& fontStyle) const
{

}

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

like image 348
user25749 Avatar asked Oct 26 '09 06:10

user25749


People also ask

How to use font in MFC?

We can select font size, font name, special styles, and text color. Like other GDI objects such as pen and brush, we need to create font with specific styles and select it into a DC in order to use it for outputting text. In MFC, the class that can be used to implement font is CFont.

How do I know what fonts I have?

The system fonts you have will largely depend on the device you're using and the version of the operating system. One of the more fun ways to check out your system fonts is to open up a design program such as Photoshop and simply go through your list of fonts.

What are Windows system fonts?

Windows system fonts are a familiar but often surprisingly divisive set of system fonts. As Windows was so dominant during the 1990s and 2000s, it seems almost everyone knows them.

How to change the system font in Windows 10 GUI?

However, there is no option in Windows 10 GUI for changing the system font. After Windows 7, the option to personalize the system font has been removed by Microsoft. But that won’t stop us from changing the system fonts.


2 Answers

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;
}
like image 140
Rob Avatar answered Sep 27 '22 22:09

Rob


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.

like image 27
Kirill V. Lyadvinsky Avatar answered Sep 27 '22 20:09

Kirill V. Lyadvinsky