2

It is well known, that the win32 api CreateFont()-function does not guarantee the creation of the desired font. Unfortunatly it silently "fails" and returns the handle of a replacement font.

This is bad, because my application needs to know the success of CreateFont(). In order to check this I do the following, which works well:

// LOGFONT lf; ....
// lf.lfFaceName == "nameWanted"   // assign my desired font name

HFONT hFnt = ::CreateFontIndirect(&lf);
HDC dc = ::CreateDC("DISPLAY",0,0,0);
HFONT oldfont = (HFONT)::SelectObject(dc, _h);
char nameFound[32];
::GetTextFace(dc,32,nameFound);
::SelectObject(dc, oldfont);
::DeleteDC(dc);
::DeleteObject(hFont);

const bool isOk = _stricmp("nameWanted", nameFound) == 0;
if (!isOk) {
    // Font doesn't seem to be available.
    // Handle this case
    ...
}

However this looks like overkill to me and I wonder, whether there is a direct way to check the success of CreateFont(). Did I miss something ?

  • You could rather query whether the font will be created the way you want, by calling [`EnumFontFamiliesEx`](http://msdn.microsoft.com/en-us/library/dd162620.aspx) prior to creating the font. This allows you to implement custom logic that decides whether a font is considered a match or not. – IInspectable May 19 '14 at 10:37
  • 2
    If you [read this answer](http://stackoverflow.com/a/7193439/2065121) you'll see that the substitution doesn't occur when you create the font, only when you select it into a DC. So, your method is probably the "best" you can achieve. – Roger Rowland May 19 '14 at 12:18
  • @Roger: Your answer gives me the insights, why this behaviour makes sense. Thanks. – Christof Schardt May 20 '14 at 12:06

0 Answers0