www.gusucode.com > VC++枚举Windows可用字体并预览-源码程序 > VC++枚举Windows可用字体并预览-源码程序\code\WizFontNamesComboBox.cpp

    // WizFontNamesComboBox.cpp : implementation file
// Download by http://www.NewXing.com

#include "stdafx.h"
#include "EnumFonts.h"
#include "WizFontNamesComboBox.h"

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

/////////////////////////////////////////////////////////////////////////////
// CWizFontNamesComboBox

CWizFontNamesComboBox::CWizFontNamesComboBox()
{
}

CWizFontNamesComboBox::~CWizFontNamesComboBox()
{
}


BEGIN_MESSAGE_MAP(CWizFontNamesComboBox, CComboBox)
	//{{AFX_MSG_MAP(CWizFontNamesComboBox)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWizFontNamesComboBox message handlers

//初始化列表框
void CWizFontNamesComboBox::Initialize(const CWizFontsEnumerator& fonts)
{
	//清空列表框的内容
	ResetContent();
	//将获得的字体加入到列表框中
	for (int i = 0; i < fonts.GetFontsCount(); i++)
	{
		const CWizFontsEnumerator::Font* pFont = fonts.GetFont(i);
		if (pFont)
		{
			CString name = pFont->Name();
			if (pFont->IsTrueType())
				name += _T("(TT)");
			
			int j = AddString(name);
			if (j >= 0)
			{
				SetItemData(j, i);
			}
			else
			{
				ASSERT(0);
			}
		}
		else
		{ ASSERT(0); }
	}
	//将第一种字体设为被选择
	if (fonts.GetFontsCount() > 0)
		SetCurSel(0);
}

BOOL CWizFontNamesComboBox::PreCreateWindow(CREATESTRUCT& cs) 
{
	return CComboBox::PreCreateWindow(cs);
}

//获得当前选择的字体
const CWizFontsEnumerator::Font* CWizFontNamesComboBox::GetCurrentFont(const CWizFontsEnumerator& fonts)
{
	int i = GetCurSel();
	//如果有被选择的条目
	if (i >= 0)
	{
		int j = GetItemData(i);
		if (j >= 0)
		{
			//获得该字体
			const CWizFontsEnumerator::Font* font = fonts.GetFont(j);
			ASSERT(font);
			return font;
		}
		else
		{	
			ASSERT(0);
		}
	}
	else
	{
		ASSERT(0); 
	}
	
	return NULL;
}