Click here to Skip to main content
13,258,055 members (74,062 online)
Click here to Skip to main content
Add your own
alternative version


19 bookmarked
Posted 6 Nov 2002

WTL Window Font Class

, 6 Nov 2002
Rate this:
Please Sign up or sign in to vote.
Use this class to add additional fonts to your WTL application
<!-- Download Links --> <!-- Article image -->

Sample Image - WTLWindowFont.jpg

<!-- Add the rest of your HTML here -->


Use this class to easily add additional fonts to your WTL windows/dialogs. The class, CWindowFont, will create a new font based on the font used by an existing window. All you supply are the necessary font attributes (bold, italic, etc.), perfect for use with a dialog based application where you want to display, for example, a static control using a bold font.

How to use

To use this class in a WTL dialog application, simply do the following:

First include the header file:

#include "windowfont.h"

Next, add a suitable CWindowFont member for each font you wish to create, e.g.:

CWindowFont m_fontBold;

Next, in your dialogs OnInitDialog function, create the font and apply it to a dialog control:

// Create the font and apply to the IDC_TEXT control
m_fontBold.Apply(m_hWnd, CWindowFont::typeBold, IDC_TEXT);

Alternatively, call the Create function and apply the font "by hand":

// Create a bold font
if (m_fontBold.Create(m_hWnd, CWindowFont::typeBold))

That's it! Simple. For example, I use this class in every "About" box I have (to display the program version info, etc, in bold). I have also used this class to create a double-height font for use on the first page of a wizard, etc.


The following font styles are available (note that you can OR these together in any combination):

  • Bold (CWindowFont::typeBold)
  • Italic (CWindowFont::typeItalic)
  • Underline (CWindowFont::typeUnderline)
  • Double-height (CWindowFont::typeDoubleHeight)

Note also that the dialogs used in the demo are all set to use the "MS Shell Dlg" font - which is best if you want to create a double-height font (as it will render better than the default "MS Sans Serif" font).

CWindowFont Source

The CWindowFont class is small enough to post here:

#pragma once

#include <ATLGDI.H>

// Wrapper for the Win32 LOGFONT structure
class CLogFont : public LOGFONT
        memset(this, 0, sizeof(LOGFONT));        

// Class used to create a font based on the font used by a specific window
class CWindowFont : public CFont  
    // Font styles
    typedef enum tagEType
        typeNormal       = 0x00,
        typeBold         = 0x01,
        typeItalic       = 0x02,
        typeUnderline    = 0x04,
        typeDoubleHeight = 0x08,
    } EType;
    CWindowFont() : CFont()
    /// hWnd  - The window to use for the base font
    /// nType - Font style flags
    CWindowFont(HWND hWnd, int nType)
        // We need a HWND
        ATLASSERT(hWnd != NULL);
        // Create the font
        Create(hWnd, nType);
    virtual ~CWindowFont()
    // Create the font
    // hWnd  - The window to use for the base font
    // nType - Font style flags
    // return true on success
    bool Create(HWND hWnd, int nType)
        // Be defensive
        ATLASSERT(hWnd != NULL);
        ATLASSERT(::IsWindow(hWnd) != FALSE);
        // Get the font the window is currently using
        HFONT hFont = (HFONT)::SendMessage(hWnd, WM_GETFONT, 0, 0);
        // Do we have a font?
        if (hFont == NULL)
            return false;
        CLogFont lf;        
        // Fill the LOGFONT
        if (::GetObject(hFont, sizeof(lf), &lf) == 0)
            return false;
        // Amend the LOGFONT members
        if (nType & typeBold)
            lf.lfWeight = FW_BOLD;
        if (nType & typeItalic)
            lf.lfItalic = TRUE;
        if (nType & typeUnderline)
            lf.lfUnderline = TRUE;
        if (nType & typeDoubleHeight)
            lf.lfHeight *= 2;
        // Create the new font
        return CreateFontIndirect(&lf) ? true : false;
    // Create the font and apply to a nominate dialog control
    bool Apply(HWND hWnd, int nType, UINT nControlID)
        // First create the font
        if (!Create(hWnd, nType))
            return false;
        // Apply to the nominated control
        CWindow wndControl = ::GetDlgItem(hWnd, nControlID);
        ATLASSERT(wndControl != NULL);
        // Apply
        return true;


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

You may also be interested in...

Comments and Discussions

QuestionLicense for this article Pin
Ori Saporta29-Apr-15 2:00
memberOri Saporta29-Apr-15 2:00 
GeneralUsing with an MFC Dialog Application Pin
Astronomer13-Jul-05 9:53
memberAstronomer13-Jul-05 9:53 
GeneralRe: Using with an MFC Dialog Application Pin
Robert Edward Caldecott14-Jul-05 6:31
memberRobert Edward Caldecott14-Jul-05 6:31 
GeneralSmall issue with the CLogFont vtable Pin
Jason De Arte16-Oct-03 12:45
memberJason De Arte16-Oct-03 12:45 
GeneralRe: Small issue with the CLogFont vtable Pin
Robert Edward Caldecott16-Oct-03 13:36
memberRobert Edward Caldecott16-Oct-03 13:36 
General(Another) Minor change to Create() Pin
ajh15-Oct-03 14:38
memberajh15-Oct-03 14:38 
Another quick mod. to Create(). An ASSERT was cropping up because the parent window was Destroyed, and then re-Created. Let me know...

<br />
...<br />
		// Be defensive<br />
		ATLASSERT(hWnd != NULL);<br />
		ATLASSERT(::IsWindow(hWnd) != FALSE);<br />
		//AH Oct 15, 2003<br />
		if (!IsNull())<br />
			return true;//our HFONT member is not null!  I am assuming we have already been created<br />
...<br />

GeneralMinor change to Create() Pin
Anonymous15-Oct-03 8:38
sussAnonymous15-Oct-03 8:38 
GeneralRe: Minor change to Create() Pin
Robert Edward Caldecott15-Oct-03 8:42
memberRobert Edward Caldecott15-Oct-03 8:42 
GeneralYou did it again! Pin
Mike Klimentiev30-Apr-03 9:12
memberMike Klimentiev30-Apr-03 9:12 
GeneralRe: You did it again! Pin
Robert Edward Caldecott30-Apr-03 22:27
memberRobert Edward Caldecott30-Apr-03 22:27 
GeneralLink error Pin
DeXian15-Feb-03 2:36
memberDeXian15-Feb-03 2:36 
GeneralRe: Link error Pin
Robert Edward Caldecott17-Feb-03 23:03
memberRobert Edward Caldecott17-Feb-03 23:03 
GeneralRe: Link error Pin
elfric17-May-04 10:11
memberelfric17-May-04 10:11 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 7 Nov 2002
Article Copyright 2002 by Rob Caldecott
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid