?? ch02.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function popUp(pPage) {
var fullURL = document.location;
var textURL = fullURL.toString();
var URLlen = textURL.length;
var lenMinusPage = textURL.lastIndexOf("/");
lenMinusPage += 1;
var fullPath = textURL.substring(0,lenMinusPage);
popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394');
figDoc= popUpWin.document;
zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>';
zhtm += '<link rel="stylesheet" href="/includes/stylesheets/ebooks.css"></head>';
zhtm += '<BODY bgcolor="#FFFFFF">';
zhtm += '<IMG SRC="' + fullPath + pPage + '">';
zhtm += '<P><B>' + pPage + '</B>';
zhtm += '</BODY></HTML>';
window.popUpWin.document.write(zhtm);
window.popUpWin.document.close();
// Johnny Jackson 4/28/98
}
//-->
</SCRIPT>
<link rel="stylesheet" href="/includes/stylesheets/ebooks.css">
<TITLE>Special Edition Using Visual C++ 6 -- Ch 2 -- Dialogs and Controls</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<CENTER>
<H1><IMG SRC="../button/que.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"><BR>
Special Edition Using Visual C++ 6</H1>
</CENTER>
<CENTER>
<P><A HREF="../ch01/ch01.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch03/ch03.htm"><IMG
SRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"
BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"
HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A>
<HR>
</CENTER>
<CENTER>
<H1>- 2 -</H1>
<H1>Dialogs and Controls</H1>
</CENTER>
<UL>
<LI><A HREF="#Heading1">Understanding Dialog Boxes</A>
<LI><A HREF="#Heading2">Creating a Dialog Box Resource</A>
<UL>
<LI><A HREF="#Heading3">Defining Dialog Box and Control IDs</A>
<LI><A HREF="#Heading4">Creating the Sample Dialog Box</A>
</UL>
<LI><A HREF="#Heading5">Writing a Dialog Box Class</A>
<LI><A HREF="#Heading6">Using the Dialog Box Class</A>
<UL>
<LI><A HREF="#Heading7">Arranging to Display the Dialog Box</A>
<LI><A HREF="#Heading8">Behind the Scenes</A>
<LI><A HREF="#Heading9">Using a List Box Control</A>
<LI><A HREF="#Heading10">Using Radio Buttons</A>
</UL>
</UL>
<P>
<HR SIZE="4">
<CENTER>
<H1><B></B></H1>
</CENTER>
<H2><A NAME="Heading1"></A>Understanding Dialog Boxes</H2>
<P>Windows programs have a graphical user interface. In the days of DOS, the program
could simply print a prompt onscreen and direct the user to enter whatever value
the program needed. With Windows, however, getting data from the user is not as 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. These components
of a dialog box are called <I>controls</I>.</P>
<P>Chances are that your Windows application will have several dialog boxes, each
designed to retrieve a specific type of information from your user. For each dialog
box that appears onscreen, there are two entities you need to develop: a <I>dialog
box resource</I> and a <I>dialog box</I> <I>class</I>.</P>
<P>The dialog box resource is used to draw the dialog box and its controls onscreen.
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 onscreen. 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 CDialog, 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>
<P>
<H2><A NAME="Heading2"></A>Creating a Dialog Box Resource</H2>
<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>In this chapter you learn to work with dialog boxes by adding one to a simple
application. Create an SDI application just as you did in Chapter 1, "Building
Your First Windows Application," calling it simply SDI. You will 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 Insert, Resource
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>
<P>Bring up the Properties dialog box for the new dialog box by choosing View, Properties.
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>
<P><A HREF="javascript:popUp('02uvc01.gif')"><B>FIG. 2.1</B></A><B> </B><I>Double-click
Dialog on the Insert Resource dialog box.</I></P>
<P><A HREF="javascript:popUp('02uvc02.gif')"><B>FIG. 2.2</B></A><B> </B><I>A brand
new dialog box resource has a title, an OK button, and a Cancel button.</I></P>
<P><A HREF="javascript:popUp('02uvc03.gif')"><B>FIG. 2.3</B></A><B> </B><I>Use the
Dialog 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>
<P>
<UL>
<LI><I>Static text. </I>Not really a control, this is used to label other controls
such as edit boxes.
<P>
<LI><I>Edit box.</I> Single line or multiline, this is a place for users to type
strings or numbers as input to the program. Read-only edit boxes are used to display
text.
<P>
<LI><I>Button. </I>Every dialog box starts with OK and Cancel buttons, but you can
add as many of your own as you want.
<P>
<LI><I>Check box.</I> You use this control to set options on or off; each option
can be selected or deselected independently.
<P>
<LI><I>Radio button. </I>You use this to select only one of a number of related options.
Selecting one button deselects the rest.
<P>
<LI><I>List box.</I> You use this box type 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><I>Combo box.</I> A combination of an edit box and a list box, this control enables
users to select from a list or type their response, if the one they want isn't on
the list.
</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>
<H3><A NAME="Heading3"></A>Defining Dialog Box and Control IDs</H3>
<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 want, accept the default IDs that
the dialog box editor creates for you. However, these IDs are generic (for example,
IDD_DIALOG1, IDC_EDIT1, IDC_RADIO1, and so on), 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 IDD, and control IDs usually begin
with the prefix IDC. You change these IDs in the Properties dialog box: Click the
control (or the dialog box background to select the entire background), and choose
View, Properties unless the Properties dialog box is already pinned in place; then
change the resource ID to a descriptive name that starts with IDD for a dialog and
IDC for a control.</P>
<P>
<H3><A NAME="Heading4"></A>Creating the Sample Dialog Box</H3>
<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 approximately 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>
<P><A HREF="javascript:popUp('02uvc04.gif')"><B>FIG. 2.4</B></A><B> </B><I>You can
build a simple dialog box quickly in the resource editor.</I></P>
<P><I></I>
<BLOCKQUOTE>
<P>
<HR>
<strong>TIP:</strong> 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.
<HR>
</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 Layout, Align, Left, and if
necessary drag the stack of controls over with the mouse while they are all selected.
Then choose Layout, Space Evenly, Down, to adjust the vertical spacing.</P>
<BLOCKQUOTE>
<P>
<HR>
<strong>TIP:</strong> The commands on the Layout 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.
<HR>
</BLOCKQUOTE>
<P>Click the One radio button again and bring up the Properties dialog box. Select
the Group 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 View, Properties to
bring up the Properties dialog box if it is not still pinned in place. 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>
<P>
<H2><A NAME="Heading5"></A>Writing a Dialog Box Class</H2>
<P>When the resource is complete, bring up ClassWizard by choosing View, ClassWizard.
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 classname as <B>CSdiDialog</B> and click OK.
ClassWizard creates a new class, prepares the source file (SdiDialog.cpp) and header
file (SdiDialog.h), and adds them to your project.</P>
<P><A HREF="javascript:popUp('02uvc05.gif')"><B>FIG. 2.5</B></A><B> </B><I>ClassWizard
makes sure you don't forget to create a class to go with your new dialog box resource.</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 IDC_CHECK1 and then click the Add Variable
button. This brings up the Add Member Variable dialog box, shown in Figure 2.8.</P>
<P><A HREF="javascript:popUp('02uvc06.gif')"><B>FIG. 2.6</B></A><B> </B><I>Creating
a dialog box class is simple with ClassWizard.</I></P>
<P><A HREF="javascript:popUp('02uvc07.gif')"><B>FIG. 2.7</B></A><B> </B><I>The Member
Variables tab of ClassWizard connects dialog box controls to dialog box class member
variables.</I></P>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -