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?

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

x
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.

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:
>>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

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
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
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.