?? ch04.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>Teach Yourself Visual C++® 5 in 24 Hours -- Hour 4 -- Dialog Boxes and C++ Classes</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<CENTER>
<H1><IMG SRC="sams.gif" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"><BR>
<FONT COLOR="#000077">Teach Yourself Visual C++® 5 in 24 Hours</FONT></H1>
</CENTER>
<CENTER>
<P><A HREF="ch03.htm" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/ch03/ch03.htm"><IMG SRC="previous.gif" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="ch05.htm" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/ch05/ch05.htm"><IMG
SRC="next.gif" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"
BORDER="0"></A><A HREF="index-1.htm" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/index.htm"><IMG SRC="contents.gif" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/button/contents.gif" WIDTH="128"
HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A>
<HR>
</CENTER>
<CENTER>
<H1><FONT COLOR="#000077">- Hour 4 -<BR>
Dialog Boxes and C++ Classes</FONT></H1>
</CENTER>
<P>In this hour, you will learn about two fundamental concepts used when creating
Windows programs using C++: object-oriented design and dialog boxes. The following
topics are covered in this hour:
<UL>
<LI>An introduction to object-oriented design concepts<BR>
<BR>
<LI>Using dialog boxes in your Windows applications<BR>
<BR>
<LI>Creating dialog-based projects<BR>
<BR>
<LI>Adding controls to dialog boxes<BR>
<BR>
<LI>Creating new classes using ClassWizard
</UL>
<P>Also during this hour, you will create two sample projects that demonstrate how
you can use dialog boxes in your applications.
<H2><FONT COLOR="#000077"><B>What Is Object-Oriented Design?</B></FONT></H2>
<P>One of the design goals for the C++ language was to provide a language that supported
object-oriented programming and design. <I>Object-oriented design</I> involves classifying
real-world objects and actions as classes that can be created, manipulated, and destroyed.</P>
<P>The data that makes up an object, and the functions that are performed on that
object, are combined to form a <I>class</I>, or a description of that object. Classes
can inherit functionality from other objects, and you easily can add new classes
that leverage existing classes.
<BLOCKQUOTE>
<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>Object-oriented
programming is not new; the first language to support object-oriented programming,
Simula, has been around since the mid-1960s.
<HR>
</BLOCKQUOTE>
<H3><FONT COLOR="#000077"><B>Why Use Object-Oriented Design?</B></FONT></H3>
<P>In traditional, structured design, the data manipulated by a program and the functions
that manipulate the data are separate. Reusing parts of a design built with structured
design techniques often is difficult unless the new design is very similar to the
old design.</P>
<P>Object-oriented design is useful because it can be used to create designs that
can be reused and extended. A design, or a portion of a design, can be reused in
future programs much like a hardware component, such as a computer chip, a disk drive,
or a sound card. Object-oriented designs describe the object's class completely,
so each class is easily reused because the data and functions described by the class
are integrated.</P>
<P>Because you can hide the implementation of a class behind an interface, changing
the implementation details of a class without affecting users of that class--as long
as the interface doesn't change--is easy. For example, the Tab control was not available
in versions of Windows before Windows 95. The MFC <TT>CPropertyPage</TT> class was
rewritten for MFC 4.0 to take advantage of the new Tab control without impacting
users of that class, except that the class is now more efficient.
<H3><FONT COLOR="#000077"><B>Describing Objects in a Class</B></FONT></H3>
<P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>In C++, objects are described
by a <I>class</I>. A class is just a description of the object that can be created
and the actions that can be performed on it.</P>
<P>A C++ class has two main parts:
<UL>
<LI>The <I>class declaration</I>. This contains the class interface and information
about data members for the class. The class interface usually is located in a header
file having a <TT>.H</TT> suffix. Any file in your program that uses the class must
use the <TT>#include</TT> directive so that the class declaration is added to the
source file by the preprocessor.<BR>
<BR>
<LI>The <I>class implementation</I>. This includes all the member functions that
have been declared as part of the class. The class implementation usually is located
in a file that has a <TT>.CPP</TT> suffix.
</UL>
<H2><FONT COLOR="#000077"><B>What Is a Dialog Box?</B></FONT></H2>
<P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>A <I>dialog box</I> is a
specialized window that is used to provide feedback or collect input from the user.
Dialog boxes come in all shapes and sizes, ranging from simple message boxes that
display single lines of text to large dialog boxes that contain sophisticated controls.</P>
<P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>The most commonly used type
of dialog box is a <I>modal dialog box</I>. A modal dialog box prevents the user
from performing any other activity with the program until the dialog box is dismissed.</P>
<P>Dialog boxes are also used for one-way communication with a user, such as "splash
screens" used to display copyright and startup information as a program is launched.
The opening screen displayed by the Visual C++ Developer Studio and Microsoft Word
are two examples of dialog boxes used for one-way communication. Dialog boxes are
sometimes used to notify the user about the progress of a lengthy operation.
<BLOCKQUOTE>
<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>Dialog boxes provide
a convenient way for users to interact with Windows programs. Users expect most interaction
with a Windows program to take place through dialog boxes. All dialog boxes have
certain things in common; these common characteristics make the user's life easier,
because users don't need to learn and relearn how dialog boxes work from program
to program.
<HR>
</BLOCKQUOTE>
<P>There are several different types of dialog boxes, and each of them has a specific
purpose. This hour covers three main types of dialog boxes:
<UL>
<LI>Message boxes<BR>
<BR>
<LI>Modal dialog boxes<BR>
<BR>
<LI>Modeless dialog boxes
</UL>
<H3><FONT COLOR="#000077"><B>Understanding Message Boxes</B></FONT></H3>
<P>The simplest type of dialog box is the message box, which is used to display information.
This type of dialog box is so simple you can call it with just one line of code using
the MFC class library. For example, to display a message box using default parameters
supplied by MFC, just use this line:</P>
<PRE><FONT COLOR="#0066FF"><TT>AfxMessageBox( "Hello World" );</TT>
</FONT></PRE>
<P>This line of code creates a message box with an exclamation mark inside a yellow
triangle. There are several additional options for the icon displayed in a message
box, as you will see later.
<H3><FONT COLOR="#000077"><B>Using Dialog Boxes for Input</B></FONT></H3>
<P>When most people think of dialog boxes, they think of the dialog boxes that collect
input from a user. Dialog boxes are often used to contain controls that are used
to handle user input. You can include in a dialog box a wide range of controls. In
fact, a major portion of this book covers the various types of controls available
in Windows.</P>
<P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>Some dialog boxes are needed
so often in Windows programs that they have been included as part of the operating
system. These dialog boxes, known as <I>common dialog boxes</I>, are available by
calling a function and don't require you to create a dialog box resource. There are
common dialog boxes for opening and selecting files, choosing fonts and colors, and
performing find and replace operations. Many of the common dialog boxes are covered
later in the book. For example, in Hour 13, "Fonts," you will use a common
dialog box to select a font.</P>
<P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>A dialog box that is <I>modeless</I>
enables other activities to be carried out while the dialog box is still open.</P>
<P>An example of a modeless dialog box is the Find and Replace common dialog box
used by Developer Studio. When the dialog box is open, you can still make selections
from the main menu, and even open other dialog boxes. In contrast, all other Developer
Studio dialog boxes are modal. As long as they are open, the user cannot interact
with the other parts of Developer Studio.
<H2><FONT COLOR="#000077"><B>How Are Dialog Boxes Used?</B></FONT></H2>
<P>Developer Studio makes using dialog boxes in a Windows program easy. All the necessary
steps are automated, and the tools used to create the dialog box and include it in
a project are all integrated.
<H3><FONT COLOR="#000077"><B>Adding Message Boxes</B></FONT></H3>
<P>As discussed earlier, you can add message boxes to your program using a single
line of code. You must supply at least a single parameter: the text that is displayed
inside the dialog box. Optionally, you can also specify an icon style and a button
arrangement pattern. The types of icons that are available for message boxes are
shown in Figure 4.1.</P>
<P><A NAME="01"></A><A HREF="01.htm" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/ch04/01.htm"><B>Figure 4.1.</B></A> <BR>
<I>Icons you can include in a message box.</I></P>
<P>Each of the icons in Figure 4.1 has a specific meaning. When most Windows programs
display a message box, they use a standard icon for each message. When programs use
the same icons consistently, users find it much easier to understand the meanings
of information provided with message boxes. The meaning and style name for each icon
is shown in Table 4.1.
<H4><FONT COLOR="#000077">Table 4.1. Icons used in Windows message-box dialog boxes.</FONT></H4>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><B>Icon Displayed</B></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><B>Meaning</B></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><B>Message Box Style</B></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">Exclamation mark</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Warning</TD>
<TD ALIGN="LEFT" VALIGN="TOP"><TT>MB_ICONEXCLAMATION</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">An "i" in a circle</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Information</TD>
<TD ALIGN="LEFT" VALIGN="TOP"><TT>MB_ICONINFORMATION</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">Question mark</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Question</TD>
<TD ALIGN="LEFT" VALIGN="TOP"><TT>MB_ICONQUESTION</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">Stop sign</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Error</TD>
<TD ALIGN="LEFT" VALIGN="TOP"><TT>MB_ICONSTOP</TT></TD>
</TR>
</TABLE>
</P>
<P>In addition, you can specify a button arrangement to be used in the message box.
By default, a single button labeled OK is included in the message box. However, sometimes
it's convenient to ask a user a simple question and collect an answer. One use for
these button arrangements is to ask the user what action to take during an error.
For example, the following code displays a message box that contains a question mark
icon and asks the user whether the current file should be deleted:</P>
<PRE><FONT COLOR="#0066FF"><TT>int nChoice = AfxMessageBox( "Overwrite existing file?",</TT>
<TT> MB_YESNOCANCEL | MB_ICONQUESTION );</TT>
<TT>if( nChoice == IDYES )</TT>
<TT>{</TT>
<TT> // Overwrite file</TT>
<TT>}</TT>
</FONT></PRE>
<P>The user can choose between buttons marked Yes, No, and Cancel. Table 4.2 gives
the different button arrangements possible for a message box.
<H4><FONT COLOR="#000077">Table 4.2. Button arrangements.</FONT></H4>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><B>Message Box Style</B></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><B>Buttons Included in Dialog Box</B></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>MB_ABORTRETRYIGNORE</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">Abort, Retry, and Ignore</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>MB_OK</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">OK</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>MB_OKCANCEL</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">OK and Cancel</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>MB_RETRYCANCEL</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">Retry and Cancel</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>MB_YESNO</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">Yes and No</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>MB_YESNOCANCEL</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">Yes, No, and Cancel</TD>
</TR>
</TABLE>
</P>
<P>The message-box return value indicates the button selected by the user. Table
4.3 is a list of possible return values and the choice made by the user.
<H4><FONT COLOR="#000077">Table 4.3. Message-box return values.</FONT></H4>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><B>Return Value</B></TD>
<TD ALIGN="LEFT"><B>Button Pressed</B></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>IDABORT</TT></TD>
<TD ALIGN="LEFT">Abort</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>IDCANCEL</TT></TD>
<TD ALIGN="LEFT">Cancel</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>IDIGNORE</TT></TD>
<TD ALIGN="LEFT">Ignore</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>IDNO</TT></TD>
<TD ALIGN="LEFT">No</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>IDOK</TT></TD>
<TD ALIGN="LEFT">OK</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>IDRETRY</TT></TD>
<TD ALIGN="LEFT">Retry</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>IDYES</TT></TD>
<TD ALIGN="LEFT">Yes</TD>
</TR>
</TABLE>
<H3><FONT COLOR="#000077"><B>Using the Bitwise <TT>OR</TT> Operator</B></FONT></H3>
<P>In an earlier code fragment, a vertical bar was used to separate two different
options for the <TT>AfxMessageBox</TT> function. The vertical bar is the <I>bitwise
<TT>OR</TT> operator</I>, which is used to combine the bit patterns of two or more
values.</P>
<P>Unlike adding two operands, the values are combined "bitwise," meaning
that the two values are compared bit by bit. If a particular bit is high in either
operand, the result will have that bit enabled. If a particular bit is low in both
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -