📄 ch05.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
<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 += '</head>';
zhtm += '<BODY bgcolor="#FFFFFF">
<!-- Spidersoft WebZIP Ad Banner Insert -->
<TABLE width=100% border="0" cellpadding="0" cellspacing="0">
<TR>
<TD>
<ILAYER id=ad1 visibility=hidden height=60></ILAYER>
<NOLAYER>
<IFRAME SRC="http://www.spidersoft.com/ads/bwz468_60.htm" width="100%" height="60" marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=0 scrolling=no></IFRAME>
</NOLAYER>
</TD>
</TR>
</TABLE>
<!-- End of Spidersoft WebZIP Ad Banner Insert-->
';
zhtm += '<IMG SRC="' + fullPath + pPage + '">';
zhtm += '<P><B>' + pPage + '</B>';
zhtm += '
<layer src="http://www.spidersoft.com/ads/bwz468_60.htm" visibility=hidden id=a1 width=600 onload="moveToAbsolute(ad1.pageX,ad1.pageY); a1.clip.height=60;visibility='show';"></layer>
</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">
<META NAME="GENERATOR" Content="Symantec Visual Page Mac 1.1.1">
<TITLE>Teach Yourself Visual C++ 6 in 21 Days -- Ch 5 -- Getting User Feedback--Adding Dialog Boxes to Your Application</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<H1 ALIGN="CENTER"><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM"
BORDER="0"><BR>
Teach Yourself Visual C++ 6 in 21 Days</H1>
<CENTER>
<P><A HREF="../ch04/ch04.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch06/ch06.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>
<H1 ALIGN="CENTER">- 5 -<BR>
Getting User Feedback--Adding Dialog Boxes to Your Application</H1>
<H1></H1>
<UL>
<LI><A HREF="#Heading1">Using Pre-existing (or System) Dialog Windows</A>
<UL>
<LI><A HREF="#Heading2">Using Message Boxes</A>
<LI><A HREF="#Heading3">Using Common Dialogs</A>
</UL>
<LI><A HREF="#Heading4">Creating Your Own Dialog Windows</A>
<UL>
<LI><A HREF="#Heading5">Creating the Dialog Window</A>
<LI><A HREF="#Heading6">Using the Dialog in Your Application</A>
</UL>
<LI><A HREF="#Heading7">Summary</A>
<LI><A HREF="#Heading8">Q&A</A>
<LI><A HREF="#Heading9">Workshop</A>
<UL>
<LI><A HREF="#Heading10">Quiz</A>
<LI><A HREF="#Heading11">Exercises</A>
</UL>
</UL>
<P>
<HR SIZE="4">
<BR>
With most applications that you might use, there are numerous situations where the
application asks you for information--how you want the application configured or
whether you want to save your work before exiting, for example. In most of these
situations, the application opens a new window to ask these questions. These windows
are called dialog windows.</P>
<P>Dialog windows typically have one or more controls and some text explaining what
information the program needs from you. Dialog windows typically do not have a large
blank work area, as you find in the main windows of a word processor or a programming
editor. All the applications that you have built in the preceding days have been
dialog windows, and your projects will continue to be dialog windows for the next
few days.</P>
<P>All the dialogs that you have created up to now have been single window dialog
applications. Today you are going to learn</P>
<P>
<UL>
<LI>How to use dialog windows in a more flexible way.
<P>
<LI>How to call other dialog windows and take the information entered by the user
on these windows back to the main application window for use in the application.
<P>
<LI>How to use both standard dialogs, such as the message boxes you used in previous
days and custom dialogs that you have created.
</UL>
<H2><A NAME="Heading1"></A>Using Pre-existing (or System) Dialog Windows</H2>
<P>The Windows operating system provides a number of pre-existing dialog windows.
Simple dialog windows, also known as message boxes, present the user with a message
and provide one to three buttons to click. More complex dialogs, such as the File
Open, Save, or Print dialogs, are also provided with Windows. These system (or common)
dialogs are created and used with a combination of a variable declaration of a C++
class and a series of interactions with the class instance.</P>
<P>
<H3><A NAME="Heading2"></A>Using Message Boxes</H3>
<P>As you learned in the previous days, using message boxes is as simple as making
a single function call, passing the message text as the only argument. This results
in a message box that displays the message to the user with an icon and gives the
user one button to click to acknowledge the message. As you probably know from using
other Windows software, you have a whole range of other message box possibilities
with various button combinations and various icons that can be displayed.</P>
<P>
<H4>The MessageBox Function</H4>
<P>As you have seen in previous days, the MessageBox function can be passed one or
two arguments. The first argument is the message to be displayed to the user. The
second argument, which is completely optional, is displayed in the title bar on the
message box. You can use a third argument, which is also optional, to specify the
buttons to be presented to the user and the icon to be displayed beside the message.
In addition to this third argument, the MessageBox function returns a result value
that indicates which button was clicked by the user. Through the combination of the
third argument and the return value, the MessageBox function can provide a whole
range of functionality in your Visual C++ applications.</P>
<BLOCKQUOTE>
<P>
<HR>
<STRONG>NOTE:</STRONG> If you use the third argument to the MessageBox function to specify
the buttons or the icon to be presented to the user, the second argument (the message
box title) is no longer optional. You must provide a value for the title bar of the
message box.
<HR>
</BLOCKQUOTE>
<P>The button combinations that you can use in the MessageBox function are limited.
You do not have the freedom to make up your own button combination. If you get to
the point where you need to make up your own, you have to create a custom dialog
window that looks like a message box. The button combinations that you can use are
listed in Table 5.1.</P>
<P>
<H4>TABLE 5.1. MESSAGEBOX BUTTON COMBINATION IDS.</H4>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">ID </TD>
<TD ALIGN="LEFT">Buttons </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">MB_ABORTRETRYIGNORE </TD>
<TD ALIGN="LEFT">Abort, Retry, Ignore </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">MB_OK </TD>
<TD ALIGN="LEFT">OK </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">MB_OKCANCEL </TD>
<TD ALIGN="LEFT">OK, Cancel </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">MB_RETRYCANCEL </TD>
<TD ALIGN="LEFT">Retry, Cancel </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">MB_YESNO </TD>
<TD ALIGN="LEFT">Yes, No </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">MB_YESNOCANCEL </TD>
<TD ALIGN="LEFT">Yes, No, Cancel </TD>
</TR>
</TABLE>
</P>
<P>To specify the icon to be displayed, you can add the icon ID to the button combination
ID. The icons that are available are listed in Table 5.2. If you want to specify
either the icon or the button combination, and you want to use the default for the
other, you can just specify the one ID that you want to use.</P>
<P>
<H4>TABLE 5.2. MESSAGEBOX ICON IDS.</H4>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">ID </TD>
<TD ALIGN="LEFT">Icon </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">MB_ICONINFORMATION </TD>
<TD ALIGN="LEFT">Informational icon </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">MB_ICONQUESTION </TD>
<TD ALIGN="LEFT">Question mark icon </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">MB_ICONSTOP </TD>
<TD ALIGN="LEFT">Stop sign icon </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">MB_ICONEXCLAMATION </TD>
<TD ALIGN="LEFT">Exclamation mark icon </TD>
</TR>
</TABLE>
</P>
<P>When you do specify a button combination, you want to capture the return value
so that you can determine which button the user clicked. The return value is defined
as an integer data type; the return value IDs are listed in Table 5.3.</P>
<P>
<H4>TABLE 5.3. MESSAGEBOX RETURN VALUE IDS.</H4>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">ID </TD>
<TD ALIGN="LEFT">Button Clicked </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">IDABORT </TD>
<TD ALIGN="LEFT">Abort </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">IDRETRY </TD>
<TD ALIGN="LEFT">Retry </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">IDIGNORE </TD>
<TD ALIGN="LEFT">Ignore </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">IDYES </TD>
<TD ALIGN="LEFT">Yes </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">IDNO </TD>
<TD ALIGN="LEFT">No </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">IDOK </TD>
<TD ALIGN="LEFT">OK </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">
<PRE>IDCANCEL</PRE>
</TD>
<TD ALIGN="LEFT">
<PRE>Cancel</PRE>
</TD>
</TR>
</TABLE>
<H4>Creating a Dialog Application</H4>
<P>To get a good understanding of how you can use the MessageBox function in your
applications to get information from the user, you will build a simple application
that uses the MessageBox function in a couple of different ways. Your application
will have two separate buttons that call two different versions of the MessageBox
function so that you can see the differences and similarities between the various
options of the function. Later in the day, you will add a standard File Open dialog
so that you can see how the standard dialogs can be used to allow the user to specify
a filename or perform other standard functions. Finally, you will create a custom
dialog that allows the user to enter a few different types of values, and you will
see how you can read these values from the main application dialog after the user
has closed the custom dialog.</P>
<P>To start this application, follow these steps:</P>
<P>
<DL>
<DT></DT>
<DD><B>1. </B>Create a new MFC AppWizard workspace project, naming it Dialogs.
<P>
<DT></DT>
<DD><B>2. </B>Choose the same settings as for the previous days' applications, giving
the application a title of Dialogs.
<P>
<DT></DT>
<DD><B>3. </B>Lay out the main application dialog as shown in Figure 5.1 using the
properties in Table 5.4.
<P>
</DL>
<H4>TABLE 5.4. CONTROL PROPERTY SETTINGS.</H4>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">Object </TD>
<TD ALIGN="LEFT">Property </TD>
<TD ALIGN="LEFT">Setting </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">Command Button </TD>
<TD ALIGN="LEFT">ID </TD>
<TD ALIGN="LEFT">IDC_YESNOCANCEL </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT"> <P>
</TD>
<TD ALIGN="LEFT">Caption </TD>
<TD ALIGN="LEFT">&Yes, No, Cancel </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">Command Button </TD>
<TD ALIGN="LEFT">ID </TD>
<TD ALIGN="LEFT">IDC_ABORTRETRYIGNORE </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT"> <P>
</TD>
<TD ALIGN="LEFT">Caption </TD>
<TD ALIGN="LEFT">&Abort, Retry, Ignore </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">Command Button </TD>
<TD ALIGN="LEFT">ID </TD>
<TD ALIGN="LEFT">IDC_FILEOPEN </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT"> <P>
</TD>
<TD ALIGN="LEFT">Caption </TD>
<TD ALIGN="LEFT">&File Open </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">Command Button </TD>
<TD ALIGN="LEFT">ID </TD>
<TD ALIGN="LEFT">IDC_BCUSTOMDIALOG </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT"> <P>
</TD>
<TD ALIGN="LEFT">Caption </TD>
<TD ALIGN="LEFT">&Custom Dialog </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">Command Button </TD>
<TD ALIGN="LEFT">ID </TD>
<TD ALIGN="LEFT">IDC_BWHICHOPTION </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT"> <P>
</TD>
<TD ALIGN="LEFT">Caption </TD>
<TD ALIGN="LEFT">&Which Option? </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT"> <P>
</TD>
<TD ALIGN="LEFT">Disabled </TD>
<TD ALIGN="LEFT">Checked </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">Command Button </TD>
<TD ALIGN="LEFT">ID </TD>
<TD ALIGN="LEFT">IDC_EXIT </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT"> <P>
</TD>
<TD ALIGN="LEFT">Caption </TD>
<TD ALIGN="LEFT">E&xit </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">Static Text </TD>
<TD ALIGN="LEFT">ID </TD>
<TD ALIGN="LEFT">IDC_STATIC </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT"> <P>
</TD>
<TD ALIGN="LEFT">Caption </TD>
<TD ALIGN="LEFT">Dialog Results: </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">Edit Box </TD>
<TD ALIGN="LEFT">ID </TD>
<TD ALIGN="LEFT">IDC_RESULTS </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT"> <P>
</TD>
<TD ALIGN="LEFT">Multiline </TD>
<TD ALIGN="LEFT">Checked </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT"> <P>
</TD>
<TD ALIGN="LEFT">Auto Vscroll </TD>
<TD ALIGN="LEFT">Checked </TD>
</TR>
</TABLE>
</P>
<P><A HREF="javascript
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -