📄 ch02.htm
字号:
<HTML>
<HEAD>
<TITLE>Special Edition Using Visual C++ 5 - Chapter 2</TITLE>
<LINK REL="Next" HREF="ch03.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/ch03.htm">
<LINK REL="Previous" HREF="ch01.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/ch01.htm"></HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H2><B>Chapter 2</B></H2>
<H2><A ID="I1" NAME="I1"><A ID="I2" NAME="I2"><B>Dialog Boxes and Controls</B></A></A></H2>
<hr>
<ul>
<li> <B>How to create and display a dialog box resource</B></P>
<P> Creating a resource is the first step. Displaying a dialog box requires only a single function call.</P>
<li> <B>How to write a custom dialog box class</B></P>
<P> You can use MFC's dialog box classes as the starting point for your own custom classes.</P>
<li> <B>How to extract information from a dialog box</B></P>
<P> MFC features automatic data transfer to and from a dialog box.</P>
<li> <B>How to validate the contents of a dialog box's controls</B></P>
<P> Make sure that the user entered the information your application is expecting.</P>
<li> <B>About the different types of Windows controls</B></P>
<P> Controls are at the heart of any graphical user interface, enabling applications to present and retrieve information to and from the user.</P>
<li> <B>How to associate an MFC class with a control</B></P>
<P> MFC features a special class for every type of control.</P>
<li> <B>How to call control-class member functions</B></P>
<P> You manipulate controls by calling the control class's member functions.</P>
</ul>
<P>Windows programs have a graphical user interface. In the days of DOS, the program could simply print a prompt on-screen and direct the user to enter whatever value the program needed. With Windows, however, getting data from the user is not so simple,
and most user input is obtained from dialog boxes. For example, a user can give the application details about a request by typing in edit boxes, choosing from list boxes, selecting radio buttons, checking or unchecking check boxes, and more.</P>
<P>This chapter builds several small applications to illustrate the use of dialog boxes in your programs.</P>
<H3><A ID="I3" NAME="I3"><A ID="I4" NAME="I4"><B>Understanding Dialog Boxes</B></A></A></H3>
<P>Chances are that your Windows application will have several dialog boxes, each designed to retrieve a different type of information from your user. For each dialog box that appears on-screen, there are two entities you need to develop: a dialog box
<I>resource</I> and a dialog box <I>class</I>.</P>
<P>The dialog box resource is used to draw the dialog box and its controls on the screen. The class holds the values of the dialog box, and it is a member function of the class that causes the dialog box to be drawn on the screen. They work together to
achieve the overall effect: making communication with the program easier for your user.</P>
<P>You build a dialog box resource with the resource editor, adding controls to it and arranging them to make the control easy to use. Class Wizard then helps you to create a dialog box class, typically derived from the MFC class <font
color="#008000">CDialog</font>, and to connect the resource to the class. Usually each control on the dialog box resource corresponds to one member variable in the class. To display the dialog box, you call a member function of the class. To set the
control values to defaults before displaying the dialog box, or to determine the values of the controls after the user is finished with the box, you use the member variables of the class.</P>
<H3><A ID="I5" NAME="I5"><A ID="I6" NAME="I6"><B>Creating a Dialog Box Resource</B></A></A></H3>
<P>The first step in adding a dialog box to your MFC application is creating the dialog box resource, which acts as a sort of template for Windows. When Windows sees the dialog box resource in your program, it uses the commands in the resource to
construct the dialog box for you.</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>In this chapter you learn to work with dialog boxes by adding one to a simple application. You can use the SDI application that you created in <A HREF="index01.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index01.htm" target="text">Chapter 1</A> or open it from the CD. You create a dialog box resource
and a dialog box class for the application, write code to display the dialog box, and write code to use the values entered by the user.</P>
<P>To create a dialog box resource, first open the application. Choose <U>I</U>nsert, <U>R</U>esource from Developer Studio's menu bar. The Insert Resource dialog box, shown in Figure 2.1, appears. Double-click Dialog in the Resource Type box. The dialog
box editor appears, as shown in Figure 2.2.</P>
<A HREF="Cfig01.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch02/Cfig01.gif"><b>Fig. 2.1</b></A>
<P><I>Double-click Dialog on the Insert Resource dialog box.</I></P>
<A HREF="Cfig02.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch02/Cfig02.gif"><b>Fig. 2.2</b></A>
<P><I>A brand new dialog box resource has a title, an OK button, and a Cancel button.</I></P>
<P>Bring up the Properties dialog box for the new dialog box by choosing <U>V</U>iew, <U>P</U>roperties. Change the caption to <B>Sample Dialog</B>, as shown in Figure 2.3. You'll be using the Properties dialog box quite a lot as you work on this dialog
box resource, so "pin" it to the screen by clicking the pushpin in the upper left corner.</P>
<A HREF="Cfig03.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch02/Cfig03.gif"><b>Fig. 2.3</b></A>
<P><I>Use the Properties dialog box to change the title of the new dialog box.</I></P>
<P>The control palette shown at the far right of Figure 2.2 is used to add controls to the dialog box resource. Dialog boxes are built and changed with a very visual WYSIWYG interface. If you need a button on your dialog box, you grab one from the control
palette, drop it where you want it, and change the caption from Button1 to Lookup, or Connect, or whatever you want the button to read. All the familiar Windows controls are available for your dialog boxes:</P>
<ul>
<li> <B>Static text.</B> Not really a control, this is used to label other controls such as edit boxes.</P>
<li> <B>Edit boxes.</B> Single line or multiline, these are places for users to type strings or numbers as input to the program.</P>
<li> <B>Buttons.</B> Every dialog box starts with OK and Cancel buttons, but you can add as many of your own as you want.</P>
<li> <B>Check boxes.</B> Used to set options on or off, each option can be selected or deselected independently.</P>
<li> <B>Radio buttons.</B> These are used to select only one of a number of related options. Selecting one button deselects the rest.</P>
<li> <B>List box.</B> This box type is used to select one item from a list hardcoded into the dialog box or filled in by the program as the dialog box is created. The user cannot type in the selection area.</P>
<li> <B>Combo box.</B> A combination of an edit box and a list box, this control allows users to select from a list, or type their response if the one they want isn't on the list.</P>
</ul>
<P>The sample application in this chapter is going to have a dialog box with a selection of controls on it, to demonstrate the way they are used.</P>
<P><A ID="I7" NAME="I7"><A ID="I8" NAME="I8"><B>Defining Dialog Box and Control IDs</B></A></A></P>
<P>Because dialog boxes are often unique to an application (with the exception of the common dialog boxes), you almost always create your own IDs for both the dialog box and the controls it contains. You can, if you like, accept the default IDs that the
dialog box editor creates for you. However, these IDs are generic (for example, <font color="#008000">IDD_DIALOG1</font>, <font color="#008000">IDC_EDIT1</font>, <font color="#008000">IDC_RADIO1</font>, and so on), and so you'll probably want to change
them to something more specific. In any case, as you can tell from the default IDs, a dialog box's ID usually begins with the prefix <font color="#008000">IDD</font>, and control IDs usually begin with the prefix <font color="#008000">IDC</font>. You
change these IDs in the Properties dialog box: click the control (or the dialog box background to select the entire background), and choose <U>E</U>dit, <U>P</U>roperties; then change the resource ID to a descriptive name that starts with <font
color="#008000">IDD</font> for a dialog and <font color="#008000">IDC</font> for a control.</P>
<P><A ID="I9" NAME="I9"><A ID="I10" NAME="I10"><B>Creating the Sample Dialog</B></A></A><B> Box</B></P>
<P>Click the Edit box button on the control palette, and then click in the upper-left corner of the dialog box to place the edit box. If necessary, grab a moving handle and move it until it is in roughly the same place as the edit box in Figure 2.4.
Normally, you would change the ID from Edit1, but for this sample leave it unchanged.</P>
<A HREF="Cfig04.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch02/Cfig04.gif"><b>Fig. 2.4</b></A>
<P><I>You can build a simple dialog box quickly in the resource editor.</I></P>
<blockquote><p><img src="tip.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/tip.gif">
<P>If you aren't sure which control palette button inserts an edit box (or any other type of control,) just hold the pointer still over one of the buttons for a short time. A ToolTip will appear reminding you of the name of the control associated with the
button. Move the pointer from button to button until you find the one for the edit box.</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<P>Add a check box and three radio buttons to the dialog box so that it resembles Figure 2.4. Change the captions on the radio buttons to <B>One</B>, <B>Two</B>, and <B>Three</B>. To align all these controls, click one, and then while holding down the
Ctrl key, click each of the rest of them. Choose <U>L</U>ayout, <U>A</U>lign Controls, <U>L</U>eft, and if necessary drag the stack of controls over with the mouse while they are all selected. Then choose <U>L</U>ayout, <U>S</U>pace Evenly, <U>D</U>own, to
adjust the vertical spacing.</P>
<blockquote><p><img src="tip.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/tip.gif">
<P>The commands on the <U>L</U>ayout menu are also on the Dialog toolbar, which appears at the bottom of your screen while you are using the resource editor. The toolbar symbols are repeated on the menu to help you learn which button is associated with
each menu item.</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<P>Click the One radio button again and bring up the Properties dialog box. Select the <U>G</U>roup check box. This indicates that this is the first of a group of buttons. When you select a radio button, all the other buttons in the group are
deselected.</P>
<P>Add a list box to the dialog box, to the right of the radio buttons, and resize it to match Figure 2.4. With the list box highlighted, choose <U>V</U>iew <U>P</U>roperties to bring up the Properties dialog box. Select the Styles tab and make sure that
the Sort box is not selected. When this box is selected, the strings in your list box are automatically presented in alphabetical order. For this application, they should be presented in the order that they are added.</P>
<H3><A ID="I11" NAME="I11"><A ID="I12" NAME="I12"><B>Writing a Dialog Box Class</B></A></A></H3>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -