Click here to Skip to main content
Licence 
First Posted 7 Dec 1999
Views 68,584
Bookmarked 20 times

How to set a font for all child windows

By | 7 Dec 1999 | Article
A technique for changing the font for all child windows in your main application
  • Download demo project - 15 Kb
  • 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);
       pWnd->SetFont(pFont);
       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);
    ...
    

    License

    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

    About the Author

    Zoran M. Todorovic

    Web Developer
    SCA d.o.o.
    Serbia Serbia

    Member

    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.

    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board. (secure sign-in)
     
    Search this forum  
     FAQ
        Noise  Layout  Per page   
      Refresh
    QuestionCFrameWnd font? PinmemberSpitfed4:17 25 Sep '06  
    Questionhow to change font size? PinmemberHuaHsin18:15 27 Apr '04  
    AnswerRe: how to change font size? Pinmemberianw.moore23:03 26 Jul '04  
    Questionhow to set to the particular control? PinmemberHuaHsin17:00 27 Apr '04  
    GeneralMemory problems fixed, but now more problems PinsussJason8:34 24 Oct '00  
    GeneralMemory leaks PinsussJason9:03 6 Oct '00  

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

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

    Permalink | Advertise | Privacy | Mobile
    Web04 | 2.5.120517.1 | Last Updated 8 Dec 1999
    Article Copyright 1999 by Zoran M. Todorovic
    Everything else Copyright © CodeProject, 1999-2012
    Terms of Use
    Layout: fixed | fluid