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

📄 在属性页中添加字体对话框.htm

📁 VC的一些技巧性文档
💻 HTM
字号:
<html><head><meta http-equiv="content-type" content="text/html; charset=gb2312"><meta name="generator" content="microsoft frontpage 3.0"><title>在属性页中添加字体对话框</title><meta name="microsoft theme" content="none"><meta name="microsoft border" content="none"></head><body background="di2001.jpg"><font size="3"><h1 align="center"></font><font size="6">在属性页中添加字体对话框</font><fontsize="3"></h1><p align="center">张圣华</font></p><hr width="90%" size="1" color="#008080"><blockquote>  <font size="3"><p align="justify">&nbsp;&nbsp;&nbsp; mfc提供了三种属性页类:cfontproppage、ccolorproppage和cpictureproppage。但是,它们只能与ole   automation一起使用。在本文中将介绍如何在属性对话框中添加字体属性页。</p>  <p align="justify">&nbsp;&nbsp;&nbsp;   解决的办法是你自己建立字体属性页。为了让你能定制通用对话框的行为,sdk提供了这些对话框的对话框资源。“choose   font”对话框是在include目录中的font.dlg文件中,相应的符号在dlgs.h文件中。使用这些文件作为出发点,我们可以为字体属性页建立资源,并且添加一个使用这些资源的cpropertypage继承类。下面的实现可能是有限的的,但也有一定的参考价值。如果你想增强它,可以查看mfc源程序目录中的cfontproppage源文件。</p>  </font></blockquote><font size="3"><blockquote>  <h4 align="justify">第一步:添加对话资源</h4>  <p align="justify">&nbsp;&nbsp;&nbsp; 你可以在devstudio中打开下面的rc文件devstudio,然后把对话框资源复制到你的主资源文件中,或者你也可以把该资源文件包含进去。</p>  <p align="justify">&nbsp;&nbsp;&nbsp; 为了复制资源文件,请在devstudio中打开“fontpage.rc”文件,然后展开资源的outline。然后打开resourceview,以便对话框资源是可见的。最后,从fontpage.rc中拖动idd_fontpage资源到你的应用程序resourceview的资源中。</p>  <p align="justify">&nbsp;&nbsp;&nbsp; 如果你想包含fontpage资源文件,只要把“fontpage.rc”和“fontpagerc.h”文件(在下面列出)到项目目录中的“res”目录中即可。然后在该目录中已有的“rc2”文件中添加下面这段。资源图像也列在下面。</p>  <dl>    <dd><pre>#include &quot;fontpage.rc&quot;	</pre>    </dd>    <dd><pre>listing of fontpage.rc </pre>    </dd>    <dd><pre>/////////////////////////////////////////////////////////////////////////// listing of fontpage.rc//</pre>    </dd>    <dd><pre>#include &quot;winresrc.h&quot;</pre>    </dd>    <dd><pre>#include &quot;fontpagerc.h&quot;</pre>    </dd>    <dd><pre>idd_fontpage dialog discardable 13, 54, 264, 133</pre>    </dd>    <dd><pre>style ws_child | ws_caption | ws_sysmenu</pre>    </dd>    <dd><pre>caption &quot;font&quot;</pre>    </dd>    <dd><pre>font 8, &quot;helv&quot;</pre>    </dd>    <dd><pre>begin</pre>    </dd>    <dd><pre>ltext &quot;&amp;font:&quot;,stc1,6,3,40,9</pre>    </dd>    <dd><pre>combobox idc_font,6,13,131,54,cbs_simple | cbs_autohscroll | </pre>    </dd>    <dd><pre>cbs_sort | cbs_disablenoscroll | ws_vscroll | ws_tabstop</pre>    </dd>    <dd><pre>ltext &quot;font st&amp;yle:&quot;,stc2,153,3,44,9</pre>    </dd>    <dd><pre>combobox idc_style,153,13,64,54,cbs_simple | cbs_disablenoscroll | </pre>    </dd>    <dd><pre>ws_vscroll | ws_tabstop</pre>    </dd>    <dd><pre>ltext &quot;&amp;size:&quot;,stc3,224,3,30,9</pre>    </dd>    <dd><pre>combobox idc_fontsize,224,13,32,54,cbs_simple | </pre>    </dd>    <dd><pre>cbs_disablenoscroll | ws_vscroll | ws_tabstop</pre>    </dd>    <dd><pre>groupbox &quot;effects&quot;,grp1,6,72,84,34,ws_group</pre>    </dd>    <dd><pre>control &quot;stri&amp;keout&quot;,idc_strikeout,&quot;button&quot;,bs_autocheckbox | </pre>    </dd>    <dd><pre>ws_tabstop,10,82,49,10</pre>    </dd>    <dd><pre>control &quot;&amp;underline&quot;,idc_underline,&quot;button&quot;,bs_autocheckbox,10,</pre>    </dd>    <dd><pre>94,51,10</pre>    </dd>    <dd><pre>groupbox &quot;sample&quot;,grp2,98,72,160,49,ws_group</pre>    </dd>    <dd><pre>ctext &quot;aabbyyzz&quot;,idc_sample,104,81,149,37,ss_noprefix</pre>    </dd>    <dd><pre>end</pre>    </dd>    <dd><pre>/////////////////////////////////////////////////////////////////////////// listing of fontpagerc.h /////////////////////////////////////////////////////////////////////////</pre>    </dd>    <dd><pre>#define idc_strikeout 0x0410#define idc_underline 0x0411#define grp1 0x0430#define grp2 0x0431#define stc1 0x0440#define stc2 0x0441#define stc3 0x0442#define idc_sample 0x0444#define idc_font 0x0470#define idc_style 0x0471#define idc_fontsize 0x0472#define idd_fontpage 1543</pre>    </dd>    <dd><pre>/////////////////////////////////////////////////////////////////////////</pre>    </dd>  </dl>  <h4 align="justify">第二步:在你的项目中包含源文件</h4>  <p align="justify">&nbsp;&nbsp;&nbsp;   下面给出了头文件和实现文件。你只要简单的把它们包含到你的项目中,并且使用cfontpage类就可以了。该类的实现是比较简单的。cfontpage构造函数获取一指向logfont结构的指针。它使用该结构的信息来初始化自己。如果logfont结构没有提供,则使用窗口的当前字体。在oninitdialog()函数中,我们列出了所需字体和字体组合框。但是,任何字体特性的改变,都要调用onselchange()函数来实现。所有的信息映射入口都指向该函数。onselchange()函数更新内部信息,并且显示样本文本。</p>  <p align="justify"><strong>fontpage.h</strong></p>  <dl>    <dd><pre>#if !defined(afx_fontpage_h__de7edeb3_056d_11d1_82df_e2cdc9000000__included_)#define afx_fontpage_h__de7edeb3_056d_11d1_82df_e2cdc9000000__included_</pre>    </dd>    <dd><pre>#if _msc_ver &gt;= 1000#pragma once#endif // _msc_ver &gt;= 1000</pre>    </dd>    <dd><pre>// fontpage.h : header file//</pre>    </dd>    <dd><pre>/////////////////////////////////////////////////////////////////////////////// cfontpage dialog</pre>    </dd>    <dd><pre>class cfontpage : public cpropertypage{    declare_dyncreate(cfontpage)</pre>    </dd>    <dd><pre>// construction</pre>    </dd>    <dd><pre>public:    void getcurrentfont(lplogfont lplf);    cfontpage(logfont* plogfont = null);    ~cfontpage();</pre>    </dd>    <dd><pre>// dialog data//{{afx_data(cfontpage)enum { idd = idd_fontpage };cstatic	m_staticsample;ccombobox	m_combostyle;ccombobox	m_combosize;ccombobox	m_combofont;</pre>    </dd>    <dd><pre>bool	m_bstrikeout;bool	m_bunderline;</pre>    </dd>    <dd><pre>cstring	m_sfont;cstring	m_ssize;cstring	m_sstyle;//}}afx_data</pre>    </dd>    <dd><pre>// overrides// classwizard generate virtual function overrides//{{afx_virtual(cfontpage)protected:virtual void dodataexchange(cdataexchange* pdx); // ddx/ddv support//}}afx_virtual</pre>    </dd>    <dd><pre>// implementationprotected:// generated message map functions//{{afx_msg(cfontpage)virtual bool oninitdialog();afx_msg void onselchange();//}}afx_msg</pre>    </dd>    <dd><pre>declare_message_map()private:cfont m_fontsample;int m_cypixelsperinch;static int callback fontenumproc(enumlogfontex *lpelfe, newtextmetricex *lpntme,                                  int fonttype, cfontpage* pfontpage );</pre>    </dd>    <dd><pre>};</pre>    </dd>    <dd><pre>//{{afx_insert_location}}</pre>    </dd>    <dd><pre>// microsoft developer studio will insert additional declarations immediately // before the previous line.</pre>    </dd>    <dd><pre>#endif // !defined(afx_fontpage_h__de7edeb3_056d_11d1_82df_e2cdc9000000__included_)</pre>    </dd>  </dl>  <p align="justify"><strong>fontpage.cpp</strong></p>  <dl>    <dd><pre>// fontpage.cpp : implementation file//</pre>    </dd>    <dd><pre>#include &quot;stdafx.h&quot;#include &quot;resource.h&quot;#include &quot;fontpage.h&quot;</pre>    </dd>    <dd><pre>#ifdef _debug#define new debug_new#undef this_filestatic char this_file[] = __file__;#endif</pre>    </dd>    <dd><pre>/////////////////////////////////////////////////////////////////////////////// cfontpage property page</pre>    </dd>    <dd><pre>implement_dyncreate(cfontpage, cpropertypage)</pre>    </dd>    <dd><pre>cfontpage::cfontpage(logfont* plogfont /*= null*/) : cpropertypage(cfontpage::idd){    //{{afx_data_init(cfontpage)    //}}afx_data_init</pre>    </dd>    <dd><pre>    logfont logfont;</pre>    </dd>    <dd><pre>    cwindowdc dc(getdesktopwindow() );</pre>    </dd>    <dd><pre>    m_cypixelsperinch = getdevicecaps(dc, logpixelsy);</pre>    </dd>    <dd><pre>    if( plogfont == null )    {        cfont *pfont = dc.getcurrentfont();        pfont-&gt;getlogfont( &amp;logfont );        plogfont = &amp;logfont;    }</pre>    </dd>    <dd><pre>    m_fontsample.createfontindirect( plogfont );    m_bstrikeout = plogfont-&gt;lfstrikeout;    m_bunderline = plogfont-&gt;lfunderline;    m_sfont = plogfont-&gt;lffacename;    m_ssize.format( &quot;%d&quot;, muldiv(plogfont-&gt;lfheight, 72, m_cypixelsperinch) );    m_sstyle = _t(&quot;regular&quot;);</pre>    </dd>    <dd><pre>    if( plogfont-&gt;lfweight &gt;= 700 &amp;&amp; plogfont-&gt;lfitalic)        m_sstyle = _t(&quot;bold italic&quot;);    else if( plogfont-&gt;lfitalic )        m_sstyle = _t(&quot;italic&quot;);    else if ( plogfont-&gt;lfweight &gt;= 700 )        m_sstyle = _t(&quot;bold&quot;);}</pre>    </dd>    <dd><pre>cfontpage::~cfontpage(){}</pre>    </dd>    <dd><pre>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}</pre>    </dd>    <dd><pre>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_mapend_message_map()</pre>    </dd>    <dd><pre>/////////////////////////////////////////////////////////////////////////////// cfontpage message handlers</pre>    </dd>    <dd><pre>bool cfontpage::oninitdialog() {    cpropertypage::oninitdialog();</pre>    </dd>    <dd><pre>    cwindowdc dc(this);</pre>    </dd>    <dd><pre>    logfont logfont;</pre>    </dd>    <dd><pre>    logfont.lfcharset = default_charset;</pre>    </dd>    <dd><pre>    logfont.lffacename[0] = '\0';</pre>    </dd>    <dd><pre>    logfont.lfpitchandfamily = 0;</pre>    </dd>    <dd><pre>    enumfontfamiliesex( dc.m_hdc, &amp;logfont, (fontenumproc)fontenumproc, (lparam) this, 0 );</pre>    </dd>    <dd><pre>    // fill size combobox with &quot;common&quot; sizes</pre>    </dd>    <dd><pre>    tchar* defaults[]={_t(&quot;8&quot;),_t(&quot;9&quot;),_t(&quot;10&quot;),_t(&quot;11&quot;),_t(&quot;12&quot;),_t(&quot;14&quot;),_t(&quot;16&quot;),_t (&quot;18&quot;),         _t(&quot;20&quot;), _t(&quot;22&quot;), _t(&quot;24&quot;), _t(&quot;26&quot;), _t(&quot;28&quot;), _t(&quot;36&quot;), _t(&quot;48&quot;) };</pre>    </dd>    <dd><pre>    for (int i = 0; i &lt; (sizeof(defaults)/sizeof(defaults[0])); i++)        m_combosize.addstring(defaults[i]);</pre>    </dd>    <dd><pre>    // fill style combobox with &quot;common&quot; styles    m_combostyle.addstring( _t(&quot;regular&quot;) );    m_combostyle.addstring( _t(&quot;bold&quot;) );    m_combostyle.addstring( _t(&quot;italic&quot;) );    m_combostyle.addstring( _t(&quot;bold italic&quot;) );</pre>    </dd>    <dd><pre>    return true; // return true unless you set the focus to a control</pre>    </dd>    <dd><pre>    // exception: ocx property pages should return false}</pre>    </dd>    <dd><pre>int callback cfontpage::fontenumproc(enumlogfontex *lpelfe, newtextmetricex *lpntme,                     int fonttype, cfontpage* pfontpage ){    if( pfontpage-&gt;m_combofont.findstringexact( 0, (lpctstr)lpelfe-&gt;elffullname )==cb_err )    {        // add to list        pfontpage-&gt;m_combofont.addstring( (lpctstr)lpelfe-&gt;elffullname );    }</pre>    </dd>    <dd><pre>    return 1;}</pre>    </dd>    <dd><pre>void cfontpage::onselchange() {    // the selection hasn't changed yet, so change it</pre>    </dd>    <dd><pre>    if( ischild( getfocus() ) &amp;&amp; getfocus()-&gt;getparent()-&gt;iskindof( runtime_class( ccombobox ) ) )    {        ccombobox *cb = (ccombobox *)getfocus()-&gt;getparent();</pre>    </dd>    <dd><pre>        cstring stext;</pre>    </dd>    <dd><pre>        if( cb-&gt;getcursel() != cb_err )        {            cb-&gt;getlbtext( cb-&gt;getcursel(), stext );            cb-&gt;setwindowtext( stext );        }    }</pre>    </dd>    <dd><pre>    updatedata(true);</pre>    </dd>    <dd><pre>    logfont logfont;</pre>    </dd>    <dd><pre>    m_fontsample.getlogfont( &amp;logfont );    logfont.lfstrikeout = m_bstrikeout;    logfont.lfunderline = m_bunderline;    memcpy( logfont.lffacename, m_sfont, lf_facesize );</pre>    </dd>    <dd><pre>    logfont.lfheight = muldiv(atoi(m_ssize), m_cypixelsperinch, 72);</pre>    </dd>    <dd><pre>    logfont.lfweight = 400;	//regular    logfont.lfitalic = false;    if( m_sstyle.find( _t(&quot;italic&quot;) ) != -1 )        logfont.lfitalic = true;</pre>    </dd>    <dd><pre>    if( m_sstyle.find( _t(&quot;bold&quot;) ) != -1 )        logfont.lfweight = 700;</pre>    </dd>    <dd><pre>    m_fontsample.deleteobject();    m_fontsample.createfontindirect( &amp;logfont );    m_staticsample.setfont(&amp;m_fontsample);}</pre>    </dd>    <dd><pre>void cfontpage::getcurrentfont(lplogfont lplf){    m_fontsample.getlogfont( lplf );}</pre>    </dd>  </dl></blockquote></font><hr width="90%" size="1" color="#008080"></body></html>

⌨️ 快捷键说明

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