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



Question:

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?


Solution:1

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.


Solution:2

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.


Solution:3

Yes you can !

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


Solution:4

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

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


Solution:5

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.


Solution:6

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »