Click here to Skip to main content
13,893,974 members
Click here to Skip to main content
Add your own
alternative version


18 bookmarked
Posted 24 Aug 2007
Licenced CPOL

The Ultimate Toolbox Font Pickers

, 25 Aug 2007
Rate this:
Please Sign up or sign in to vote.
Two convenient font selection controls from the Ultimate Toolbox for MFC

Visit the Ultimate Toolbox main page for an overview and configuration guide to the Ultimate Toolbox library.

Source code and project files for this sample can be found in the samples\gui\FontPickers directory of the sample projects download.



COXFontPickerButton is a COXBitmapButton derived class that is designed to allow a user to choose font and text color values. This control uses the standard CFontDialog to choose font and text color values and uses the COXStatic control in order to display the chosen font and text color. And of course, it allows you to use all the power of the COXBitmapButton class such as displaying an image, specifying text font and color, tooltip, and many more features are available in this class. Also, DDX routines are provided that allow you to associate COLORREF and CFont variables with the control and set/retrieve data a la standard MFC data exchange (see below).


  1. COXFontPickerButton in dialog or form view.
    1. In the resource editor, put a button control in a dialog template. Make sure that you specify the owner drawn style for this button (this is a COXBitmapButton requirement).
    2. Put a static control next to the button. This static control will be later explicitly associated with the button and subclassed with the COXStatic object. This control will be used in order to display the chosen font and text color.
    3. In OnInitDialog(), for a CDialog-derived implementation, and in OnInitialUpdate(), for a CFormView-derived implementation, you must call the following function that will associate the static control with the font picker button:
      • BOOL SetBuddy (UINT nBuddyWndID);
      // TODO: Add extra initialization here
    4. You might want to associate a COLORREF and/or CFont variable with the font picker button using the controls DDX routines:
      • void DDX_FontPickerFont (CDataExchange *pDX, int nIDC, CFont* pFont);
      • void DDX_FontPickerColor(CDataExchange *pDX, int nIDC, COLORREF& clr);

      In order to do that, you have to add the corresponding variables to your CDialog or CFormView derived class:

      • CFont m_font;
      • COLORREF m_clrText;

      and update your DoDataExchange() routine by adding the following lines of code:

      DDX_FontPickerFont(pDX, IDC_BUTTON_FONT, &m_font);
      DDX_FontPickerColor(pDX, IDC_BUTTON_FONT, m_clrText);

      where IDC_BUTTON_FONT is presumed to be an ID of the font picker button. Using these DDX routines, you can set/retrieve the font and text color by simply calling the UpdateData() function, as in this BN_CLICKED handler from the sample, which simply synchronizes the font picker combo:

      void CFontPickersDlg::OnButtonFontPicker() 
          // TODO: Add your control notification handler code here
  2. COXFontPickerButton as a child control of any arbitrary window (explicit creation).
    1. Create the COXFontPickerButton control explicitly using the CButton::Create() function. Make sure that you specify the BS_OWNERDRAW style for this button (COXBitmapButton requirement).
    2. Create the COXStatic control explicitly using the COXStatic::Create() function. Before calling this function, you need to calculate the rectangle for this control so it will be positioned nicely relative to the COXFontPickerButton control.
    3. After both controls are successfully created, you have to associate the COXStatic control with the COXFontPickerButton control using:
      • BOOL SetBuddy(COXStatic* pBuddy);
    4. After accomplishing the three previous steps, you will get a fully functional font picker button control. The user can click on the button and the standard CFontDialog will appear where a user can choose a font and text color. The chosen font and text color can be retrieved using the following functions:
      • CFont* GetBuddyFont() const;
      • BOOL GetBuddyLogFont(LOGFONT* pLF) const;
      • COLORREF GetBuddyTextColor() const;

A full class reference for the COXFontPickerButton can be found in the Graphical User Interface | Bitmap Enhanced Controls section of the compiled HTML help documentation.


The font combo showing the MRU capability of the underlying extended list box.

COXFontComboBox is a CComboBox derived class that uses COXFontListBox as a drop down list box control.

COXFontListBox is a list box control which is automatically populated with all fonts installed in the specified device context (by default we use the screen device context). So whenever a user clicks on the dropdown button the list box with items that represent available fonts is displayed. The name of the selected font will appear in edit part of the combo box.


In order to use COXFontComboBox object in your application you have to create it using standard CComboBox::Create function or subclass the existing control (e.g. using DDX/DDV technology). When creating control explicitly or defining it in dialog template you have to make sure that the following requirements are met:

CBS_OWNERDRAWVARIABLE style must be specified
CBS_HASSTRINGSstyle must be specified
CBS_SORTstyle must NOT be specified

#include <span class="code-string">"OXFontComboBox.h"</span>


// CFontPickersDlg dialog

class CFontPickersDlg : public CDialog
// Construction

    CFontPickersDlg(CWnd* pParent = NULL);    // standard constructor

// Dialog Data


    COXFontComboBox    m_cmbFontPicker;

You can add a standard CBN_SELCHANGE notification handler to your dialog to take action when a font is selected:

void CFontPickersDlg::OnCbnSelchangeComboFontPicker()
    // TODO: Add your control notification handler code here

    CFont* pFont = m_cmbFontPicker.GetSelectedFont();

    LOGFONT lf;

So the control was successfully created or subclassed. Before the dropdown list box is displayed for first time it will be automatically populated with all fonts defined in the screen device context (default device context).

The COXFontComboBox contains a COXFontListBox which is derived from COXListBoxEx. More advanced customization is available through getting a pointer to the internal COXFontListBox object and calling its functions. Use this function in order to retrieve a pointer to the object:

COXFontListBox* GetFontListBox();

For example, you can limit the number of most recently used items displayed in the font list box by calling:


While a control is being populated with enumerated fonts the following protected virtual function will be called for every font before it is added to the list:

virtual BOOL FilterFont(OXLBFONTINFO* pLBFI);

The default implementation of this function returns always TRUE. In your own implementation you might override it in order to filter the fonts displayed in the list box.

A full class reference for the COXFontComboBox can be found in the Graphical User Interface | Combo Boxes section of the compiled HTML Help documentation.


Initial CodeProject release August 2007.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

The Ultimate Toolbox
Web Developer
Canada Canada
In January 2005, David Cunningham and Chris Maunder created, a new group dedicated to the continued development, support and growth of Dundas Software’s award winning line of MFC, C++ and ActiveX control products.

Ultimate Grid for MFC, Ultimate Toolbox for MFC, and Ultimate TCP/IP have been stalwarts of C++/MFC development for a decade. Thousands of developers have used these products to speed their time to market, improve the quality of their finished products, and enhance the reliability and flexibility of their software.
Group type: Organisation

458 members

You may also be interested in...

Comments and Discussions

Questionhi.. i cannot download src file Pin
Roy Barina5-Aug-13 8:22
memberRoy Barina5-Aug-13 8:22 
QuestionHow to add the font picker combo to the ToolBar Pin
fifth811824-Nov-07 10:54
memberfifth811824-Nov-07 10:54 
AnswerRe: How to add the font picker combo to the ToolBar Pin
Tim Deveaux30-Nov-07 6:05
memberTim Deveaux30-Nov-07 6:05 
GeneralRe: How to add the font picker combo to the ToolBar Pin
fifth81181-Dec-07 5:00
memberfifth81181-Dec-07 5:00 

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 | Cookies | Terms of Use | Mobile
Web04 | 2.8.190306.1 | Last Updated 25 Aug 2007
Article Copyright 2007 by The Ultimate Toolbox
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid