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

📄 ch04.htm

📁 Using Visual C++ 6.0 一本关于Visual C++ 6.0基本编程应用的书籍。
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<!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 4 -- Documents and Views</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="../ch03/ch03.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch05/ch05.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>- 4 -</H1>

<H1>Documents and Views</H1>
</CENTER>

<UL>
	<LI><A HREF="#Heading1">Understanding the Document Class</A>
	<LI><A HREF="#Heading2">Understanding the View Class</A>
	<LI><A HREF="#Heading3">Creating the Rectangles Application</A>
	<LI><A HREF="#Heading4">Other View Classes</A>
	<LI><A HREF="#Heading5">Document Templates, Views, and Frame Windows</A>
</UL>

<P>
<HR SIZE="4">

<CENTER>
<H1></H1>
</CENTER>
<H2><A NAME="Heading1"></A>Understanding the Document Class</H2>
<P>When you generate your source code with AppWizard, you get an application featuring
all the bells and whistles of a commercial 32-bit Windows 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 need to modify the code that AppWizard generates. This task can be easy or complex,
depending on how you want your application to look and act.</P>
<P>Probably the most important set of modifications are those related to the <I>document</I>--the
information the user can save from your application and restore later--and to the
<I>view</I>--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 onscreen 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>
<P>SDI and MDI applications created with AppWizard are document/view applications.
That means that AppWizard generates a class for you derived from CDocument, and delegates
certain tasks to this new document class. It also creates a view class derived from
CView and delegates other tasks to your new view class. Let's look through an AppWizard
starter application and see what you get.</P>
<P>Choose File, New, 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 table, and 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 App1 classes in ClassView,
and you see that six classes have been created: CAboutDlg, CApp1App, CApp1Doc, CApp1View,
CChildFrame, and CMainframe.</P>
<P>CApp1Doc represents a document; it holds the application's document data. You
add storage for the document by adding data members to the CApp1Doc class. To see
how this works, look at Listing 4.1, which shows the header file AppWizard creates
for the CApp1Doc class.</P>
<P>
<H4>Listing 4.1&#160;&#160;APP1DOC.H--The Header File for the CApp1Doc Class</H4>
<PRE>// App1Doc.h : interface of the CApp1Doc class
//
///////////////////////////////////////////////////////////////////////////
#if !defined(AFX_APP1DOC_H__43BB481D_64AE_11D0_9AF3_0080C81A397C__INCLUDED_)
#define AFX_APP1DOC_H__43BB481D_64AE_11D0_9AF3_0080C81A397C__INCLUDED_
#if _MSC_VER &gt; 1000
#pragma once
#endif // _MSC_VER &gt; 1000
class CApp1Doc : public CDocument
{
protected: // create from serialization only
     CApp1Doc();
     DECLARE_DYNCREATE(CApp1Doc)
// Attributes
public:
// Operations
public:
// Overrides
     // ClassWizard generated virtual function overrides
     //{{AFX_VIRTUAL(CApp1Doc)
     public:
     virtual BOOL OnNewDocument();
     virtual void Serialize(CArchive&amp; ar);
     //}}AFX_VIRTUAL
// Implementation
public:
     virtual ~CApp1Doc();
#ifdef _DEBUG
     virtual void AssertValid() const;
     virtual void Dump(CDumpContext&amp; dc) const;
#endif
protected:
// Generated message map functions
protected:
     //{{AFX_MSG(CApp1Doc)
        // NOTE - the ClassWizard will add and remove member functions here.
        //    DO NOT EDIT what you see in these blocks of generated code !
     //}}AFX_MSG
     DECLARE_MESSAGE_MAP()
};
///////////////////////////////////////////////////////////////////////////
</PRE>

<PRE>//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations
// immediately before the previous line.
#endif // !defined(AFX_APP1DOC_H__43BB481D_64AE_11D0_9AF3
</PRE>

<PRE>[ccc]    _0080C81A397C__INCLUDED_)
</PRE>
<P>Near the top of the listing, you can see the class declaration's Attributes section,
which is followed by the public 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 CPoint objects as the application's
data. That array is declared as a member of the document class like this:</P>
<P>
<PRE>// Attributes
public:
    CPoint points[100];
</PRE>
<P>CPoint is an MFC class that encapsulates the information relevant to a point on
the screen, most importantly the x and y coordinates of the point.</P>
<P>Notice also in the class's header file that the CApp1Doc class includes two virtual
member functions called OnNewDocument() and Serialize(). MFC calls the OnNewDocument()
function whenever the user selects the File, New 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, which has only a single document open at any time, the open document
is closed and a new blank document is loaded into the same object; in an MDI application,
which can have multiple documents open, a blank document is opened in addition to
the documents that are already open. The Serialize() member function is where the
document class loads and saves its data. This is discussed in Chapter 7, &quot;Persistence
and File I/O.&quot;</P>
<P>
<H2><A NAME="Heading2"></A>Understanding the View Class</H2>
<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 4.2 is the header file for Capp1View, as
generated by AppWizard.</P>


<BLOCKQUOTE>
	<P>
<HR>
<strong>TIP:</strong> 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. The reasoning behind these design principles
	is explored in Appendix A, &quot; C++ Review and Object-Oriented Concepts.&quot;
<HR>


</BLOCKQUOTE>

<H4>Listing 4.2&#160;&#160;APP1VIEW.H--The Header File for the CApp1View Class</H4>
<PRE>// App1View.h : interface of the CApp1View class
//
///////////////////////////////////////////////////////////////////////////
#if !defined(AFX_APP1VIEW_H__43BB481F_64AE_11D0_9AF3
[ccc]_0080C81A397C__INCLUDED_)
#define AFX_APP1VIEW_H__43BB481F_64AE_11D0_9AF3_0080C81A397C__INCLUDED_
#if _MSC_VER &gt; 1000
#pragma once
#endif // _MSC_VER &gt; 1000
class CApp1View : public CView
{
protected: // create from serialization only
     CApp1View();
     DECLARE_DYNCREATE(CApp1View)
// Attributes
public:
     CApp1Doc* GetDocument();
// Operations
public:
// Overrides
     // ClassWizard generated virtual function overrides
     //{{AFX_VIRTUAL(CApp1View)
     public:
     virtual void OnDraw(CDC* pDC);  // overridden to draw this view
virtual BOOL PreCreateWindow(CREATESTRUCT&amp; cs);
     protected:
     virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
     virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
     virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
     //}}AFX_VIRTUAL
// Implementation
public:
     virtual ~CApp1View();
#ifdef _DEBUG
     virtual void AssertValid() const;
     virtual void Dump(CDumpContext&amp; dc) const;
#endif
protected:
// Generated message map functions
protected:
     //{{AFX_MSG(CApp1View)
        // NOTE - the ClassWizard will add and remove member functions here.
        //    DO NOT EDIT what you see in these blocks of generated code !
     //}}AFX_MSG
     DECLARE_MESSAGE_MAP()

⌨️ 快捷键说明

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