Font dialog with custom text preview '& color


Application Security Testing: An Integral Part of DevOps

The MFC CFontDialog class encapulates the standard Font common dialog box. This dialog allows you to select a font. It display a sample window showing some text in the required font. It is nice to be able to specify your own sample text - for example, if I am editing a text object in a drawing package, I would want to see the actual text in the sample box.

However, the MFC implmentation does not let you easily specify the sample text.

The following class, derived from CFontDialog, lets you do just this. It also lets you set and get the color selected in the dialog. Simple create a CMyFontDialog and specify the sample text in the constructor (or call SetSampleText() before calling DoModal()). Also call SetTextColor() before calling DoModal(), and retrieve the selected color afterwards with TextColor().

// FontDialog.h
// (c) 1997 Roger Onslow

#ifndef _CMyFontDialog_
#define _CMyFontDialog_

class CMyFontDialog : public CFontDialog {
     CMyFontDialog(LPLOGFONT lplogfont=NULL, LPCTSTR sampletext="Sample Text", CWnd* pParentWnd=NULL);
     LPCTSTR m_sampletext;
     CString SampleText() const { return m_sampletext; }
     void SetSampleText(LPCTSTR sampletext) { m_sampletext = sampletext; }
     COLORREF TextColor() const { return m_cf.rgbColors; }
     void SetTextColor(COLORREF rgbColors) { m_cf.rgbColors = rgbColors; }
     // Dialog Data
     // Overrides
     // ClassWizard generate virtual function overrides
     virtual BOOL OnInitDialog();
     // Implementation
     // Generated message map functions


// FontDialog.cpp
// (c) 1997 Roger Onslow

#include "stdafx.h"
#include "FontDialog.h"

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;


BEGIN_MESSAGE_MAP(CMyFontDialog, CFontDialog)

CMyFontDialog::CMyFontDialog(LPLOGFONT lpLogfont, LPCTSTR sampletext, CWnd* pParentWnd)
		: CFontDialog(lpLogfont,CF_EFFECTS | CF_SCREENFONTS, NULL, pParentWnd)
		, m_sampletext(sampletext)

BOOL CMyFontDialog::OnInitDialog() {
     BOOL r = CFontDialog::OnInitDialog();
     if (m_sampletext) {
          SetDlgItemText(stc5, m_sampletext);
     return r;



  • stc5

    Posted by Ghostie on 07/14/2004 09:45am

    This won't compile. What is 'stc5' in the SetDlgItemText() call?

    • Posted by Ghostie on 07/14/2004 10:47am

      #include Dlgs.h (in brackets) in order to be able to use 'stc5'.

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.