Click here to Skip to main content
15,742,709 members
Articles / Multimedia / GDI
Posted 7 Dec 1999


22 bookmarked

How to Set a Font for All Child Windows

Rate me:
Please Sign up or sign in to vote.
4.62/5 (7 votes)
7 Dec 1999
A technique for changing the font for all child windows in your main application

This article shows how to change the font for all child windows using only one line with a callback function.

The idea is to create a callback function that is passed to Win32 API function ::EnumChildWindows(). Somewhere in the application, global CFont object is created. Pointer to this object is passed as a third argument in EnumChildWindows(). This argument is passed to a callback function together with a handle to a child window. Using these 2 arguments, the function changes the font for the child window.

Using this technique, it is very easy to:

  • Change the font for main frame window, its child views and a status bar from CMainFrame::OnCreate().
  • Change the font for all dialog controls from OnInitDialog().

The Callback function looks like this:

// lParam is a pointer to CFont object
BOOL __stdcall SetChildFont(HWND hwnd, LPARAM lparam)
   CFont *pFont = (CFont*)lparam;
   CWnd *pWnd = CWnd::FromHandle(hwnd);
   return TRUE;

This callback function is used from OnCreate() or OnInitDialog() in the following way:

// g_Font is an object of type CFont or derived from CFont
EnumChildWindows(m_hWnd, ::SetChildFont, &g_Font);


This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.

Written By
Web Developer SCA d.o.o.
Serbia Serbia
I am a cofounder of SCA Software, company that specializes in software for process control, visualization and communication. Programming for the last 10 years in C++, Delphi. Visual C++ for the last 6 years. Degree in Electronics Engineering and Telecommunications.

Comments and Discussions

QuestionCFrameWnd font? Pin
Spitfed25-Sep-06 4:17
Spitfed25-Sep-06 4:17 
Questionhow to change font size? Pin
Hua Hsin27-Apr-04 18:15
Hua Hsin27-Apr-04 18:15 
AnswerRe: how to change font size? Pin
ianw.moore26-Jul-04 23:03
ianw.moore26-Jul-04 23:03 
Basically, the 'lfHeight' value determines the size of the font so therefore in the main application class (in this case "SetFontColor.cpp") add to "void TApplication::createFont(void)" the size of font (e.g =36) to the line:
"m_nFontSize = metrics.lfStatusFont.lfHeight;"

Therefore: "m_nFontSize = metrics.lfStatusFont.lfHeight=36;" (for a font size that is ‘36’)

And that is it! Oh, apart from resizing the column bar which is now too small.Big Grin | :-D

Questionhow to set to the particular control? Pin
Hua Hsin27-Apr-04 17:00
Hua Hsin27-Apr-04 17:00 
GeneralMemory problems fixed, but now more problems Pin
Jason24-Oct-00 8:34
Jason24-Oct-00 8:34 
GeneralMemory leaks Pin
JasonH6-Oct-00 9:03
JasonH6-Oct-00 9:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.