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

📄 ch04.htm

📁 24小时学会vc++
💻 HTM
📖 第 1 页 / 共 3 页
字号:
files will be added to your project:

<UL>
	<LI>The <TT>HelloDlg.h</TT> file contains the class declaration.<BR>
	<BR>
	
	<LI>The <TT>HelloDlg.cpp</TT> file contains the class definitions.
</UL>

<H3><FONT COLOR="#000077"><B>Adding a Message Handler for <TT>WM_INITDIALOG</TT></B></FONT></H3>
<P>Dialog boxes receive the <TT>WM_INITDIALOG</TT> message from the operating system
when all the controls owned by the dialog box have been created. Most dialog boxes
use the <TT>WM_INITDIALOG</TT> message to perform any initialization that is needed.</P>
<P>After you have added the <TT>CHelloDlg</TT> class to the HelloSDI project, you
can use ClassWizard to add a message-handling function for messages such as <TT>WM_INITDIALOG</TT>.</P>
<P>To add a message handler for <TT>WM_INITDIALOG</TT>, follow these steps:

<DL>
	<DD>1. Open ClassWizard by pressing Ctrl+W or by right-clicking in a source code
	window and selecting ClassWizard from the menu.<BR>
	<BR>
	2. Select the tab labeled Message Maps and select from the Class Name combo box the
	class that will handle the message--in this case, <TT>CHelloDlg</TT>.<BR>
	<BR>
	3. Select the object that is generating the message from the Object ID list box--in
	this case, <TT>CHelloDlg</TT>. A list of messages sent to the dialog box will be
	displayed in the Messages list box.<BR>
	<BR>
	4. Select the <TT>WM_INITDIALOG</TT> message from the Messages list box and click
	the Add Function button. ClassWizard will automatically add the <TT>OnInitDialog</TT>
	function to the <TT>CHelloDlg</TT> class.<BR>
	<BR>
	5. Click OK to close ClassWizard.
</DL>

<P>The <TT>CHelloDlg::OnInitDialog</TT> function doesn't really need to initialize
any variables, so you can display a message box instead. Edit <TT>OnInitDialog</TT>
so that it looks like the function in Listing 4.1.
<H4><FONT COLOR="#000077">TYPE: Listing 4.1. The CHelloDlg::OnInitDialog function.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>BOOL CHelloDlg::OnInitDialog()</TT>
<TT>{</TT>
<TT>    CDialog::OnInitDialog();</TT>
<TT>    AfxMessageBox( &quot;WM_INITDIALOG received&quot; );</TT>
<TT>    return TRUE;</TT>
<TT>}</TT>
</FONT></PRE>
<H3><FONT COLOR="#000077"><B>Adding a Menu Choice for the New Dialog Box</B></FONT></H3>
<P>To add a menu item to the menu used by HelloSDI follow the steps in this section.
Don't worry too much about what's going on here; you'll learn more about menus in
Hour 10.</P>
<P>Menus are stored in your project as resources. To display the current menu resources,
select the ResourceView tab in the project workspace window. Expand the resource
tree to show the different resource types defined for the current project; one of
the folders is labeled Menu.</P>
<P>Open the Menu folder to display the single menu named <TT>IDR_MAINFRAME</TT>.
Open the menu resource by double-clicking the menu resource icon. The menu is displayed
in the resource editor ready for editing. Clicking any top-level menu item displays
the pop-up menu associated with that item, as shown in Figure 4.4.</P>
<P><A NAME="04"></A><A HREF="04.htm" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/ch04/04.htm"><B>Figure 4.4.</B></A> <BR>
<I>Using the Developer Studio resource editor to edit a menu resource.</I></P>
<P>The last item of every menu is an empty box. This box is used to add new menu
items to the menu resource. All menu items are initially added to the end of a menu
resource and then moved to their proper position. To add a new menu item, follow
these steps:

<DL>
	<DD>1. Double-click the empty box on the File menu to display the Menu Properties
	dialog box.<BR>
	<BR>
	2. To add a menu item, provide a menu ID and caption for the new menu item. For this
	example, enter <TT>ID_FILE_HELLO</TT> as the menu ID and <TT>&amp;Hello</TT> as the
	menu caption.<BR>
	<BR>
	3. Click anywhere outside the properties dialog box to return to the editor.
</DL>

<P>After adding a menu item, the next step is to add a message-handling function
to handle the new menu item. To add a message-handling function for the <TT>ID_FILE_HELLO</TT>
menu item, follow these steps:

<DL>
	<DD>1. Open ClassWizard by pressing Ctrl+W or by right-clicking in a source code
	window and selecting ClassWizard from the menu.<BR>
	<BR>
	2. Select the tab labeled Message Maps and select from the Class Name combo box the
	class that will handle the message--in this case, <TT>CMainFrame</TT>.<BR>
	<BR>
	3. Select the object that is generating the message from the Object ID list box--in
	this case, <TT>ID_FILE_HELLO</TT>. Two message-handling functions are displayed in
	the Messages list box.<BR>
	<BR>
	4. Select the <TT>COMMAND</TT> message from the Messages list box and click the Add
	Function button. Accept the default name suggested by ClassWizard for the function
	name: <TT>OnFileHello</TT>.<BR>
	<BR>
	5. Click OK to close ClassWizard.
</DL>

<P>Edit the <TT>CMainFrame::OnFileHello</TT> function so that it looks like the function
provided in Listing 4.2.
<H4><FONT COLOR="#000077">TYPE: Listing 4.2. The message-handling function for the
Hello menu item.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>void CMainFrame::OnFileHello()</TT>
<TT>{</TT>
<TT>    CHelloDlg   dlgHello;</TT>

<TT>    if( dlgHello.DoModal() == IDOK )</TT>
<TT>        AfxMessageBox( &quot;OK button pressed&quot; );</TT>
<TT>    else // IDCANCEL</TT>
<TT>        AfxMessageBox( &quot;Cancel button pressed&quot; );</TT>
<TT>}</TT>
</FONT></PRE>
<P>Add an <TT>#include</TT> statement in <TT>MainFrm.cpp</TT> that includes the class
definition for <TT>CHelloDlg</TT>, found in <TT>HelloDlg.h</TT>, by adding the following
line just above the include statement for <TT>MainFrm.h</TT>:</P>
<PRE><FONT COLOR="#0066FF"><TT>#include &quot;HelloDlg.h&quot;</TT>
</FONT></PRE>
<P>Compile and run the HelloSDI project. When the <TT>DoModal</TT> member function
is called, the <TT>IDD_HELLO</TT> dialog box is displayed. The function call does
not return until you close the dialog box by pressing one of the dialog box's buttons.
If you press OK, the return value is <TT>IDOK</TT>. If you press Cancel, the return
value is <TT>IDCANCEL</TT>.
<H2><FONT COLOR="#000077"><B>Creating Dialog Box-Based Projects</B></FONT></H2>
<P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>A <I>dialog box-based project</I>
uses a dialog box as the main window of a simple program. For example, many of the
utilities found in the Windows 95 Control Panel are dialog box-based.</P>
<P>A dialog box-based program has a menu that is accessed through the system menu
at the upper-left corner of the dialog box's caption bar. A dialog box-based project
is often used to build very small programs that interact with the user through a
single dialog box. The program can be much smaller and easier to program because
the number of classes created by AppWizard is reduced by about half.


<BLOCKQUOTE>
	<P>
<HR>
<FONT COLOR="#000077"><B>Time Saver:</B></FONT><B> </B>If your program must have
	sophisticated menus, it should not be dialog box based. 
<HR>


</BLOCKQUOTE>

<P>A user can easily operate a dialog box-based program. There is only one dialog
box window, no menu, and all the available controls usually are initially visible.
There are no hidden dialog boxes or menu items, and the user can usually see exactly
which operations should be carried out.
<H3><FONT COLOR="#000077"><B>AppWizard Support for Dialog Box-Based Projects</B></FONT></H3>
<P>You can create a dialog box-based program using AppWizard, just like the SDI program
you built earlier in this hour. Building a dialog box-based project is one of the
initial options offered by AppWizard.</P>
<P>Because a dialog box-based project is much simpler than an SDI or MDI project,
fewer steps are required when using AppWizard. Only four wizard pages are presented
by AppWizard when building a dialog box-based project, versus the six pages required
for an SDI or MDI project.</P>
<P>To create a dialog box-based project using AppWizard, follow these steps:

<DL>
	<DD>1. Open MFC AppWizard by creating a new project workspace, as you have in previous
	hours. For the purpose of building an example for this hour, use the name HelloDialog.<BR>
	<BR>
	2. When the opening screen for AppWizard appears, select a dialog box-based project
	as the project type.<BR>
	<BR>
	3. Accept the default project settings suggested by AppWizard and press the Finish
	button. You can also browse through the Wizard pages and change the default settings.
	AppWizard creates the dialog box-based project for you, just as it did earlier in
	the hour for the HelloSDI project.
</DL>

<H3><FONT COLOR="#000077"><B>Exploring the HelloDialog AppWizard Project</B></FONT></H3>
<P>After you create the HelloDialog project, take some time to explore the project
workspace. Much of the project workspace looks just as it does for an SDI project.
There are four tabs for the different project workspace views, and several files
and resources have been created for you.</P>
<P>There are several differences between a dialog-based project and an SDI or MDI
project:

<UL>
	<LI>No menu resource is created for the project. Because the project uses a dialog
	box as its main window, there's no need for a menu in most cases.<BR>
	<BR>
	
	<LI>There are no document or view classes. Dialog-based projects are intended to
	be very simple applications that don't require Document/View support.<BR>
	<BR>
	
	<LI>There are two dialog box resources. The main window for the project is a dialog
	box, as is the About box. The names of the two dialog box resources for the HelloDialog
	project are <TT>IDD_ABOUTBOX</TT> and <TT>IDD_HELLODIALOG_DIALOG</TT>.
</UL>

<H3><FONT COLOR="#000077"><B>Using the Dialog Box Editor</B></FONT></H3>
<P>Open the dialog box editor by double-clicking the <TT>IDD_HELLODIALOG_DIALOG</TT>
icon. The <TT>IDD_HELLODIALOG_DIALOG</TT> dialog box is displayed in the dialog box
editor, along with a dockable control toolbar or palette. The dialog box will already
contain a static text control. Modify the static text control so that it reads Hello
Dialog Project, as shown in Figure 4.5.</P>
<P><A NAME="05"></A><A HREF="05.htm" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/ch04/05.htm"><B>Figure 4.5.</B></A> <I><BR>
The main dialog box for the HelloDialog project.</I></P>
<P>Build and run the HelloDialog project. Because it is much smaller, the HelloDialog
project will compile and launch faster than an SDI or MDI project. For that reason
many of the examples in this book that deal with controls will use dialog box-based
projects.
<H2><FONT COLOR="#000077"><B>Summary</B></FONT></H2>
<P>This hour began with an introduction to object-oriented design. In this hour,
you also learned about dialog boxes and how they are used in programs written for
Windows. This hour also covered the support provided by Developer Studio, including
ClassWizard, the MFC class library, and the dialog box editor.
<H2><FONT COLOR="#000077"><B>Q&amp;A</B></FONT></H2>

<DL>
	<DD><B>Q When I display a modal dialog box, no other part of the user interface can
	be used; does my application also stop functioning while the dialog box is displayed?</B><BR>
	<BR>
	<B>A</B> A modal dialog box prevents the user from accessing other parts of your
	application; it does not prevent Windows from sending events to your message-handling
	procedures. Your application will continue to work normally while displaying a modal
	dialog box.<BR>
	<BR>
	<B>Q Why are C++ classes always split into two files? Wouldn't it be easier to have
	only a single file that defines the class as is done with Java?</B><BR>
	<BR>
	<B>A</B> A key part of most languages that support object-oriented programming is
	the idea that the description of a class should be kept separate from its implementation.
	This fits in with the notion of information hiding, where unnecessary details are
	hidden whenever possible. In a well-designed C++ class, the implementation is considered
	a detail that the consumer doesn't need to be concerned with.<BR>
	<BR>
	In Java, the class is always defined inside the class declaration, and they are never
	separated. This simplifies the work required for the compiler and runtime system.
	However, it also forces you to deal with implementation details when reading the
	class declaration.<BR>
	<BR>
	If you prefer to define a class inside the class declaration, C++ supports that coding
	style; just include the function body after its declaration:
</DL>



<BLOCKQUOTE>
	<PRE><FONT COLOR="#0066FF"><TT>class CFoo</TT>
<TT>{</TT>
<TT>    int m_nBar;</TT>
<TT>public:</TT>
<TT>    CFoo(){</TT>
<TT>        m_nBar = 0;</TT>
<TT>    }</TT>
<TT>    void SetBar(int newVal){</TT>
<TT>        m_nBar = newVal;</TT>
<TT>    }</TT>
<TT>    int GetBar() const{</TT>
<TT>        return m_nBar;</TT>
<TT>    }</TT>
<TT>};</TT></FONT></PRE>

</BLOCKQUOTE>

<PRE><FONT COLOR="#0066FF"><TT></TT></FONT></PRE>
<H2><FONT COLOR="#000077"><B>Workshop</B></FONT></H2>
<P>The Workshop is designed to help you anticipate possible questions, review what
you've learned, and begin thinking ahead to putting your knowledge into practice.
The answers to the quiz are in Appendix B, &quot;Quiz Answers.&quot;
<H3><FONT COLOR="#000077"><B>Quiz</B></FONT></H3>

<DL>
	<DD>1. What is the difference between a modal and modeless dialog box?<BR>
	<BR>
	2. What message is sent to a dialog box for initialization purposes?<BR>
	<BR>
	3. What is the file extension used for C++ class declaration files?<BR>
	<BR>
	4. What is the file extension used for C++ class implementation files?<BR>
	<BR>
	5. What message box style is provided by default when using <TT>AfxMessageBox</TT>?<BR>
	<BR>
	6. What message box style should be used when reporting an error to a user?<BR>
	<BR>
	7. What MFC class is used to manage dialog boxes?<BR>
	<BR>
	8. What member function is called to pop up a modal dialog box?<BR>
	<BR>
	9. If the user presses the Yes button in a message box, what return value is provided
	to <TT>AfxMessageBox</TT>?<BR>
	<BR>
	10. If the user presses the No button in a message box, what return value is provided
	to <TT>AfxMessageBox</TT>?
</DL>

<H3><FONT COLOR="#000077"><B>Exercises</B></FONT></H3>

<DL>
	<DD>1. Change the HelloSDI example so that the message box displayed for <TT>WM_INITIDIALOG</TT>
	uses the information icon.<BR>
	<BR>
	2. Add a second static text label to the HelloDialog project's main dialog box that
	displays your name.
</DL>

<CENTER>
<P>
<HR>
<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> <BR>
<BR>
<BR>
<IMG SRC="corp.gif" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/button/corp.gif" WIDTH="284" HEIGHT="45" ALIGN="BOTTOM" ALT="Macmillan Computer Publishing USA"
BORDER="0"></P>

<P>&#169; <A HREF="copy.htm" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/copy.htm">Copyright</A>, Macmillan Computer Publishing. All
rights reserved.
</CENTER>


</BODY>

</HTML>

⌨️ 快捷键说明

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