Discussion:
CreateFont() never fails
(too old to reply)
Nobody
2011-09-16 01:23:51 UTC
Permalink
Why is it that CFont::CreateFont(), CFont::CreateFontIndirect(), and
CFont::CreatePointFont() never fail, even if the font parameters are
bad?
CFont f;
f.CreateFont(-12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "odsjfoerfj"));
Worse, the resulting font appears to be valid in every way. I can
select it into a DC and draw with it. It looks like Arial to me,
actually.
Is there any way to know whether or not a CFont was "really" created,
instead of just sort of half created like this?
That's normal. If it can't find the requested font, it would try to find a
closer match based on the other attributes. This is not clearly documented
in CFont::CreateFont, but in the Win32 API function CreateFont().

CreateFont function (Windows):
http://msdn.microsoft.com/en-us/library/dd183499%28VS.85%29.aspx

Quote: "Because available fonts can vary from system to system, do not
assume that the selected font is always the same as the requested font. For
example, if you request a font named Palatino, but no such font is available
on the system, the font mapper will substitute a font that has similar
attributes but a different name. Always report the name of the selected font
to the user."

If you want to know if a particular font exists, you need enumerate the
fonts:

EnumFonts function (Windows)
http://msdn.microsoft.com/en-us/library/dd162622%28v=VS.85%29.aspx
Scott Gregg
2011-09-17 00:31:08 UTC
Permalink
Why is it that CFont::CreateFont(), CFont::CreateFontIndirect(), and
CFont::CreatePointFont() never fail, even if the font parameters are
bad?
CFont f;
f.CreateFont(-12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "odsjfoerfj"));
Worse, the resulting font appears to be valid in every way. I can
select it into a DC and draw with it. It looks like Arial to me,
actually.
Is there any way to know whether or not a CFont was "really" created,
instead of just sort of half created like this?
Thanks.
How about CFont::AssertValid()?

Loading...