📄 ch05.htm
字号:
<HTML>
<HEAD>
<TITLE>Special Edition Using Visual C++ 5 - Chapter 5</TITLE>
<LINK REL="Next" HREF="ch06.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/ch06.htm">
<LINK REL="Previous" HREF="ch04.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/ch04.htm"></HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H2><A ID="I1" NAME="I1"><B>Chapter 5</B></A></H2>
<H2><A ID="I2" NAME="I2"><A ID="I3" NAME="I3">Documents and Views</A></A></H2>
<hr>
<ul>
<li> <B>How document objects declare their data</B></P>
<P> Any information your application plans to save must be added to the document class.</P>
<li> <B>How view objects access the document's data</B></P>
<P> It is the view class that shows the data to the user: it asks the document for the data.</P>
<li> <B>Other view classes in MFC</B></P>
<P> MFC provides 10 different view classes from which your class can inherit.</P>
<li> <B>Document templates</B></P>
<P> Connections between views and documents are simple to arrange.</P>
</ul>
<P>When you generate your source code with AppWizard, you get an application featuring all the bells and whistles of a commercial Windows 95 application, including a toolbar, a status bar, ToolTips, menus, and even an About dialog box. However, in spite
of all those features, the application really doesn't do anything useful. In order to create an application that does more than look pretty on your desktop, you've got to modify the code that AppWizard generates. This task can be easy or complex, depending
upon how you want your application to look and act.</P>
<P>Probably the most important set of modifications are those related to the document—the information the user can save from your application and restore later—and to the view—the way that information is presented to the user. MFC's
document/view architecture separates an application's data from the way the user actually views and manipulates that data. Simply, the document object is responsible for storing, loading, and saving the data, whereas the view object (which is just another
type of window) enables the user to see the data on-screen and to edit that data in a way that is appropriate to the application. In this chapter, you learn the basics of how MFC's document/view architecture works.</P>
<H3><A ID="I4" NAME="I4"><A ID="I5" NAME="I5"><B>Understanding the Document Class</B></A></A></H3>
<P>SDI and MDI applications created with AppWizard are document/view applications. That means that AppWizard generates a class for you, derived from <font color="#008000">CDocument</font>, and delegates certain tasks to this new document class. It also
creates a view class, derived from <font color="#008000">CView</font>, and delegates other tasks to the your new view class. Let's look through an AppWizard starter application and see what you get.</P>
<P>Choose <U>F</U>ile, <U>N</U>ew, and select the Projects tab. Fill in the project name as <B>App1</B> and fill in an appropriate directory for the project files. Make sure that MFC AppWizard (exe) is selected. Click OK.</P>
<P>Move through the AppWizard dialog boxes, changing the settings to match those in the following list, then click Next to continue:</P>
<P>Step 1: Multiple documents</P>
<P>Step 2: Don't change the defaults presented by AppWizard</P>
<P>Step 3: Don't change the defaults presented by AppWizard</P>
<P>Step 4: Deselect all check boxes except Printing and Print Preview</P>
<P>Step 5: Don't change the defaults presented by AppWizard</P>
<P>Step 6: Don't change the defaults presented by AppWizard</P>
<P>After you click Finish on the last step, the New project information box summarizes your work. Click OK to create the project. Expand the <font color="#008000">App1</font> classes in ClassView, and you see that six classes have been created: <font
color="#008000">CAboutDlg</font>, <font color="#008000">CApp1App</font>, <font color="#008000">CApp1Doc</font>, <font color="#008000">CApp1View</font>, <font color="#008000">CChildFrame</font>, and <font color="#008000">CMainframe</font>.</P>
<p><font color="#008000">CApp1Doc</font> represents a document; it holds the application's document data. You add storage for the document by adding data members to the <font color="#008000">CApp1Doc</font> class. To see how this works, look at Listing
5.1, which shows the header file AppWizard creates for the <font color="#008000">CApp1Doc</font> class.</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><I>Listing 5.1—</I>APP1DOC.H<I>—The Header </I><I>File for the </I>CApp1Doc<I> Class</I></P>
<pre><font color="#008000">// App1Doc.h : interface of the CApp1Doc class</font></pre>
<pre><font color="#008000">//</font></pre>
<pre><font color="#008000">/////////////////////////////////////////////////////////////////////////////</font></pre>
<pre><font color="#008000">#if !defined(AFX_APP1DOC_H__43BB481D_64AE_11D0_9AF3_0080C81A397C__INCLUDED_)</font></pre>
<pre><font color="#008000">#define AFX_APP1DOC_H__43BB481D_64AE_11D0_9AF3_0080C81A397C__INCLUDED_</font></pre>
<pre><font color="#008000">class CApp1Doc : public CDocument</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000">protected: // create from serialization only</font></pre>
<pre><font color="#008000"> CApp1Doc();</font></pre>
<pre><font color="#008000"> DECLARE_DYNCREATE(CApp1Doc)</font></pre>
<pre><font color="#008000">// Attributes</font></pre>
<pre><font color="#008000">public:</font></pre>
<pre><font color="#008000">// Operations</font></pre>
<pre><font color="#008000">public:</font></pre>
<pre><font color="#008000">// Overrides</font></pre>
<pre><font color="#008000"> // ClassWizard generated virtual function overrides</font></pre>
<pre><font color="#008000"> //{{AFX_VIRTUAL(CApp1Doc)</font></pre>
<pre><font color="#008000"> public:</font></pre>
<pre><font color="#008000"> virtual BOOL OnNewDocument();</font></pre>
<pre><font color="#008000"> virtual void Serialize(CArchive& ar);</font></pre>
<pre><font color="#008000"> //}}AFX_VIRTUAL</font></pre>
<pre><font color="#008000">// Implementation</font></pre>
<pre><font color="#008000">public:</font></pre>
<pre><font color="#008000"> virtual ~CApp1Doc();</font></pre>
<pre><font color="#008000">#ifdef _DEBUG</font></pre>
<pre><font color="#008000"> virtual void AssertValid() const;</font></pre>
<pre><font color="#008000"> virtual void Dump(CDumpContext& dc) const;</font></pre>
<pre><font color="#008000">#endif</font></pre>
<pre><font color="#008000">protected:</font></pre>
<pre><font color="#008000">// Generated message map functions</font></pre>
<pre><font color="#008000">protected:</font></pre>
<pre><font color="#008000"> //{{AFX_MSG(CApp1Doc)</font></pre>
<pre><font color="#008000"> // NOTE - the ClassWizard will add and remove member functions here.</font></pre>
<pre><font color="#008000"> // DO NOT EDIT what you see in these blocks of generated code !</font></pre>
<pre><font color="#008000"> //}}AFX_MSG</font></pre>
<pre><font color="#008000"> DECLARE_MESSAGE_MAP()</font></pre>
<pre><font color="#008000">};</font></pre>
<pre><font color="#008000">/////////////////////////////////////////////////////////////////////////////</font></pre>
<pre><font color="#008000">//{{AFX_INSERT_LOCATION}}</font></pre>
<pre><font color="#008000">// Microsoft Developer Studio will insert additional declarations immediately before the </font><font color="#008000">previous line.</font></pre>
<pre><font color="#008000">#endif // !defined(AFX_APP1DOC_H__43BB481D_64AE_11D0_9AF3_0080C81A397C__INCLUDED_)</font></pre>
<P>Near the top of the listing, you can see the class declaration's Attributes section, which is followed by the <font color="#008000">public</font> keyword. This is where you declare the data members that will hold your application's data. In the program
that you create a little later in this chapter, the application must store an array of <font color="#008000">CPoint</font> objects as the application's data. That array is declared as a member of the document class like this:</P>
<pre><font color="#008000">// Attributes</font></pre>
<pre><font color="#008000">public:</font></pre>
<pre><font color="#008000"> CPoint points[100];</font></pre>
<P>CPoint is an MFC class that encapsulates the information relevant to a point on the screen, most importantly the x and y co-ordinates of the point.</P>
<P>Notice also in the class's header file that the <font color="#008000">CApp1Doc</font> class includes two virtual member functions called <font color="#008000">OnNewDocument()</font> and <font color="#008000">Serialize()</font>. MFC calls the <font
color="#008000">OnNewDocument()</font> function whenever the user selects the <U>F</U>ile, <U>N</U>ew command (or its toolbar equivalent, if a New button has been implemented in the application). You can use this function to perform whatever initialization
must be performed on your document's data. In an SDI application, the open document is closed and a new blank document is loaded into the same object; in an MDI application a blank document is opened in addition to documents that are already open. The
<font color="#008000">Serialize()</font> member function is where the document class loads and saves its data. This is discussed in <A HREF="index08.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index08.htm" target="text">Chapter 8</A>, "Persistence and File I/O."</P>
<H3><A ID="I6" NAME="I6"><B> </B><A ID="I7" NAME="I7"><B>Understanding the View Class</B></A></A></H3>
<P>As mentioned previously, the view class displays the data stored in the document object and enables the user to modify this data. The view object keeps a pointer to the document object, which it uses to access the document's member variables in order
to display or modify them. Listing 5.2 is the header file for <font color="#008000">Capp1View</font>, as generated by AppWizard.</P>
<blockquote><p><img src="tip.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/tip.gif">
<P>Most MFC programmers add public member variables to their documents to make it easy for the view class to access them. A more object-oriented approach is to add private or protected member variables, and then add public functions to get or change the
values of these variables. </P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<p><img src="cd_rom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/cd_rom.gif" hspace=10>
<P><I>Listing 5.2—</I>APP1VIEW.H<I>—The </I><I>Header File for the </I>CApp1View<I> Class</I></P>
<pre><font color="#008000">// App1View.h : interface of the CApp1View class</font></pre>
<pre><font color="#008000">//</font></pre>
<pre><font color="#008000">/////////////////////////////////////////////////////////////////////////////</font></pre>
<pre><font color="#008000">#if !defined(AFX_APP1VIEW_H__43BB481F_64AE_11D0_9AF3_0080C81A397C__INCLUDED_)</font></pre>
<pre><font color="#008000">#define AFX_APP1VIEW_H__43BB481F_64AE_11D0_9AF3_0080C81A397C__INCLUDED_</font></pre>
<pre><font color="#008000">class CApp1View : public CView</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000">protected: // create from serialization only</font></pre>
<pre><font color="#008000"> CApp1View();</font></pre>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -