⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 choosefontpage.cpp

📁 这是WINCE进行字体选择演示的的一个程序
💻 CPP
字号:
// ChooseFontPage.cpp : implementation file
//
#include "stdafx.h"
#include "resource.h"
#include "ChooseFontPage.h"

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


//---------------------------------------------------------------------------
//
//	CChooseFontPage property page
//
//---------------------------------------------------------------------------


IMPLEMENT_DYNCREATE(CChooseFontPage, CPropertyPage)


CChooseFontPage::CChooseFontPage() : CPropertyPage(CChooseFontPage::IDD)
{
	//{{AFX_DATA_INIT(CChooseFontPage)
	m_strPreview = _T("AaBbCcXxYyZz");
	//}}AFX_DATA_INIT
}


CChooseFontPage::~CChooseFontPage()
{
}


void CChooseFontPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChooseFontPage)
	DDX_Control(pDX, IDC_FONT_SIZE, m_wndSize);
	DDX_Control(pDX, IDC_FONT_NAME, m_wndFont);
	DDX_Text(pDX, IDC_PREVIEW, m_strPreview);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChooseFontPage, CPropertyPage)
	//{{AFX_MSG_MAP(CChooseFontPage)
	ON_WM_PAINT()
	ON_BN_CLICKED(IDC_CLEAR_TYPE, OnClearType)
	ON_BN_CLICKED(IDC_BOLD, OnBold)
	ON_BN_CLICKED(IDC_ITALIC, OnItalic)
	ON_BN_CLICKED(IDC_UNDERLINE, OnUnderline)
	ON_CBN_SELCHANGE(IDC_FONT_NAME, OnSelChangeFontName)
	ON_CBN_EDITCHANGE(IDC_FONT_NAME, OnEditChangeFontName)
	ON_CBN_SELCHANGE(IDC_FONT_SIZE, OnSelChangeFontSize)
	ON_CBN_EDITCHANGE(IDC_FONT_SIZE, OnEditChangeFontSize)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


// CChooseFontPage::MultDiv
//
//		Integer multiplication and division with rounding
//
int CChooseFontPage::MultDiv(int a, int b, int c)
{
	int nMul = a * b,
		nMod = nMul % c,
		nRes = nMul / c;

	if(nMod >= c / 2)	// Round up if >= 0.5
		nRes++;
	return nRes;
}


// CChooseFontPage::RenderFont
//
//		Renders the font
//
void CChooseFontPage::RenderFont()
{
	if(m_fontSample.m_hObject)
		m_fontSample.DeleteObject();

	if(m_fontSample.CreateFontIndirect(&m_logFont))
	{
		CWnd*	pWnd = GetDlgItem(IDC_PREVIEW);

		if(pWnd)
			pWnd->SetFont(&m_fontSample);
	}
}


// CChooseFontPage::Underline
//
//		Underlines a control
//
void CChooseFontPage::Underline(CDC &dc, UINT nID)
{
	CWnd*	pWnd;

	pWnd = GetDlgItem(nID);
	if(pWnd)
	{
		CRect	rc;
		CPen	penGrey(PS_SOLID, 1, RGB(0xC0, 0xC0, 0xC0));
		CPen*	pPenOld;

		pWnd->GetWindowRect(&rc);
		ScreenToClient(&rc);

		pPenOld = dc.SelectObject(&penGrey);

		dc.MoveTo(rc.left, rc.bottom + 2);
		dc.LineTo(rc.right, rc.bottom + 2);

		dc.SelectObject(pPenOld);
	}
}


//---------------------------------------------------------------------------
//
//	CChooseFontPage message handlers
//
//---------------------------------------------------------------------------


BOOL CChooseFontPage::OnInitDialog() 
{
	CWnd*	pWnd;
	LOGFONT	lf;
	int		iIndex,
			nPoint;
	CString	strPoint;

	CPropertyPage::OnInitDialog();

	//
	// Create the bold font
	//
	lf.lfHeight			= -11;
	lf.lfWidth			= 0;
	lf.lfEscapement		= 0;
	lf.lfOrientation	= 0;
	lf.lfWeight			= FW_BOLD;
	lf.lfItalic			= FALSE;
	lf.lfUnderline		= FALSE;
	lf.lfStrikeOut		= 0;
	lf.lfCharSet		= ANSI_CHARSET;
	lf.lfOutPrecision	= OUT_DEFAULT_PRECIS;
	lf.lfClipPrecision	= CLIP_DEFAULT_PRECIS;
	lf.lfQuality		= DEFAULT_QUALITY;
	lf.lfPitchAndFamily = DEFAULT_PITCH | FF_SWISS;
	_tcscpy(lf.lfFaceName, TEXT("Tahoma"));
	m_fontBold.CreateFontIndirect(&lf);

	//
	// Set the dividers to bold
	//
	pWnd = GetDlgItem(IDC_ST_STYLE);
	if(pWnd)
		pWnd->SetFont(&m_fontBold);
	pWnd = GetDlgItem(IDC_ST_PREVIEW);
	if(pWnd)
		pWnd->SetFont(&m_fontBold);
	
	//
	// Enumerate all the fonts
	//
	CDC*	pDC = GetDC();
	m_nLogPixY = GetDeviceCaps(*pDC, LOGPIXELSY);
	EnumFontFamilies(*pDC, NULL, (FONTENUMPROC)EnumFontFamProc, (LPARAM)this);
	ReleaseDC(pDC);

	//
	// Now, find the prespecified font, if any
	//
	iIndex = m_wndFont.FindStringExact(-1, m_logFont.lfFaceName);
	if(iIndex != CB_ERR)
		m_wndFont.SetCurSel(iIndex);
	else
		m_wndFont.SetWindowText(m_logFont.lfFaceName);

	//
	// Fill in the default font sizes
	//
	m_wndSize.AddString(_T("8"));
	m_wndSize.AddString(_T("9"));
	m_wndSize.AddString(_T("10"));
	m_wndSize.AddString(_T("11"));
	m_wndSize.AddString(_T("12"));
	m_wndSize.AddString(_T("14"));
	m_wndSize.AddString(_T("16"));
	m_wndSize.AddString(_T("20"));
	m_wndSize.AddString(_T("24"));
	m_wndSize.AddString(_T("28"));
	m_wndSize.AddString(_T("36"));

	//
	// Find the given point size
	//
	nPoint = MultDiv(-72, m_logFont.lfHeight, m_nLogPixY);
	strPoint.Format(_T("%d"), nPoint);
	iIndex = m_wndSize.SelectString(-1, strPoint);
	if(iIndex == CB_ERR)
		m_wndSize.SetWindowText(strPoint);

	//
	// Check for other font properties
	//
	CheckDlgButton(IDC_BOLD,		m_logFont.lfWeight == FW_BOLD ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(IDC_ITALIC,		m_logFont.lfItalic ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(IDC_UNDERLINE,	m_logFont.lfUnderline ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(IDC_CLEAR_TYPE,	m_logFont.lfQuality == 5 ? BST_CHECKED : BST_UNCHECKED);

	//
	// Render the sample
	//
	RenderFont();
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}


// CChooseFontPage::OnPaint
//
//		Paints the light grey separators
//
void CChooseFontPage::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	//
	// Underline the separators
	//
	Underline(dc, IDC_ST_STYLE);
	Underline(dc, IDC_ST_PREVIEW);
	
	// Do not call CPropertyPage::OnPaint() for painting messages
}


// CChooseFontPage::OnClearType
//
//		The user clicked the clear type check box
//
void CChooseFontPage::OnClearType() 
{
	m_logFont.lfQuality = IsDlgButtonChecked(IDC_CLEAR_TYPE) ? 5 : DEFAULT_QUALITY;

	RenderFont();
}


// CChooseFontPage::OnBold
//
//		The user clicked the bold check box
//
void CChooseFontPage::OnBold() 
{
	m_logFont.lfWeight = IsDlgButtonChecked(IDC_BOLD) ? FW_BOLD : FW_NORMAL;

	RenderFont();
}


// CChooseFontPage::OnItalic
//
//		The user clicked the italic check box
//
void CChooseFontPage::OnItalic() 
{
	m_logFont.lfItalic = IsDlgButtonChecked(IDC_ITALIC);

	RenderFont();
}


// CChooseFontPage::OnUnderline
//
//		The user clicked the underline check box
//
void CChooseFontPage::OnUnderline() 
{
	m_logFont.lfUnderline = IsDlgButtonChecked(IDC_UNDERLINE);

	RenderFont();
}


// CChooseFontPage::OnSelChangeFontName
//
//		The user selected a new font name
//
void CChooseFontPage::OnSelChangeFontName() 
{
	int		iSel;
	CString	strFont;

	iSel = m_wndFont.GetCurSel();
	if(iSel != CB_ERR)
	{
		m_wndFont.GetLBText(iSel, strFont);
		_tcscpy(m_logFont.lfFaceName, strFont);

		RenderFont();
	}
}


// CChooseFontPage::OnEditChangeFontName
//
//		The user changed the font name on the edit field
//
void CChooseFontPage::OnEditChangeFontName() 
{
	CString	strFont;

	m_wndFont.GetWindowText(strFont);
	_tcscpy(m_logFont.lfFaceName, strFont);

	RenderFont();
}


// CChooseFontPage::OnSelChangeFontSize
//
//		The user selected a new font size
//
void CChooseFontPage::OnSelChangeFontSize() 
{
	int		iSel,
			nPoint;
	CString	strSize;

	iSel = m_wndSize.GetCurSel();
	if(iSel != CB_ERR)
	{
		m_wndSize.GetLBText(iSel, strSize);
		nPoint = _ttoi(strSize);

		m_logFont.lfHeight = -MultDiv(nPoint, m_nLogPixY, 72);

		RenderFont();
	}
}


// CChooseFontPage::OnEditChangeFontSize
//
//		The user changed the font size on the edit field
//
void CChooseFontPage::OnEditChangeFontSize() 
{
	CString	strSize;
	int		nPoint;

	m_wndSize.GetWindowText(strSize);
	nPoint = _ttoi(strSize);
	if(nPoint > 0)
	{
		m_logFont.lfHeight = -MultDiv(nPoint, m_nLogPixY, 72);

		RenderFont();
	}
}


//---------------------------------------------------------------------------
//
//	CChooseFontPage font enumeration
//
//---------------------------------------------------------------------------


// EnumFontFamProc
//
//		Font enumeration function
//
int CALLBACK EnumFontFamProc(ENUMLOGFONT *lpelf, TEXTMETRIC *lpntm, 
							 int FontType, LPARAM lParam)
{
	CChooseFontPage*	pPage = (CChooseFontPage*)lParam;

	pPage->m_wndFont.AddString(lpelf->elfLogFont.lfFaceName);
	return 1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -