[ACCEPTED]-How to set default font for all the windows in a Win32 Application?-fonts

Accepted answer
Score: 22

Implement this:

    bool CALLBACK SetFont(HWND child, LPARAM font){
        SendMessage(child, WM_SETFONT, font, true);
        return true;

inside a separate file or 5 just in the main.cpp and then just run:

EnumChildWindows(hwnd, (WNDENUMPROC)SetFont, (LPARAM)GetStockObject(DEFAULT_GUI_FONT));

whenever 4 you want, for example in the WM_CREATE message, after 3 you've created all your child windows!

I 2 always have a SetFont.cpp and a SetFont.h in my win32 GUI application 1 solutions.

Score: 9

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

The Windows common control 11 window classes all respond to WM_SETFONT, which is 10 the standard window message for telling 9 a window what font you want it to use. When 8 you implement your own window classes (especially 7 new child control window classes), you should 6 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 5 dialog manager does some of this for you; when 4 instantiating a dialog template, the new 3 dialog's font is set to the font named in 2 the template, and the dialog sends WM_SETFONT all 1 of its child controls.

Score: 8

Yes you can !

HFONT defaultFont;
defaultFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
SendMessage(handlerControl, WM_SETFONT, WPARAM (defaultFont), TRUE); // Send this to each control


Score: 4

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

SendMessageToDescendants( WM_SETFONT, 
                          0 ); 
Score: 1

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

More Related questions