Home > Windows API > Changing font of all child controls at one go!

Changing font of all child controls at one go!

Ever felt the need to change the font of all child controls of a dialog, or a window, and you had to use GetWindow, GetNextWindow and stuff like that to set a font for a window. Well here is an easy way!

// Idea taken from MSDN...
BOOL CSomeDialog::OnInitDialog()
{
    // I am using a static font for demo purpose only, make sure
    // you have this as a member
    static CFont fnt;
    CDC dc;
    dc.Attach( GetDC()->GetSafeHdc() );
    fnt.CreatePointFont( 100, _T( "Courier new" ), &dc );

    // Set change font message to all child windows
    SendMessageToDescendants( GetSafeHwnd(),
                              WM_SETFONT,
                              (WPARAM)fnt.GetSafeHandle(),
                              MAKELONG(WORD( TRUE ), 0 ),
                              true,
                              false );
}// End OnInitDialog
  1. No comments yet.
  1. No trackbacks yet.

Leave a comment