CreateFont with Default Font

Hello,

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?

Ingo
OcranaAsked:
Who is Participating?
 
pgnatyukCommented:
>>xFont->Create()
So:
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.

0
 
jkrCommented:
Sure you can, there's a default cnstructor that takes no arguments: http://msdn.microsoft.com/en-us/library/fzzwz7a7(VS.80).aspx ("CFont::CFont")
// Declare a CFont object.
CFont font;

Open in new window

0
 
OcranaAuthor Commented:
Hi,

sorry, I ment "create". Means
xFont->Create()

Thanks,

Ocrana
0
 
pgnatyukCommented:
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:

NONCLIENTMETRICS ncm = { sizeof(NONCLIENTMETRICS) };
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);
http://msdn.microsoft.com/en-us/library/ykds215s(v=VS.80).aspx

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

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


0
 
pgnatyukCommented:
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.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.