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

📄 ch02.htm

📁 VC使用大全。里面集合了VC使用的各种使用技巧。非常有用。
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<pre><font color="#008000">     int index = m_listbox.GetCurSel();</font></pre>

<pre><font color="#008000">     if (index != LB_ERR)</font></pre>

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

<pre><font color="#008000">          m_listbox.GetText(index, m_selected);</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">          m_selected = &quot;&quot;;</font></pre>

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

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

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

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

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

<P>This code calls the list box member function <font color="#008000">GetCurSel()</font>, which returns a constant represented by <font color="#008000">LB_ERR</font> if there is no selection or if more than one string has been selected. Otherwise it 
returns the zero-based index of the selected string. The <font color="#008000">GetText()</font> member function fills <font color="#008000">m_selected</font> with the string at position <font color="#008000">index</font>. After filling this member 
variable, this function calls the base class <font color="#008000">OnOK()</font> function to do the other processing required.</P>

<P>In a moment you will add lines to <font color="#008000">CSdiApp::InitInstance()</font> to mention the selected string in the message box. Those lines will execute whether the user clicks OK or Cancel, so you need to add a function to handle the user's 
clicking Cancel. Simply follow the numbered steps for adding OnOK, except that you choose ID_CANCEL from the top-right box and agree to call the function OnCancel. The code, as shown in Listing 2.4, resets <font color="#008000">m_selected</font> since the 
user canceled the dialog box.</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.4&#151;</I>SDIDIALOG.CPP<I>&#151;</I><I><font color="#008000">CSdiDialog::OnCancel()</font></I></P>

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

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

<pre><font color="#008000">     m_selected = &quot;&quot;;</font></pre>

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

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

<P>Add these lines to <font color="#008000">CSdiApp::InitInstance()</font> just before the call to <font color="#008000">AfxMessageBox()</font>:</P>

<pre><font color="#008000">     msg += &quot;. List Selection: &quot;;</font></pre>

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

<P>Build the application, run it, and test it. Does it work as you expect? Does it resemble Figure 2.17?</P>

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

<P><I>Your application now displays strings in the list box.</I></P>

<P><A ID="I21" NAME="I21"><A ID="I22" NAME="I22"><B>Using Radio buttons</B></A></A></P>

<P>You may have already noticed that when the dialog box first appears on-screen, none of the radio buttons are selected. You can arrange for one of them to be selected by default: simply add two lines to <font 
color="#008000">CSdiDialog::OnInitDialog()</font>. These lines set the second radio button and saves the change to the dialog box:</P>

<pre><font color="#008000">     m_radio = 1;</font></pre>

<pre><font color="#008000">     UpdateData(FALSE);</font></pre>

<P>You may recall that <font color="#008000">m_radio</font> is the member variable to which the group of radio buttons is connected. It is a zero-based index into the group of buttons, indicating which one is selected. Button 1 is the second button. The 
call to <font color="#008000">UpdateData()</font> refreshes the dialog box controls with the member variable values. The parameter indicates the direction of transfer: <font color="#008000">UpdateData(TRUE)</font> would refresh the member variables with 
the control values, wiping out the setting of <font color="#008000">m_radio</font> you just made.</P>

<P>Unlike list boxes, a group of radio buttons can be accessed after the dialog box is no longer on-screen, so you won't need to add code to <font color="#008000">OnOK()</font> or <font color="#008000">OnCancel()</font>. But you have a problem: how to 
convert the integer selection into a string to tack on the end of <font color="#008000">msg</font>. There are lots of approaches, including the <font color="#008000">Format()</font> function of <font color="#008000">CString</font>, but in this case, since 
there are not many possible selections, a <font color="#008000">switch</font> statement is readable and quick. At the end of <font color="#008000">CSdiApp::InitInstance()</font>, add the lines in Listing 2.5 just before the call to <font 
color="#008000">AfxMessageBox()</font>.</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.5&#151;</I>SDIDIALOG.CPP<I>&#151;Lines to Add to </I><I><font color="#008000">CSdiApp::InitInstance()</font></I></P>

<pre><font color="#008000">     msg += &quot;\r\n&quot;;</font></pre>

<pre><font color="#008000">     msg += &quot;Radio Selection: &quot;;</font></pre>

<pre><font color="#008000">     switch (dlg.m_radio)</font></pre>

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

<pre><font color="#008000">     case 0:</font></pre>

<pre><font color="#008000">          msg += &quot;0&quot;;</font></pre>

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

<pre><font color="#008000">     case 1:</font></pre>

<pre><font color="#008000">          msg += &quot;1&quot;;</font></pre>

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

<pre><font color="#008000">     case 2:</font></pre>

<pre><font color="#008000">          msg += &quot;2&quot;;</font></pre>

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

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

<pre><font color="#008000">          msg += &quot;none&quot;;</font></pre>

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

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

<P>The first new line adds two special characters to the message. Return, represented by <font color="#008000">\r</font>, and new line, represented by <font color="#008000">\n</font>, combine to form the Windows end-of-line marker. This adds a line break 
after the part of the message you have built so far. The rest of <font color="#008000">msg</font> will appear on the second line of the message box. The <font color="#008000">switch</font> statement is an ordinary piece of C++ code, which was also present 
in C. It executes one of the <font color="#008000">case</font> statements, depending on the value of <font color="#008000">dlg.m_radio</font>.</P>

<P>Once again, build and test the application. Any surprises? It should resemble Figure 2.18. You are going to be building and using dialog boxes throughout this book, so take the time to understand how this application works and what it does. You may 
want to step through it with the debugger and watch it in action: you can read all about debugging in <A HREF="indexc.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/indexc.htm" target="text">Appendix C</A>, &quot;Debugging.&#148;</P>

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

<P><I>Your application now selects button Two by default.</I></P>

<H3><A ID="I23" NAME="I23"><A ID="I24" NAME="I24"><B>From Here</B></A></A><B>...</B></H3>

<P>To add a dialog box to your application, you need to build a dialog box resource and a dialog box class and to connect them with ClassWizard. Then your application calls member functions of the dialog box class, or of the individual controls to use the 
dialog box. You may have to catch messages in order to gain full control of your dialog box.</P>

<P>Almost every sample application in this book features one or more dialog boxes. Some of the chapters that show you the power of dialog boxes include the following:</P>

<ul>

<li> <A HREF="index03.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index03.htm" target="text">Chapter 3</A>, &quot;Windows 95 Common Controls,&quot; provides an overview of the many special controls that you can use in Windows 95 applications.</P>

<li> <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; covers messages and virtual function overrides in more detail, so you'll know what is was you did with messages in this chapter.</P>

<li> <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; shows how to bring up a dialog box when the user chooses a menu item, and how to use the control values in your application.</P>

<li> <A HREF="index12.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index12.htm" target="text">Chapter 12</A>, &quot;Property Pages and Sheets and Wizards,&quot; shows you how to use property sheets, which are nothing more than a special kind of dialog box, and wizards, which are simply special property 
sheets.</P>

</ul>

<p><hr></p>

<center>

<p><font size=-2>

&copy; 1997, QUE Corporation, an imprint of Macmillan Publishing USA, a

Simon and Schuster Company.</font></p>

</center>

</BODY></HTML>

⌨️ 快捷键说明

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