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

📄 ch09.htm

📁 VC使用大全。里面集合了VC使用的各种使用技巧。非常有用。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
</ol>

<P>The ClassWizard dialog box has been waiting behind these other dialog boxes, and now you use it. Click the Member Variables tab and connect <font color="#008000">IDC_OPTIONS_STRING</font> to a <font color="#008000">CString</font> called <font 
color="#008000">m_string</font>, just as you connected controls to member variables of the dialog box class in <A HREF="index02.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index02.htm" target="text">Chapter 2</A>, &quot;Dialog Boxes and Controls.&quot; Click OK to close ClassWizard.</P>

<P>Perhaps you're curious about what code was created for you when ClassWizard made the class. The header file is shown in Listing 9.5.</P>

<p><img src="cd_rom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/cd_rom.gif" hspace=10>

<P><I>Listing </I><I>9</I><I>.5&#151;</I>OPTIONSDIALOG.H<I>&#151;Header File for </I>COptionsDialog</P>

<pre><font color="#008000">// OptionsDialog.h : header file</font></pre>

<pre><font color="#008000">//</font></pre>

<pre><font color="#008000">/////////////////////////////////////////////////////////////////////////////</font></pre>

<pre><font color="#008000">// COptionsDialog dialog</font></pre>

<pre><font color="#008000">class COptionsDialog : public CDialog</font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">// Construction</font></pre>

<pre><font color="#008000">public:</font></pre>

<pre><font color="#008000">    COptionsDialog(CWnd* pParent = NULL);   // standard constructor</font></pre>

<pre><font color="#008000">// Dialog Data</font></pre>

<pre><font color="#008000">    //{{AFX_DATA(COptionsDialog)</font></pre>

<pre><font color="#008000">    enum { IDD = IDD_OPTIONS };</font></pre>

<pre><font color="#008000">    CString     m_string;</font></pre>

<pre><font color="#008000">    //}}AFX_DATA</font></pre>

<pre><font color="#008000">// Overrides</font></pre>

<pre><font color="#008000">    // ClassWizard generated virtual function overrides</font></pre>

<pre><font color="#008000">    //{{AFX_VIRTUAL(COptionsDialog)</font></pre>

<pre><font color="#008000">    protected:</font></pre>

<pre><font color="#008000">    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support</font></pre>

<pre><font color="#008000">    //}}AFX_VIRTUAL</font></pre>

<pre><font color="#008000">// Implementation</font></pre>

<pre><font color="#008000">protected:</font></pre>

<pre><font color="#008000">    // Generated message map functions</font></pre>

<pre><font color="#008000">    //{{AFX_MSG(COptionsDialog)</font></pre>

<pre><font color="#008000">        // NOTE: the ClassWizard will add member functions here</font></pre>

<pre><font color="#008000">    //}}AFX_MSG</font></pre>

<pre><font color="#008000">    DECLARE_MESSAGE_MAP()</font></pre>

<pre><font color="#008000">};</font></pre>

<P>There are an awful lot of comments here to help ClassWizard find its way around in the file when the time comes to add more functionality, but there is only one member variable, <font color="#008000">m_string</font>; one constructor; and one member 
function, <font color="#008000">DoDataExchange()</font>, which gets the control value into the member variable, or vice versa. The source file is not much longer; it's shown in Listing 9.6.</P>

<p><img src="cd_rom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/cd_rom.gif" hspace=10>

<P><I>Listing </I><I>9</I><I>.6&#151;</I>OPTIONSDIALOG.CPP<I>&#151;Implementation File for </I>COptionsDialog</P>

<pre><font color="#008000">// OptionsDialog.cpp : implementation file</font></pre>

<pre><font color="#008000">//</font></pre>

<pre><font color="#008000">#include &quot;stdafx.h&quot;</font></pre>

<pre><font color="#008000">#include &quot;ShowString.h&quot;</font></pre>

<pre><font color="#008000">#include &quot;OptionsDialog.h&quot;</font></pre>

<pre><font color="#008000">#ifdef _DEBUG</font></pre>

<pre><font color="#008000">#define new DEBUG_NEW</font></pre>

<pre><font color="#008000">#undef THIS_FILE</font></pre>

<pre><font color="#008000">static char THIS_FILE[] = __FILE__;</font></pre>

<pre><font color="#008000">#endif</font></pre>

<pre><font color="#008000">/////////////////////////////////////////////////////////////////////////////</font></pre>

<pre><font color="#008000">// COptionsDialog dialog</font></pre>

<pre><font color="#008000">COptionsDialog::COptionsDialog(CWnd* pParent /*=NULL*/)</font></pre>

<pre><font color="#008000">    : CDialog(COptionsDialog::IDD, pParent)</font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">    //{{AFX_DATA_INIT(COptionsDialog)</font></pre>

<pre><font color="#008000">    m_string = _T(&quot;&quot;);</font></pre>

<pre><font color="#008000">    //}}AFX_DATA_INIT</font></pre>

<pre><font color="#008000">}</font></pre>

<pre><font color="#008000">void COptionsDialog::DoDataExchange(CDataExchange* pDX)</font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">    CDialog::DoDataExchange(pDX);</font></pre>

<pre><font color="#008000">    //{{AFX_DATA_MAP(COptionsDialog)</font></pre>

<pre><font color="#008000">    DDX_Text(pDX, IDC_OPTIONS_STRING, m_string);</font></pre>

<pre><font color="#008000">    //}}AFX_DATA_MAP</font></pre>

<pre><font color="#008000">}</font></pre>

<pre><font color="#008000">BEGIN_MESSAGE_MAP(COptionsDialog, CDialog)</font></pre>

<pre><font color="#008000">    //{{AFX_MSG_MAP(COptionsDialog)</font></pre>

<pre><font color="#008000">        // NOTE: the ClassWizard will add message map macros here</font></pre>

<pre><font color="#008000">    //}}AFX_MSG_MAP</font></pre>

<pre><font color="#008000">END_MESSAGE_MAP()</font></pre>

<P>The constructor sets the string to an empty string; this code is surrounded by special ClassWizard comments that enable it to add other variables later. The <font color="#008000">DoDataExchange()</font> function calls <font 
color="#008000">DDX_Text()</font> to transfer data from the control with the resource ID <font color="#008000">IDC_OPTIONS_STRING</font> to the member variable <font color="#008000">m_string</font>, or vice versa. This code, too, is surrounded by 
ClassWizard comments. Finally, there is an empty message map, because <font color="#008000">COptionsDialog</font> doesn't catch any messages.</P>

<P><A ID="I14" NAME="I14"><B>Catching the Message</B></A></P>

<P>The next step in building ShowString is to catch the command message sent when the user chooses <U>T</U>ools, <U>O</U>ptions. There are seven classes in ShowString: <font color="#008000">CAboutDlg</font>, <font color="#008000">CChildFrame</font>, <font 
color="#008000">CMainFrame</font>, COptionsDialog, <font color="#008000">CShowStringApp</font>, <font color="#008000">CShowStringDoc</font>, and <font color="#008000">CShowStringView</font>. Which one should catch the command? The string and the options 
will be saved in the document and displayed in the view, so one of those two classes should handle the changing of the string. The document owns the private variable and will not let the view change the string unless you implement a public function to set 
the string. So it makes the most sense to have the document catch the message.</P>

<P>To catch the message, follow these steps:</P>

<ol> 

<li><P> Bring up ClassWizard (if it is not already up).</P>

<li><P> Click the Message Maps tab.</P>

<li><P> Select <font color="#008000">CShowStringDoc</font> from the Class <U>N</U>ame drop-down list box.</P>

<li><P> Select <font color="#008000">ID_TOOLS_OPTIONS</font> from the Object <U>I</U>Ds list box on the left, and select <font color="#008000">COMMAND</font> from the Messa<U>g</U>es list box on the right.</P>

<li><P> Click <U>A</U>dd Function to add a function to handle this command.</P>

<li><P> The Add Member Function dialog box, shown in Figure 9.13, appears, giving you an opportunity to change the function name from the usual one. Do not change it; just click OK.</P>

</ol>

<A HREF="Kfigs13.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch09/Kfigs13.gif"><b>Fig. 9.13</b></A>

<P><I>ClassWizard suggests a good name for the message-catching function.</I></P>

<blockquote><p><img src="tip.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/tip.gif">

<P>You should almost never change the names that ClassWizard suggests for message catchers. If you find that you have to (perhaps because the suggested name is too long or conflicts with another function name in the same object), be sure to choose a name 
that starts with <I>On.</I></P>

<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>

<P>Click Edit Code to close ClassWizard and edit the newly added function. What happened to <font color="#008000">CShowStringDoc</font> when you arranged for the <font color="#008000">ID_TOOLS_OPTIONS</font> message to be caught? The new message map in 
the header file is shown in Listing 9.7.</P>

<p><img src="cd_rom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/cd_rom.gif" hspace=10>

<P><I>Listing </I><I>9</I><I>.7&#151;</I>SHOWSTRINGDOC.H<I>&#151;Message Map for </I>CShowStringDoc</P>

<pre><font color="#008000">// Generated message map functions</font></pre>

<pre><font color="#008000">protected:</font></pre>

<pre><font color="#008000">    //{{AFX_MSG(CShowStringDoc)</font></pre>

<pre><font color="#008000">    afx_msg void OnToolsOptions();</font></pre>

<pre><font color="#008000">    //}}AFX_MSG</font></pre>

<pre><font color="#008000">    DECLARE_MESSAGE_MAP()</font></pre>

<P>This is just declaring the function. In the source file, ClassWizard changed the message maps shown in Listing 9.8.</P>

<p><img src="cd_rom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/cd_rom.gif" hspace=10>

<P><I>Listing </I><I>9</I><I>.8&#151;</I>SHOWSTRINGDOC.CPP<I>&#151;Message Map for </I>CShowStringDoc</P>

<pre><font color="#008000">BEGIN_MESSAGE_MAP(CShowStringDoc, CDocument)</font></pre>

<pre><font color="#008000">    //{{AFX_MSG_MAP(CShowStringDoc)</font></pre>

<pre><font color="#008000">    ON_COMMAND(ID_TOOLS_OPTIONS, OnToolsOptions)</font></pre>

<pre><font color="#008000">    //}}AFX_MSG_MAP</font></pre>

<pre><font color="#008000">END_MESSAGE_MAP()</font></pre>

⌨️ 快捷键说明

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