Click here to Skip to main content
15,886,085 members
Articles / Desktop Programming / MFC

XMonoFontDialog - Customizing CFontDialog Part II: Selecting Monospaced Fonts

Rate me:
Please Sign up or sign in to vote.
4.88/5 (19 votes)
22 Oct 2008CPOL8 min read 34.2K   724   20  
XMonoFontDialog is a CFontDialog clone that is customized for selecting monospaced fonts. A custom label has been added to the dialog that informs the user when a monospaced font is selected in the combobox, and bold typeface is used to highlight monospaced fonts in the combobox list.
// XMonoFontListComboEdit.cpp
//
// Author:  Hans Dietrich
//          hdietrich@gmail.com
//
// Description:
//     XMonoFontListComboEdit.cpp implements CXMonoFontListComboEdit, a class 
//     used by CXMonoFontListCombo.
//
// License:
//     This software is released into the public domain.  You are free to use
//     it in any way you like, except that you may not sell this source code.
//
//     This software is provided "as is" with no expressed or implied warranty.
//     I accept no liability for any damage or loss of business that this 
//     software may cause.
//
///////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "XMonoFontListComboEdit.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#ifndef __noop
#if _MSC_VER < 1300
#define __noop ((void)0)
#endif
#endif

#undef TRACE
#define TRACE __noop

//=============================================================================
// if you want to see the TRACE output, uncomment this line:
//#include "XTrace.h"
//=============================================================================

//=============================================================================
BEGIN_MESSAGE_MAP(CXMonoFontListComboEdit, CEdit)
//=============================================================================
	//{{AFX_MSG_MAP(CXMonoFontListComboEdit)
	ON_CONTROL_REFLECT(EN_SETFOCUS, OnSetfocus)
	ON_WM_CTLCOLOR_REFLECT()
	//}}AFX_MSG_MAP
	ON_WM_PAINT()
END_MESSAGE_MAP()

//=============================================================================
CXMonoFontListComboEdit::CXMonoFontListComboEdit()
//=============================================================================
{
	m_bBold   = FALSE;
	m_strText = _T("");
}

//=============================================================================
CXMonoFontListComboEdit::~CXMonoFontListComboEdit()
//=============================================================================
{
	if (m_Font.GetSafeHandle())
		m_Font.DeleteObject();
	if (m_BoldFont.GetSafeHandle())
		m_BoldFont.DeleteObject();
	if (m_brush.GetSafeHandle())
		m_brush.DeleteObject();
}

//=============================================================================
void CXMonoFontListComboEdit::PreSubclassWindow() 
//=============================================================================
{
	TRACE(_T("in CXMonoFontListComboEdit::PreSubclassWindow ================\n"));

	// get current font
	CFont* pFont = GetSafeFont();

	// create the font for this control
	LOGFONT lf;
	pFont->GetLogFont(&lf);
	m_Font.CreateFontIndirect(&lf);

	lf.lfWeight = FW_BOLD;
	m_BoldFont.CreateFontIndirect(&lf);

	m_brush.CreateSolidBrush(GetSysColor(COLOR_WINDOW));

	CEdit::PreSubclassWindow();
}

//=============================================================================
void CXMonoFontListComboEdit::OnPaint() 
//=============================================================================
{
	CPaintDC dc(this); // device context for painting

	BOOL bFocus = FALSE;
	CWnd *pWndFocus = GetFocus();
	if (pWndFocus && IsWindow(pWndFocus->m_hWnd))
	{
		if (pWndFocus->m_hWnd == m_hWnd)
			bFocus = TRUE;
	}
	
	CString strText = m_strText;
	if (strText.IsEmpty())
		GetWindowText(strText);

	TRACE(_T("strText=%s\n"), strText);

	CRect rc;
	GetClientRect(&rc);
	dc.FillSolidRect(&rc, GetSysColor(COLOR_WINDOW));

	CEdit::SetSel(0, 0);

	CFont *pOldFont = 0;
	if (m_bBold)
		pOldFont = dc.SelectObject(&m_BoldFont);
	else
		pOldFont = dc.SelectObject(&m_Font);

	dc.SetBkMode(OPAQUE);

	if (bFocus)
	{
		dc.SetTextColor(GetSysColor(COLOR_WINDOW));
		dc.SetBkColor(GetSysColor(COLOR_HIGHLIGHT));
	}
	else
	{
		dc.SetTextColor(GetSysColor(COLOR_WINDOWTEXT));
		dc.SetBkColor(GetSysColor(COLOR_WINDOW));
	}

	dc.TextOut(rc.left, rc.top, strText);

	dc.SelectObject(pOldFont);
}

//=============================================================================
CFont * CXMonoFontListComboEdit::GetSafeFont()
//=============================================================================
{
	// get current font
	CFont *pFont = CWnd::GetFont();

	if (pFont == 0)
	{
		// try to get parent font
		CWnd *pParent = GetParent();
		if (pParent && IsWindow(pParent->m_hWnd))
			pFont = pParent->GetFont();

		if (pFont == 0)
		{
			// no font, so get a system font
			HFONT hFont = (HFONT)::GetStockObject(DEFAULT_GUI_FONT);
			if (hFont == 0)
				hFont = (HFONT)::GetStockObject(SYSTEM_FONT);
			if (hFont == 0)
				hFont = (HFONT)::GetStockObject(ANSI_VAR_FONT);
			if (hFont)
				pFont = CFont::FromHandle(hFont);
		}
	}

	return pFont;
}

//=============================================================================
void CXMonoFontListComboEdit::SetWindowText(LPCTSTR lpszString) 
//=============================================================================
{ 
	m_strText = lpszString; 
	RedrawWindow(); 
}

//=============================================================================
LRESULT CXMonoFontListComboEdit::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
//=============================================================================
{
	if (message == WM_SETTEXT)
	{
		// don't allow CEdit to paint text
		m_strText = (LPCTSTR) lParam;
		lParam = (LPARAM)_T("");
		RedrawWindow();
	}
	else if ((message == WM_LBUTTONDOWN) ||
			 (message == WM_LBUTTONDBLCLK))
	{
		GetParent()->SendMessage(message, wParam, lParam);
		return TRUE;
	}
	else if (message == WM_CHAR)
	{
		// let combobox handle it
		GetParent()->SendMessage(message, wParam, lParam);
		return TRUE;
	}
	else if (message == WM_CONTEXTMENU)
	{
		// suppress context menu
		return TRUE;
	}
	else if (message == WM_ERASEBKGND)
	{
		// entire control is painted in OnPaint
		return TRUE;
	}

	return CEdit::WindowProc(message, wParam, lParam);
}

//=============================================================================
void CXMonoFontListComboEdit::OnSetfocus() 
//=============================================================================
{
	RedrawWindow();	
}

//=============================================================================
HBRUSH CXMonoFontListComboEdit::CtlColor(CDC* /*pDC*/, UINT /*nCtlColor*/) 
//=============================================================================
{
	// this is necessary on Vista
	return m_brush;	
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.

Recently, I have moved to Los Angeles where I am doing consulting and development work.

For consulting and custom software development, please see www.hdsoft.org.






Comments and Discussions