CreateFont with Default Font


is it possible to create a CFont Object without the usage of a named font? I need to create a Font in a own component and I think the usage of a named font is the reason of some crashes people reported.
So, how I can generate a CFont in the constructor of my component with a "Default" font that is available on every system?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sure you can, there's a default cnstructor that takes no arguments: ("CFont::CFont")
// Declare a CFont object.
CFont font;

Open in new window

OcranaAuthor Commented:

sorry, I ment "create". Means


I think, CFont font declares a font.
If I understand the question, you can obtain the system font in this way:
HFONT hFont = ::GetStockObject(SYSTEM_FONT);

People say that SYSTEM_FONT and DEFAULT_GUI_FONT are outdated. But it still works and everybody uses this simple method. You do not need to call DeleteObject for the font handle if you use GetStockObject.

You can try this way instead:

SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, &ncm, 0);
hFont = CreateFontIndirect(&ncm.lfMessageFont);

You can make CFont from it if you need - CFont has FromHandle method:
 CFont* pFont = CFont::FromHandle(hFont);

And then draw whatever you need:
CClientDC dc(this);
CFont* pOldFont = dc.SelectObject(pFont);
dc.TextOut(5, 5, "Hello", 5);

GetObject allows to retrieve LOGFONT from HFONT and you can use this value for CreateFontIndirect.

HFONT hFont = ::GetStockObject(SYSTEM_FONT);
Is the answer. You get the default font that you can use and do not need to delete.
CFont, the MFC class, is a bit more than a simple wrapper for HFONT and few GDI font functions. This is an example, where you can use Win32 API directly without MFC. It's only my opinion.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
How you are going to use the font?
DrawText function?

If for a GUI control, so simply do not set any font and the control will have the same font as its parent window. If the parent window is a dialog, for example, this font is set in VS. By default it should be MS Shell... (something) - you can change it in VS.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.