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

📄 ch02.htm

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

<pre><font color="#008000">void CSdiDialog::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(CSdiDialog)</font></pre>

<pre><font color="#008000">     DDX_Control(pDX, IDC_LIST1, m_listbox);</font></pre>

<pre><font color="#008000">     DDX_Check(pDX, IDC_CHECK1, m_check);</font></pre>

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

<pre><font color="#008000">     DDV_MaxChars(pDX, m_edit, 10);</font></pre>

<pre><font color="#008000">     DDX_Radio(pDX, IDC_RADIO1, m_radio);</font></pre>

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

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

<P>The functions with names that start <font color="#008000">DDX</font> all perform data exchange: their second parameter is the resource ID of a control, and the third parameter is a member variable in this class. This is the way that ClassWizard 
connected the controls to member variables: by generating this code for you. Remember that ClassWizard also added these variables to the dialog box class by generating code in the header file that declares them.</P>

<P>There are 34 functions whose names start <font color="#008000">DDX</font>: one for each type of data that might be exchanged between a dialog box and a class. Each has the type in its name. For example, <font color="#008000">DDX_Check</font> is used to 
connect a check box to a <font color="#008000">BOOL</font> member variable. <font color="#008000">DDX_Text</font> is used to connect an edit box to a <font color="#008000">CString</font> member variable. ClassWizard chooses the right function name when you 
make the connection.</P>

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

<P>There are some <font color="#008000">DDX</font> functions that are not generated by ClassWizard. For example, when you connect a list box as a Value, your only choice for type is <font color="#008000">CString</font>. Choosing that causes ClassWizard to 
generate a call to <font color="#008000">DDX_LBString()</font>, which connects the selected string in the list box to a <font color="#008000">CString</font> member variable. There are cases when the integer index into the list box might be more useful, and 
there is a <font color="#008000">DDX_LBIndex()</font> function that performs that exchange. You can add code to <font color="#008000">DoDataExchange()</font>, outside the special ClassWizard comments, to make this connection. If you do so, remember to add 
the member variable to the class yourself. You can find the full list of <font color="#008000">DDX</font> functions in the online documentation.</P>

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

<P>Functions with names that start <font color="#008000">DDV</font> perform data validation. ClassWizard adds a call to <font color="#008000">DDV_MaxChars</font> right after the call to <font color="#008000">DDX_Text</font> that filled <font 
color="#008000">m_edit</font> with the contents of <font color="#008000">IDC_EDIT1</font>. The first parameter of the call is the member variable name and the second is the limit: how many characters can be in the string. If a user ever managed to get 
extra characters into a length-validated string, the <font color="#008000">DDV_MaxChars()</font> function contains code that puts up a warning box and gets the user to try again. You can just set the limit and count on its being enforced.</P>

<P><A ID="I19" NAME="I19"><A ID="I20" NAME="I20"><B>Using a List Box Control</B></A></A></P>

<P>Dealing with the list box is more difficult, because only while the dialog box is on-screen is the list box control a real window. You cannot call a member function of the list box control class unless the dialog box is on-screen. (This is true of any 
control that you access as a control rather than as a value.) This means that you must initialize the list box (fill it with strings) and use it (determine which string is selected) in functions that are called by MFC while the dialog box is on-screen.</P>


<P>When it is time to initialize the dialog box, just before it displays on-screen, a <font color="#008000">CDialog</font> function called <font color="#008000">OnInitDialog()</font> is called. While the full explanation of what you are about to do will 
have to wait until <A HREF="index04.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index04.htm" target="text">Chapter 4</A>, &quot;Messages and Commands,&quot; follow the steps below to add the function to your class.</P>

<P>In ClassView, right-click <font color="#008000">CSdiDialog</font> and choose Add Windows Message Handler. The New Windows Message and Event Handlers dialog box shown in Figure 2.13 appears. Choose <font color="#008000">WM_INITDIALOG</font> from the 
list and click <U>A</U>dd Handler. The message name disappears from the left list and appears in the right list. Click it and then click Edit E<U>x</U>isting to see the code.</P>

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

<P><I>The New Windows Message and Event Handlers dialog box helps you override </I><I><font color="#008000">OnInitDialog()</font></I><I>.</I></P>

<P>Remove the <font color="#008000">TODO</font> comment and add calls to the member functions of the list box so that the function is as shown in Listing 2.2.</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 2.2&#151;</I>SDIDIALOG.CPP<I>&#151;</I><I><font color="#008000">CSdiDialog::OnInitDialog()</font></I></P>

<pre><font color="#008000">BOOL CSdiDialog::OnInitDialog()</font></pre>

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

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

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

<pre><font color="#008000">     m_listbox.AddString(&quot;First String&quot;);</font></pre>

<pre><font color="#008000">     m_listbox.AddString(&quot;Second String&quot;);</font></pre>

<pre><font color="#008000">     m_listbox.AddString(&quot;Yet Another String&quot;);</font></pre>

<pre><font color="#008000">     m_listbox.AddString(&quot;String Number Four&quot;);</font></pre>

<pre><font color="#008000">     m_listbox.SetCurSel(2);</font></pre>

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

<pre><font color="#008000">     return TRUE;  // return TRUE unless you set the focus to a control</font></pre>

<pre><font color="#008000">                   // EXCEPTION: OCX Property Pages should return FALSE</font></pre>

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

<P>This function starts by calling the base class version of <font color="#008000">OnInitDialog()</font> to do whatever behind-the-scenes work MFC does when dialog boxes are initialized. Then it calls the list box member function <font 
color="#008000">AddString()</font> which, as you can probably guess, adds a string to the list box. <font color="#008000">The </font><font color="#008000">strings will be displayed to the user in the order that they were added </font><font 
color="#008000">with AddString()</font>. The final call is to <font color="#008000">SetCurSel()</font>,<font color="#008000"> </font>which sets the current selection. As you see when you run this program, the index you pass to <font 
color="#008000">SetCurSel()</font> is zero-based, which means that item 2 is the third in the list, counting: 0, 1, 2.</P>

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

<P>Typically the strings of a list box are not hardcoded like this. To set them from elsewhere in your program, you have to add a <font color="#008000">CStringArray</font> member variable to the dialog box class and a function to add strings to that 
array. The <font color="#008000">OnInitDialog()</font> would use the array to fill the list box. Alternatively, you can use another one of MFC's collection classes or even fill the list box from a database. For more about <font 
color="#008000">CStringArray</font> and other MFC collection classes, consult <A HREF="indexe.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/indexe.htm" target="text">Appendix E</A>, &quot;Useful Classes.&quot; Database programming is covered in <A HREF="index22.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index22.htm" target="text">Chapter 22</A>, 
&quot;Database Access.&quot;</P>

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

<P>In order to have the message box display some indication of what was selected in the list box, you have to add another member variable to the dialog box class. This member variable will be set as the dialog box closes and can be accessed after it is 
closed. In ClassView, right-click <font color="#008000">CSdiDialog</font> and choose Add Member <U>V</U>ariable. Fill in the dialog box, as shown in Figure 2.14, and click OK. This adds the declaration of the <font color="#008000">CString</font> called 
<font color="#008000">m_selected</font> to the header file for you. (If the list box allowed multiple selections, you would have to use a <font color="#008000">CStringArray</font> to hold the list of selected items.) Strictly speaking, the variable should 
be private, and you should either add a public accessor function or make <font color="#008000">CSdiApp::InitInstance()</font> a friend function to <font color="#008000">CSdiDialog</font> in order to be truly object oriented. Here you take an excusable 
shortcut. The general rule still holds: member variables should be private.</P>

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

<P><I>Add a </I><I><font color="#008000">CString</font></I><I> to your class to hold the string that was selected in the list box.</I></P>

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

<P>Object-oriented concepts like accessor functions, friend functions, and the reasoning behind private member variables.</P>

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

<P>This new member variable is used to hold the string that was selected by the user. It is set when the user clicks OK or Cancel. To add a function that is called when the user clicks OK, follow these steps:</P>

<ol>

<li><P> Right-click <font color="#008000">CSdiDialog</font> in the ClassView, and choose Add Windows Message <U>H</U>andler.</P>

<li><P> In the New Windows Message and Event Handlers dialog box, shown in Figure 2.15, highlight ID_OK in the list box at the lower right, labeled <U>C</U>lass or Object to Handle.</P>

</ol>

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

<P><I>Add a function to handle the user's clicking OK on your dialog box.</I></P>

<ol start=3> 

<li><P> In the right-hand list box, select BN_CLICKED. You are adding a function to &quot;handle&quot; the user's clicking the OK button once.</P>

<li><P> Click the Add Handler button. The Add Member Function dialog box shown in Figure 2.16 appears.</P>

</ol>

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

<P><I>ClassWizard suggests a very good name for this event handler: do not change it.</I></P>

<ol start=5>

<li><P> Accept the suggested name, <font color="#008000">OnOK()</font>, by clicking OK.</P>

<li><P> Click the Edit Existing button to edit the code, and add lines so that it is as shown in Listing 2.3.</P>

</ol>

<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 2.3&#151;</I>SDIDIALOG.CPP<I>&#151;</I><I><font color="#008000">CSdiDialog::OnOK()</font></I></P>

<pre><font color="#008000">void CSdiDialog::OnOK()</font></pre>

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

⌨️ 快捷键说明

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