东软总结02-文档类的相关操作

在文档类中找到序列函数SerializeCArchive &ar

void CHomework_jiangjieDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
		ar << nCount;//写入数据
	}
	else
	{
		// TODO: add loading code here
		ar >> nCount;//读取数据
	}
}


 

新建 文件时触发事件:找到OnNewDocument()

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

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)
	nCount = 0;

	return TRUE;
}


 

关闭程序时,提醒用户是否保存:view中要保存的动作之后添加:

	pDoc->SetModifiedFlag();

整个view类代码:

// homework_jiangjieView.cpp : implementation of the CHomework_jiangjieView class
//

#include "stdafx.h"
#include "homework_jiangjie.h"

#include "homework_jiangjieDoc.h"
#include "homework_jiangjieView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CHomework_jiangjieView

IMPLEMENT_DYNCREATE(CHomework_jiangjieView, CView)

BEGIN_MESSAGE_MAP(CHomework_jiangjieView, CView)
	//{{AFX_MSG_MAP(CHomework_jiangjieView)
	ON_WM_LBUTTONDOWN()
	ON_WM_RBUTTONDOWN()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHomework_jiangjieView construction/destruction

CHomework_jiangjieView::CHomework_jiangjieView()
{
	// TODO: add construction code here

}

CHomework_jiangjieView::~CHomework_jiangjieView()
{
}

BOOL CHomework_jiangjieView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CHomework_jiangjieView drawing

void CHomework_jiangjieView::OnDraw(CDC* pDC)
{
	CHomework_jiangjieDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here

	CString strTemp;
	strTemp.Format("计数%d", pDoc->nCount);
	pDC->TextOut(10, 10, strTemp);
}

/////////////////////////////////////////////////////////////////////////////
// CHomework_jiangjieView printing

BOOL CHomework_jiangjieView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CHomework_jiangjieView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CHomework_jiangjieView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CHomework_jiangjieView diagnostics

#ifdef _DEBUG
void CHomework_jiangjieView::AssertValid() const
{
	CView::AssertValid();
}

void CHomework_jiangjieView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CHomework_jiangjieDoc* CHomework_jiangjieView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CHomework_jiangjieDoc)));
	return (CHomework_jiangjieDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CHomework_jiangjieView message handlers

void CHomework_jiangjieView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CHomework_jiangjieDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	
	++pDoc->nCount;
	Invalidate();

	pDoc->SetModifiedFlag();

	CView::OnLButtonDown(nFlags, point);
}

void CHomework_jiangjieView::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CHomework_jiangjieDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	
	--pDoc->nCount;
	Invalidate();

	pDoc->SetModifiedFlag();

	CView::OnRButtonDown(nFlags, point);
}


doc类:

// homework_jiangjieDoc.cpp : implementation of the CHomework_jiangjieDoc class
//

#include "stdafx.h"
#include "homework_jiangjie.h"

#include "homework_jiangjieDoc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CHomework_jiangjieDoc

IMPLEMENT_DYNCREATE(CHomework_jiangjieDoc, CDocument)

BEGIN_MESSAGE_MAP(CHomework_jiangjieDoc, CDocument)
	//{{AFX_MSG_MAP(CHomework_jiangjieDoc)
		// 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()

/////////////////////////////////////////////////////////////////////////////
// CHomework_jiangjieDoc construction/destruction

CHomework_jiangjieDoc::CHomework_jiangjieDoc():nCount(0)
{
	// TODO: add one-time construction code here

}

CHomework_jiangjieDoc::~CHomework_jiangjieDoc()
{
}

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

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)
	nCount = 0;

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CHomework_jiangjieDoc serialization

void CHomework_jiangjieDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
		ar << nCount;//写入数据
	}
	else
	{
		// TODO: add loading code here
		ar >> nCount;//读取数据
	}
}

/////////////////////////////////////////////////////////////////////////////
// CHomework_jiangjieDoc diagnostics

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

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

/////////////////////////////////////////////////////////////////////////////
// CHomework_jiangjieDoc commands


doc.h:

// homework_jiangjieDoc.h : interface of the CHomework_jiangjieDoc class
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_HOMEWORK_JIANGJIEDOC_H__04F202FD_45D8_4B07_9C20_A15C605B0064__INCLUDED_)
#define AFX_HOMEWORK_JIANGJIEDOC_H__04F202FD_45D8_4B07_9C20_A15C605B0064__INCLUDED_

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


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

// Attributes
public:

// Operations
public:
	int nCount;

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

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

protected:

// Generated message map functions
protected:
	//{{AFX_MSG(CHomework_jiangjieDoc)
		// 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_HOMEWORK_JIANGJIEDOC_H__04F202FD_45D8_4B07_9C20_A15C605B0064__INCLUDED_)


其他文件不用变化。

你可能感兴趣的:(东软总结02-文档类的相关操作)