Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Fast Track Visual C++ 6.0 Programming
(Publisher: John Wiley & Sons, Inc.)
Author(s): Steve Holzner
ISBN: 0471312908
Publication Date: 09/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


We use the view object to display that text; specifically, we use TextOut() in the OnDraw() function.

 -----------------------         -----------------------
| Document Object       |       | View Object          |
|                       |       |                      |
|                       |-------|                      |
|         text          |       |     TextOut()        |
|                       |       |                      |
 -----------------------         -----------------------

The document is for storing data, as we’ve done in KeysSDI.

Storing Data in the Document

We set up storage for the text CString in the document’s header file, KeysSDIDoc.h.

class CKeysSDIDoc : public CDocument
{
protected: // create from serialization only
    CKeysSDIDoc();
    DECLARE_DYNCREATE(CKeysSDIDoc)

// Attributes
public:
    CString text;                                                   ⇐
    .
    .
    .

In addition to storing the data in the document, we initialize it in the document’s constructor, CKeysSDIDoc(), which appears in KeysSDIDoc.cpp, this way:

CKeysSDIDoc::CKeysSDIDoc()
{
    text = “”;                                                    ⇐
}

The preceding example shows the standard way of doing things in documents: You set up space for the data you want to store and then initialize that data as required in the document’s constructor.

In addition to storing data in memory, the document also provides a mechanism for storing it on disk, and that method is serialization.

Serializing Data

One of the document’s member functions is the Serialize() function.

void CKeysSDIDoc::Serialize(CArchive& ar)
{
    if (ar.IsStoring())
    {
     // TODO: add storing code here
    }
    else
    {
     // TODO: add loading code here
    }
}

As we’ll see in more detail later, this function is used to store data to disk and read it back again. In this case, our data is a simple CString object, so we can store it to the archive object passed to us and read it back from disk this way:

void CKeysSDIDoc::Serialize(CArchive& ar)
{
    if (ar.IsStoring())
    {
        text >> ar;                                                 ⇐
    }
    else
    {
        ar >> text;                                                 ⇐
    }
}

We’ll investigate this process further in Chapter 5. In the meantime, we have finished our overview of the document for the moment. The listing for KeysSDIDoc.h and KeysSDIDoc.cpp appears in Listing 1.4.


Listing 1.4 KeysSDIDoc.h and KeysSDIDoc.cpp

// KeysSDIDoc.h : interface of the CKeysSDIDoc class
//
/////////////////////////////////////////////////////////////////////////////

#if
!defined(AFX_KEYSSDIDOC_H__701B1ADB_9BB7_11D1_887F_D42B07C10710__INCLUDED_)
#define AFX_KEYSSDIDOC_H__701B1ADB_9BB7_11D1_887F_D42B07C10710__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


class CKeysSDIDoc : public CDocument
{
protected: // create from serialization only
    CKeysSDIDoc();
    DECLARE_DYNCREATE(CKeysSDIDoc)

// Attributes
public:
    CString text;
// Operations
public:

// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CKeysSDIDoc)
    public:
    virtual BOOL OnNewDocument();
    virtual void Serialize(CArchive& ar);
    //}}AFX_VIRTUAL

// Implementation
public:
    virtual ~CKeysSDIDoc();
#ifdef _DEBUG
    virtual void AssertValid() const;
    virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions
protected:

    //{{AFX_MSG(CKeysSDIDoc)
     // 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()
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately
before the previous line.

#endif //
!defined(AFX_KEYSSDIDOC_H__701B1ADB_9BB7_11D1_887F_D42B07C10710__INCLUDED_)


// KeysSDIDoc.cpp : implementation of the CKeysSDIDoc class
//

#include “stdafx.h”
#include “KeysSDI.h”

#include “KeysSDIDoc.h”
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CKeysSDIDoc

IMPLEMENT_DYNCREATE(CKeysSDIDoc, CDocument)

BEGIN_MESSAGE_MAP(CKeysSDIDoc, CDocument)
    //{{AFX_MSG_MAP(CKeysSDIDoc)
     // NOTE - the ClassWizard will add and remove mapping macros here.
        //    DO NOT EDIT what you see in these blocks of generated code!
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CKeysSDIDoc construction/destruction

CKeysSDIDoc::CKeysSDIDoc()
{
    text = “”;
}

CKeysSDIDoc::~CKeysSDIDoc()
{
}

BOOL CKeysSDIDoc::OnNewDocument()
{
    if (!CDocument::OnNewDocument())
     return FALSE;

    // TODO: add reinitialization code here
    // (SDI documents will reuse this document)

    return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
// CKeysSDIDoc serialization

void CKeysSDIDoc::Serialize(CArchive& ar)
{
    if (ar.IsStoring())
    {
     // TODO: add storing code here
    }
    else
    {
        // TODO: add loading code here
    }
}

/////////////////////////////////////////////////////////////////////////////
// CKeysSDIDoc diagnostics

#ifdef _DEBUG
void CKeysSDIDoc::AssertValid() const
{
    CDocument::AssertValid();
}

void CKeysSDIDoc::Dump(CDumpContext& dc) const
{
    CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CKeysSDIDoc commands

The document object is only half the data-handling story, of course. The other half is the view object, which we’ll take a look at now.

The View Object

Judging from its name, you’d think the view object has only one purpose: to display the program’s data to the user. In fact, you usually use the view object for almost all user interactions—not only displaying data, but also getting that data from the user in the form of user input. For example, we added a character-reading function, OnChar(), to the view:

void CKeysSDIView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    CKeysSDIDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);

    pDoc->text += nChar;

    CView::OnChar(nChar, nRepCnt, nFlags);

    Invalidate();
}


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.