下载本文示例代码
p>第二步:在你的项目中包含源文件
下面给出了头文件和实现文件。你只要简单的把它们包含到你的项目中,并且使用cfontpage类就可以了。该类的实现是比较简单的。cfontpage构造函数获取一指向logfont结构的指针。它使用该结构的信息来初始化自己。如果logfont结构没有提供,则使用窗口的当前字体。在oninitdialog()函数中,我们列出了所需字体和字体组合框。但是,任何字体特性的改变,都要调用onselchange()函数来实现。所有的信息映射入口都指向该函数.onselchange()函数更新内部信息,并且显示样本文本。
fontpage.h
#if !defined(afx_fontpage_h__de7edeb3_056d_11d1_82df_e2cdc9000000__included_)
#define afx_fontpage_h__de7edeb3_056d_11d1_82df_e2cdc9000000__included_
#if _msc_ver $#@62;= 1000
#pragma once
#endif // _msc_ver $#@62;= 1000
// fontpage.h : header file
//
/////////////////////////////////////////////////////////////////////////////
//
cfontpage dialog
class cfontpage : public cpropertypage
{
declare_dyncreate(cfontpage)
// construction
public:
void getcurrentfont(lplogfont lplf);
cfontpage(logfont* plogfont = null);
~cfontpage();
// dialog data
//{{afx_data(cfontpage)
enum { idd = idd_fontpage };
cstatic m_staticsample;
ccombobox m_combostyle;
ccombobox m_combosize;
ccombobox m_combofont;
bool m_bstrikeout;
bool m_bunderline;
cstring m_sfont;
cstring m_ssize;
cstring m_sstyle;
//}}afx_data
// overrides
// classwizard generate virtual function overrides
//{{afx_virtual(cfontpage)
protected:
virtual void dodataexchange(cdataexchange* pdx); // ddx/ddv
support
//}}afx_virtual
// implementation
protected:
// generated message map functions
//{{afx_msg(cfontpage)
virtual bool oninitdialog();
afx_msg void onselchange();
//}}afx_msg
declare_message_map()
private:
cfont fontsample;
int m_cypixelsperinch;
static int callback fontenumproc(enumlogfontex *lpelfe, newtextmetricex
*lpntme,
int fonttype, cfontpage* pfontpage );
};
//{{afx_insert_location}}
// microsoft developer studio will insert additional declarations
immediately
// before the previous line.
#endif // !defined(afx_fontpage_h__de7edeb3_056d_11d1_82df_e2cdc9000000__included_)
fontpage.cpp
// fontpage.cpp : implementation file
//
#include "stdafx.h"
#include "resource.h"
#include "fontpage.h"
#ifdef _debug
#define new debug_new
#undef this_file
static char this_file[] = __file__;
#endif
/////////////////////////////////////////////////////////////////////////////
// cfontpage property page
implement_dyncreate(cfontpage, cpropertypage)
cfontpage::cfontpage(logfont* plogfont /*= null*/) : cpropertypage(cfontpage::idd)
{
//{{afx_data_init(cfontpage)
//}}afx_data_init
logfont logfont;
cwindowdc dc(getdesktopwindow() );
m_cypixelsperinch = getdevicecaps(dc, logpixelsy);
if( plogfont == null )
{
cfont *pfont = dc.getcurrentfont();
pfont-$#@62;getlogfont( &logfont );
plogfont = &logfont;
}
m_fontsample.createfontindirect( plogfont );
m_bstrikeout = plogfont-$#@62;lfstrikeout;
m_bunderline = plogfont-$#@62;lfunderline;
m_sfont = plogfont-$#@62;lffacename;
m_ssize.format( "%d", muldiv(plogfont-$#@62;lfheight,
72, m_cypixelsperinch) );
m_sstyle = _t("regular");
if( plogfont-$#@62;lfweight $#@62;= 700 && plogfont-$#@62;lfitalic)
m_sstyle = _t("bold italic");
else if( plogfont-$#@62;lfitalic )
m_sstyle = _t("italic");
else if ( plogfont-$#@62;lfweight $#@62;= 700 )
m_sstyle = _t("bold");
}
cfontpage::~cfontpage()
{
}
void cfontpage::dodataexchange(cdataexchange* pdx)
{
cpropertypage::dodataexchange(pdx);
//{{afx_data_map(cfontpage)
ddx_control(pdx, idc_sample, m_staticsample);
ddx_control(pdx, idc_style, m_combostyle);
ddx_control(pdx, idc_fontsize, m_combosize);
ddx_control(pdx, idc_font, m_combofont);
ddx_check(pdx, idc_strikeout, m_bstrikeout);
ddx_check(pdx, idc_underline, m_bunderline);
ddx_cbstring(pdx, idc_font, m_sfont);
ddx_cbstring(pdx, idc_fontsize, m_ssize);
ddv_maxchars(pdx, m_ssize, lf_facesize);
ddx_cbstring(pdx, idc_style, m_sstyle);
//}}afx_data_map
}
begin_message_map(cfontpage, cpropertypage)
//{{afx_msg_map(cfontpage)
on_cbn_selchange(idc_font, onselchange)
on_cbn_selchange(idc_style, onselchange)
on_cbn_selchange(idc_fontsize, onselchange)
on_bn_clicked(idc_strikeout, onselchange)
on_bn_clicked(idc_underline, onselchange)
on_cbn_killfocus(idc_font, onselchange)
on_cbn_killfocus(idc_style, onselchange)
on_cbn_killfocus(idc_fontsize, onselchange)
//}}afx_msg_map
end_message_map()
/////////////////////////////////////////////////////////////////////////////
// cfontpage message handlers
bool cfontpage::oninitdialog()
{
cpropertypage::oninitdialog();
cwindowdc dc(this);
logfont logfont;
logfont.lfcharset = default_charset;
logfont.lffacename[0] = \0;
logfont.lfpitchandfamily = 0;
enumfontfamiliesex( dc.m_hdc, &logfont, (fontenumproc)fontenumproc,
(lparam) this, 0 );
// fill size combobox with "common" sizes
tchar* defaults[]={_t("8"),_t("9"),_t("10"),_t("11"),_t("12"),_t("14"),_t("16"),_t
("18"),
_t("20"), _t("22"), _t("24"),
_t("26"), _t("28"), _t("36"),
_t("48") };
for (int i = 0; i $#@60; (sizeof(defaults)/sizeof(defaults[0]));
i )
m_combosize.addstring(defaults[i]);
// fill style combobox with "common" styles
m_combostyle.addstring( _t("regular") );
m_combostyle.addstring( _t("bold") );
m_combostyle.addstring( _t("italic") );
m_combostyle.addstring( _t("bold italic") );
return true; // return true unless you set the focus to a control
// exception: ocx property pages should return false
}
int callback cfontpage::fontenumproc(enumlogfontex *lpelfe,
newtextmetricex *lpntme,
int fonttype, cfontpage* pfontpage )
{
if( pfontpage-$#@62;m_combofont.findstringexact( 0, (lpctstr)lpelfe-$#@62;elffullname)==cb_err
)
{
// add to list
pfontpage-$#@62;m_combofont.addstring( (lpctstr)lpelfe-$#@62;elffullname
);
}
return 1;
}
void cfontpage::onselchange()
{
// the selection hasnt changed yet, so change it
if( ischild( getfocus())&& getfocus()-$#@62;getparent()-$#@62;iskindof(
runtime_class( ccombobox )))
{
ccombobox *cb = (ccombobox *)getfocus()-$#@62;getparent();
cstring stext;
if( cb-$#@62;getcursel() != cb_err )
{
cb-$#@62;getlbtext( cb-$#@62;getcursel(), stext );
cb-$#@62;setwindowtext( stext );
}
}
updatedata(true);
logfont logfont;
m_fontsample.getlogfont( &logfont );
logfont.lfstrikeout = m_bstrikeout;
logfont.lfunderline = m_bunderline;
memcpy( logfont.lffacename, m_sfont, lf_facesize );
logfont.lfheight = muldiv(atoi(m_ssize), m_cypixelsperinch,
72);
logfont.lfweight = 400; //regular
logfont.lfitalic = false;
if( m_sstyle.find( _t("italic") ) != -1 )
logfont.lfitalic = true;
if( m_sstyle.find( _t("bold") ) != -1 )
logfont.lfweight = 700;
m_fontsample.deleteobject();
m_fontsample.createfontindirect( &logfont );
m_staticsample.setfont(&m_fontsample);
}
void cfontpage::getcurrentfont(lplogfont lplf)
{
m_fontsample.getlogfont( lplf );
}
p>第二步:在你的项目中包含源文件
下面给出了头文件和实现文件。你只要简单的把它们包含到你的项目中,并且使用cfontpage类就可以了。该类的实现是比较简单的。cfontpage构造函数获取一指向logfont结构的指针。它使用该结构的信息来初始化自己。如果logfont结构没有提供,则使用窗口的当前字体。在oninitdialog()函数中,我们列出了所需字体和字体组合框。但是,任何字体特性的改变,都要调用onselchange()函数来实现。所有的信息映射入口都指向该函数.onselchange()函数更新内部信息,并且显示样本文本。
fontpage.h
#if !defined(afx_fontpage_h__de7edeb3_056d_11d1_82df_e2cdc9000000__included_)
#define afx_fontpage_h__de7edeb3_056d_11d1_82df_e2cdc9000000__included_
#if _msc_ver $#@62;= 1000
#pragma once
#endif // _msc_ver $#@62;= 1000
// fontpage.h : header file
//
/////////////////////////////////////////////////////////////////////////////
//
cfontpage dialog
class cfontpage : public cpropertypage
{
declare_dyncreate(cfontpage)
// construction
public:
void getcurrentfont(lplogfont lplf);
cfontpage(logfont* plogfont = null);
~cfontpage();
// dialog data
//{{afx_data(cfontpage)
enum { idd = idd_fontpage };
cstatic m_staticsample;
ccombobox m_combostyle;
ccombobox m_combosize;
ccombobox m_combofont;
bool m_bstrikeout;
bool m_bunderline;
cstring m_sfont;
cstring m_ssize;
cstring m_sstyle;
//}}afx_data
// overrides
// classwizard generate virtual function overrides
//{{afx_virtual(cfontpage)
protected:
virtual void dodataexchange(cdataexchange* pdx); // ddx/ddv
support
//}}afx_virtual
// implementation
protected:
// generated message map functions
//{{afx_msg(cfontpage)
virtual bool oninitdialog();
afx_msg void onselchange();
//}}afx_msg
declare_message_map()
private:
cfont fontsample;
int m_cypixelsperinch;
static int callback fontenumproc(enumlogfontex *lpelfe, newtextmetricex
*lpntme,
int fonttype, cfontpage* pfontpage );
};
//{{afx_insert_location}}
// microsoft developer studio will insert additional declarations
immediately
// before the previous line.
#endif // !defined(afx_fontpage_h__de7edeb3_056d_11d1_82df_e2cdc9000000__included_)
fontpage.cpp
// fontpage.cpp : implementation file
//
#include "stdafx.h"
#include "resource.h"
#include "fontpage.h"
#ifdef _debug
#define new debug_new
#undef this_file
static char this_file[] = __file__;
#endif
/////////////////////////////////////////////////////////////////////////////
// cfontpage property page
implement_dyncreate(cfontpage, cpropertypage)
cfontpage::cfontpage(logfont* plogfont /*= null*/) : cpropertypage(cfontpage::idd)
{
//{{afx_data_init(cfontpage)
//}}afx_data_init
logfont logfont;
cwindowdc dc(getdesktopwindow() );
m_cypixelsperinch = getdevicecaps(dc, logpixelsy);
if( plogfont == null )
{
cfont *pfont = dc.getcurrentfont();
pfont-$#@62;getlogfont( &logfont );
plogfont = &logfont;
}
m_fontsample.createfontindirect( plogfont );
m_bstrikeout = plogfont-$#@62;lfstrikeout;
m_bunderline = plogfont-$#@62;lfunderline;
m_sfont = plogfont-$#@62;lffacename;
m_ssize.format( "%d", muldiv(plogfont-$#@62;lfheight,
72, m_cypixelsperinch) );
m_sstyle = _t("regular");
if( plogfont-$#@62;lfweight $#@62;= 700 && plogfont-$#@62;lfitalic)
m_sstyle = _t("bold italic");
else if( plogfont-$#@62;lfitalic )
m_sstyle = _t("italic");
else if ( plogfont-$#@62;lfweight $#@62;= 700 )
m_sstyle = _t("bold");
}
cfontpage::~cfontpage()
{
}
void cfontpage::dodataexchange(cdataexchange* pdx)
{
cpropertypage::dodataexchange(pdx);
//{{afx_data_map(cfontpage)
ddx_control(pdx, idc_sample, m_staticsample);
ddx_control(pdx, idc_style, m_combostyle);
ddx_control(pdx, idc_fontsize, m_combosize);
ddx_control(pdx, idc_font, m_combofont);
ddx_check(pdx, idc_strikeout, m_bstrikeout);
ddx_check(pdx, idc_underline, m_bunderline);
ddx_cbstring(pdx, idc_font, m_sfont);
ddx_cbstring(pdx, idc_fontsize, m_ssize);
ddv_maxchars(pdx, m_ssize, lf_facesize);
ddx_cbstring(pdx, idc_style, m_sstyle);
//}}afx_data_map
}
begin_message_map(cfontpage, cpropertypage)
//{{afx_msg_map(cfontpage)
on_cbn_selchange(idc_font, onselchange)
on_cbn_selchange(idc_style, onselchange)
on_cbn_selchange(idc_fontsize, onselchange)
on_bn_clicked(idc_strikeout, onselchange)
on_bn_clicked(idc_underline, onselchange)
on_cbn_killfocus(idc_font, onselchange)
on_cbn_killfocus(idc_style, onselchange)
on_cbn_killfocus(idc_fontsize, onselchange)
//}}afx_msg_map
end_message_map()
/////////////////////////////////////////////////////////////////////////////
// cfontpage message handlers
bool cfontpage::oninitdialog()
{
cpropertypage::oninitdialog();
cwindowdc dc(this);
logfont logfont;
logfont.lfcharset = default_charset;
logfont.lffacename[0] = \0;
logfont.lfpitchandfamily = 0;
enumfontfamiliesex( dc.m_hdc, &logfont, (fontenumproc)fontenumproc,
(lparam) this, 0 );
// fill size combobox with "common" sizes
tchar* defaults[]={_t("8"),_t("9"),_t("10"),_t("11"),_t("12"),_t("14"),_t("16"),_t
("18"),
_t("20"), _t("22"), _t("24"),
_t("26"), _t("28"), _t("36"),
_t("48") };
for (int i = 0; i $#@60; (sizeof(defaults)/sizeof(defaults[0]));
i )
m_combosize.addstring(defaults[i]);
// fill style combobox with "common" styles
m_combostyle.addstring( _t("regular") );
m_combostyle.addstring( _t("bold") );
m_combostyle.addstring( _t("italic") );
m_combostyle.addstring( _t("bold italic") );
return true; // return true unless you set the focus to a control
// exception: ocx property pages should return false
}
int callback cfontpage::fontenumproc(enumlogfontex *lpelfe,
newtextmetricex *lpntme,
int fonttype, cfontpage* pfontpage )
{
if( pfontpage-$#@62;m_combofont.findstringexact( 0, (lpctstr)lpelfe-$#@62;elffullname)==cb_err
)
{
// add to list
pfontpage-$#@62;m_combofont.addstring( (lpctstr)lpelfe-$#@62;elffullname
);
}
return 1;
}
void cfontpage::onselchange()
{
// the selection hasnt changed yet, so change it
if( ischild( getfocus())&& getfocus()-$#@62;getparent()-$#@62;iskindof(
runtime_class( ccombobox )))
{
ccombobox *cb = (ccombobox *)getfocus()-$#@62;getparent();
cstring stext;
if( cb-$#@62;getcursel() != cb_err )
{
cb-$#@62;getlbtext( cb-$#@62;getcursel(), stext );
cb-$#@62;setwindowtext( stext );
}
}
updatedata(true);
logfont logfont;
m_fontsample.getlogfont( &logfont );
logfont.lfstrikeout = m_bstrikeout;
logfont.lfunderline = m_bunderline;
memcpy( logfont.lffacename, m_sfont, lf_facesize );
logfont.lfheight = muldiv(atoi(m_ssize), m_cypixelsperinch,
72);
logfont.lfweight = 400; //regular
logfont.lfitalic = false;
if( m_sstyle.find( _t("italic") ) != -1 )
logfont.lfitalic = true;
if( m_sstyle.find( _t("bold") ) != -1 )
logfont.lfweight = 700;
m_fontsample.deleteobject();
m_fontsample.createfontindirect( &logfont );
m_staticsample.setfont(&m_fontsample);
}
void cfontpage::getcurrentfont(lplogfont lplf)
{
m_fontsample.getlogfont( lplf );
}
下载本文示例代码
在属性页中添加字体对话框(续)在属性页中添加字体对话框(续)在属性页中添加字体对话框(续)在属性页中添加字体对话框(续)在属性页中添加字体对话框(续)在属性页中添加字体对话框(续)在属性页中添加字体对话框(续)在属性页中添加字体对话框(续)在属性页中添加字体对话框(续)在属性页中添加字体对话框(续)在属性页中添加字体对话框(续)在属性页中添加字体对话框(续)在属性页中添加字体对话框(续)在属性页中添加字体对话框(续)在属性页中添加字体对话框(续)