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
Overview
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).
Usage
COXFontPickerButton
in dialog or form view.
- 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).
- 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.
- 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);
m_btnFontPicker.LoadBitmap(IDB_FONT,FALSE,RGB(192,192,192));
m_btnFontPicker.SetBuddy(IDC_STATIC_FONT_PICKER);
m_btnFontPicker.SetToolTip(TRUE);
- 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()
{
UpdateData(TRUE);
m_cmbFontPicker.SelectFont(&m_font);
}
COXFontPickerButton
as a child control of any arbitrary window (explicit creation).
- Create the
COXFontPickerButton
control explicitly using the CButton::Create()
function. Make sure that you specify the BS_OWNERDRAW
style for this button (COXBitmapButton
requirement).
- 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.
- After both controls are successfully created, you have to associate the
COXStatic
control with the COXFontPickerButton
control using:
BOOL SetBuddy(COXStatic* pBuddy);
- 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.
COXFontComboBox
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.
Usage
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_HASSTRINGS
style must be specified
CBS_SORT
style must NOT be specified
#include "OXFontComboBox.h"
class CFontPickersDlg : public CDialog
{
public:
CFontPickersDlg(CWnd* pParent = NULL);
enum { IDD = IDD_FONTPICKERS_DIALOG };
...
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()
{
CFont* pFont = m_cmbFontPicker.GetSelectedFont();
LOGFONT lf;
pFont->GetLogFont(&lf);
MessageBox(lf.lfFaceName);
}
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:
m_cmbFontPicker.GetFontListBox()->SetMaxItemsBeforeSeparator(4);
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.