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

📄 ch02.htm

📁 VC使用大全。里面集合了VC使用的各种使用技巧。非常有用。
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<P>When the resource is complete, bring up ClassWizard by choosing <U>V</U>iew, <U>C</U>lassWizard. ClassWizard recognizes that this new dialog box resource does not have a class associated with it and offers to build one for you, as shown in Figure 2.5. 
Leave the Create a New Class radio button selected, and click OK. The New Class dialog box appears, as shown in Figure 2.6. Fill in the class name as <B><font color="#008000">CSdiDialog</font></B> and click OK. ClassWizard creates a new class, prepares the 
source file (<font color="#008000">SdiDialog.cpp</font>) and header file (<font color="#008000">SdiDialog.h</font>), and adds them to your project.</P>

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

<P><I>ClassWizard makes sure you don't forget to create a class to go with your new dialog box resource.</I></P>

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

<P><I>Creating a dialog box class is simple with ClassWizard.</I></P>

<P>You connect the dialog box resources to your code with the Member Variables tab of ClassWizard, shown in Figure 2.7. Click <font color="#008000">IDC_CHECK1</font> and then click the <U>A</U>dd Variables button. This brings up the Add Member Variable 
dialog box, shown in Figure 2.8.</P>

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

<P><I>The Member Variables tab of ClassWizard connects dialog box controls to dialog box class member </I><I>variables.</I></P>

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

<P><I>You choose the name for the member variable associated with each control.</I></P>

<P>A member variable in the new dialog box class can be connected to a control's value, or to the control itself. This sample demonstrates both kinds of connection. For <font color="#008000">IDC_CHECK1</font>, fill in the variable name as <B><font 
color="#008000">m_check</font></B>, and make sure that the <U>C</U>ategory drop-down box has Value selected. If you open the Variable <U>t</U>ype drop-down box, you will see that the only possible choice is <font color="#008000">BOOL</font>. Since a check 
box can be either selected or not selected, it can be connected only to a <font color="#008000">BOOL</font> variable, which holds the value <font color="#008000">TRUE</font> or <font color="#008000">FALSE</font>.</P>

<P>Here are the data types that go with each control type:</P>

<ul>

<li> <B>Edit box.</B> Usually a string, but also can be other data types including <font color="#008000">int</font>, <font color="#008000">float</font>, and <font color="#008000">long</font></pre>

<li> <B>Check box.</B> <font color="#008000">int</font></pre>

<li> <B>Radio button.</B> <font color="#008000">int</font></pre>

<li> <B>List box.</B> String</P>

<li> <B>Combo box.</B> String</P>

<li> <B>Scroll bar.</B> <font color="#008000">int</font></pre>

</ul>

<P>Connect <font color="#008000">IDC_EDIT1</font> in the same way, to a member variable called <font color="#008000">m_edit</font> of type <font color="#008000">CString</font> as a Value. Connect <font color="#008000">IDC_LIST1</font> as a Control to a 
member variable called <font color="#008000">m_listbox</font> of type <font color="#008000">CListBox</font>. Connect <font color="#008000">IDC_RADIO1</font>, the first of the group of radio buttons, as a Value to an <font color="#008000">int</font> member 
variable called <font color="#008000">m_radio</font>.</P>

<P>After you click OK to add the variable, ClassWizard offers, for some kinds of variables, the capability to validate the user's data entry. For example, when an edit control is selected, a field under the list of variables allows you to set the maximum 
number of characters the user can enter into the edit box (see Figure 2.9). Set it to 10 for <font color="#008000">m_edit</font>. If the edit box is connected to a number (<font color="#008000">int</font> or <font color="#008000">float</font>), this area 
of ClassWizard is used to specify minimum or maximum values for the number entered by the user. The error messages asking the user to try again are generated automatically by MFC with no work on your part.</P>

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

<P><I>Enter a number in the Ma</I><I><U>x</U></I><I>imum Characters field to limit the length of a user's entry.</I></P>

<H3><A ID="I13" NAME="I13"><A ID="I14" NAME="I14"><B>Using the Dialog Box Class</B></A></A></H3>

<P>Now that you have your dialog box resource built and your dialog box class written, you can create objects of that class within your program and display the associated dialog box element. The first step is to decide what will cause the dialog box to 
display. Typically it is a menu choice, but since adding menu items and connecting them to code are not covered until <A HREF="index09.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index09.htm" target="text">Chapter 9</A>, &quot;Building a Complete Application: ShowString,&quot; you can simply have the dialog 
box display when the application starts running. To display the dialog box, you call the <font color="#008000">DoModal()</font> member function of the dialog box class.</P>

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

<P ALIGN="CENTER">

<CENTER><I>Modeless Dialog Boxes</I></CENTER></P>

<P>Most of the dialog boxes you will code will be <I>modal</I> dialog boxes. A modal dialog box is on top of all the other windows in the application: the user must deal with the dialog box and then close it before going on to other work. An example of 
this is the dialog box that comes up when the user chooses <U>F</U>ile, <U>O</U>pen in any Windows application.</P>

<P>A <I>modeless</I> dialog box allows the user to click the underlying application and do some other work and then return to the dialog box. An example of this is the dialog box that comes up when the user chooses <U>E</U>dit, <U>F</U>ind in many Windows 
applications.</P>

<P>Displaying a modeless dialog box is more difficult than displaying a modal one. The dialog box object, the instance of the dialog box class, must be managed carefully. Typically it is created with <font color="#008000">new</font> and destroyed with 
<font color="#008000">delete</font> when the user closes the dialog box with Cancel or OK. You have to override a number of functions within the dialog box class. In short, you should be familiar and comfortable with modal dialog boxes before you attempt 
to use a modeless dialog box. When you're ready, look at the Visual C++ sample called MODELESS that comes with Developer Studio. The fastest way to open this sample is by searching for MODELESS in InfoViewer. Searching in InfoViewer is covered in Reference 
C, &quot;Developer Studio Menus and Commands.&quot;</P>

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

<P><A ID="I15" NAME="I15"><A ID="I16" NAME="I16"><B>Arranging to Display the Dialog</B></A></A><B> Box</B></P>

<P>Select the ClassView in the project workspace pane, expand the SDI Classes item, and then expand CSdiApp. Double-click the <font color="#008000">InitInstance()</font> member function. This function is called whenever the application starts. Scroll down 
to the very end of the function, and just before the return at the end of the function, add the lines in Listing 2.1.</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.1&#151;</I>SDI.CPP<I>&#151;Lines to Add at the End of</I><I><font color="#008000"> </font></I><I><font color="#008000">CSdiApp::InitInstance()</font></I></P>

<pre><font color="#008000">     CSdiDialog dlg;</font></pre>

<pre><font color="#008000">     dlg.m_check = TRUE;</font></pre>

<pre><font color="#008000">     dlg.m_edit = &quot;hi there&quot;;</font></pre>

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

<pre><font color="#008000">     if (dlg.DoModal() == IDOK)</font></pre>

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

<pre><font color="#008000">          msg = &quot;You clicked OK. &quot;;</font></pre>

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

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

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

<pre><font color="#008000">          msg = &quot;You cancelled. &quot;;</font></pre>

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

<pre><font color="#008000">     msg += &quot;Edit box is: &quot;;</font></pre>

<pre><font color="#008000">     msg += dlg.m_edit;</font></pre>

<pre><font color="#008000">     AfxMessageBox (msg);</font></pre>

<P>This code first creates an instance of the dialog box class. It sets the check box and edit box to simple default values. (The list box and radio buttons are a little more complex and are added later in this chapter, in &quot;Using a List Box 
Control&quot; and &quot;Using Radio Buttons.&quot;) The dialog box displays on-screen by calling its <font color="#008000">DoModal()</font> function, which returns a number represented by <font color="#008000">IDOK</font> if the user clicks OK and <font 
color="#008000">IDCANCEL</font> if the user clicks Cancel. The code then builds a message and displays it with the <font color="#008000">AfxMessageBox</font> function.</P>

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

<P>The <font color="#008000">CString</font> class has a number of useful member functions and operator overloads. As you see here, the <font color="#008000">+=</font> operator tacks characters onto the end of a string. For more about the <font 
color="#008000">CString</font> class, 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;</P>

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

<P>At the very beginning of the file, add this directive:</P>

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

<P>This makes sure that the compiler knows what a <font color="#008000">CSdiDialog</font> class is when it compiles this file. Build the project by choosing <U>B</U>uild, <U>B</U>uild, or by clicking the Build button on the Build toolbar. Run the 
application by choosing <U>B</U>uild, <U>E</U>xecute, or by clicking the Execute Program button on the Build toolbar. You will see that the dialog box displays with the default values you just coded, as shown in Figure 2.10. Change them, and click OK. You 
should get a message box telling you what you did, like the one in Figure 2.11. Now the program sits there, ready to go, but since there is no more for it to do, you can close it by choosing <U>F</U>ile, E<U>x</U>it, or by clicking the X in the top-right 
corner.</P>

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

<P><I>Your application displays the dialog box when it first runs.</I></P>

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

<P><I>After you click OK, the application echoes the contents of the edit control.</I></P>

<P>Run it again, change the contents of the edit box, and this time click Cancel on the dialog box. Notice in Figure 2.12 that the edit box is reported as still &quot;hi there&quot;&#151;this is because MFC does not copy the control values into the member 
variables when the user clicks Cancel. Again just close the application after the dialog box is gone.</P>

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

<P><I>When you click Cancel, the application ignores any changes you made.</I></P>

<P>Be sure to try entering more characters into the edit box than the 10 you specified with ClassWizard. You will find you cannot type more than 10 characters&#151;the system just beeps at you. If you try to paste in something longer than 10 characters, 
only the first 10 characters appear in the edit box.</P>

<P><A ID="I17" NAME="I17"><A ID="I18" NAME="I18"><B>Behind the Scenes</B></A></A></P>

<P>You may be wondering what's going on here. When you click OK on the dialog box, MFC arranges for a function called <font color="#008000">OnOK()</font> to be called. This function is inherited from <font color="#008000">CDialog</font>, the base class 
for <font color="#008000">CSdiDialog</font>. Among other things it calls a function called <font color="#008000">DoDataExchange()</font>, which ClassWizard wrote for you. Here's how it looks at the moment:</P>

⌨️ 快捷键说明

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