views:

42

answers:

1

When creating a control (e.g. an edit control) on the fly using CreateWindow, it usually starts out with an ugly (boldish sans serif) font.

Usually I wok around that by grabbing the parent dialog's font, and setting it to the control - I can't even say if this is a good idea.

How do I "legally" fetch the right font?

+3  A: 

The "correct" way to get the font used in dialog boxes like message boxes, etc. would be via the SystemParametersInfo() function:

// C++ example
NONCLIENTMETRICS metrics;
metrics.cbSize = sizeof(NONCLIENTMETRICS);
::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS),
    &metrics, 0);
HFONT font = ::CreateFontIndirect(&metrics.lfMessageFont);
::SendMessage(ctrlHWND, WM_SETFONT, (WPARAM)font, MAKELPARAM(TRUE, 0));

Don't forget to destroy the font when the controls are destroyed:

::DeleteObject(font);

You can look up the MSDN documentation for NONCLIENTMETRICS and SystemParametersInfo() to see what other system-wide parameters you can retrieve.

In silico
Thanks!(I'm putting it into a CHandleRef, no chance to forget a delete! - http://www.codeproject.com/KB/stl/boostsp_handleref.aspx)
peterchen