MFC CFontDialog类封装了字体公共对话框,用于选择字体。
我们可以从CFontDialog派生一个对话框CMyFontDialog,用于预览选择的字体的显示效果。
下面是对应两文件代码,可以复制后生成两个文件。
具体使用时只要创建CFontDialog实例,将CFontDialog更改为CMyFontDialog便可。
// FontDialog.h
// (c) 1997 Roger Onslow
#ifndef _CMyFontDialog_
#define _CMyFontDialog_
class CMyFontDialog : public CFontDialog {
DECLARE_DYNCREATE(CMyFontDialog);
public:
CMyFontDialog(LPLOGFONT lplogfont=NULL, LPCTSTR sampletext="Sample Text", CWnd* pParentWnd=NULL);
protected:
LPCTSTR m_sampletext;
public:
CString SampleText() const { return m_sampletext; }
void SetSampleText(LPCTSTR sampletext) { m_sampletext = sampletext; }
public:
COLORREF TextColor() const { return m_cf.rgbColors; }
void SetTextColor(COLORREF rgbColors) { m_cf.rgbColors = rgbColors; }
protected:
// Dialog Data
protected:
//{{AFX_DATA(CMyFontDialog)
//}}AFX_DATA
// Overrides
protected:
// ClassWizard generate virtual function overrides
//{{AFX_VIRTUAL(CMyFontDialog)
virtual BOOL OnInitDialog();
//}}AFX_VIRTUAL
// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(CMyFontDialog)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
#endif
////////////////////////////////////////////////////////////////////
// FontDialog.cpp
// (c) 1997 Roger Onslow
#include "stdafx.h"
#include "FontDialog.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
IMPLEMENT_DYNCREATE(CMyFontDialog, CFontDialog)
BEGIN_MESSAGE_MAP(CMyFontDialog, CFontDialog)
//{{AFX_MSG_MAP(CMyFontDialog)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
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;
}
|