How to set default font for all the windows in a Win32 Application? How to set default font for all the windows in a Win32 Application? windows windows

How to set default font for all the windows in a Win32 Application?


Implement this:

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

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

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

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

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


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.


Yes you can !

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