views:

849

answers:

4

I want all the controls (edit,list control, etc) in my application to be having the same font which is not the system default. How do i do this? Is there any Win32 API that sets the application default font?

A: 

You can set the font for each Dialog box through the resources view. Right click on a dialog (not on other control), select properties and the font option.

Nick D
+1  A: 

You can't, there is no way to do this for all controls at the same time. You'll need to set it through the resource editor, as was suggested before, or call SetFont() manually on every control.

Roel
+4  A: 

Windows does not provide any mechanism for an application-wide font. Each window class may have its own behavior for choosing a font to use by default. It may try to select the font used by Windows shell dialogs, or it may simply draw its text using the horrid bitmap 'system' font automatically selected into new DCs.

The Windows common control window classes all respond to WM_SETFONT, which is the standard window message for telling a window what font you want it to use. When you implement your own window classes (especially new child control window classes), you should also write a handler for WM_SETFONT:

  1. If your window class has any child windows, your WM_SETFONT handler should forward the message to each of them.
  2. If your window class does any custom drawing, make sure to save the HFONT you receive in your WM_SETFONT handler and select it into the DC you use when drawing your window.
  3. If your window class is used as a top-level window, it will need logic to choose its own font, since it will have no parent window to send it a WM_SETFONT message.

Note that the dialog manager does some of this for you; when instantiating a dialog template, the new dialog's font is set to the font named in the template, and the dialog sends WM_SETFONT all of its child controls.

Matthew Xavier
Do all Windows controls react to this message? Are there any exceptions you know of?
Canopus
I expect all controls provided by Microsoft will respect WM_SETFONT, as a control that does not is almost guaranteed to use a mismatched font in dialog boxes, which would have been caught quickly in such widely-used software. If you are using controls from other sources, then you are at the mercy of their developers' skill.
Matthew Xavier
A: 

A handy method for setting the font for all child windows in one call:

SendMessageToDescendants( WM_SETFONT, 
                          (WPARAM)m_fntDialogFont.GetSafeHandle(), 
                          0 );
Alan