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

来源:互联网 发布:javascript是jsp 编辑:程序博客网 时间:2024/06/16 19:13

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

void CHomework_jiangjieDoc::Serialize(CArchive& ar){if (ar.IsStoring()){// TODO: add storing code herear << nCount;//写入数据}else{// TODO: add loading code herear >> 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_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CHomework_jiangjieViewIMPLEMENT_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 commandsON_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/destructionCHomework_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 csreturn CView::PreCreateWindow(cs);}/////////////////////////////////////////////////////////////////////////////// CHomework_jiangjieView drawingvoid CHomework_jiangjieView::OnDraw(CDC* pDC){CHomework_jiangjieDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);// TODO: add draw code for native data hereCString strTemp;strTemp.Format("计数%d", pDoc->nCount);pDC->TextOut(10, 10, strTemp);}/////////////////////////////////////////////////////////////////////////////// CHomework_jiangjieView printingBOOL CHomework_jiangjieView::OnPreparePrinting(CPrintInfo* pInfo){// default preparationreturn 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 _DEBUGvoid 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 handlersvoid CHomework_jiangjieView::OnLButtonDown(UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call defaultCHomework_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 defaultCHomework_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_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CHomework_jiangjieDocIMPLEMENT_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_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CHomework_jiangjieDoc construction/destructionCHomework_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 serializationvoid CHomework_jiangjieDoc::Serialize(CArchive& ar){if (ar.IsStoring()){// TODO: add storing code herear << nCount;//写入数据}else{// TODO: add loading code herear >> nCount;//读取数据}}/////////////////////////////////////////////////////////////////////////////// CHomework_jiangjieDoc diagnostics#ifdef _DEBUGvoid 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 > 1000class CHomework_jiangjieDoc : public CDocument{protected: // create from serialization onlyCHomework_jiangjieDoc();DECLARE_DYNCREATE(CHomework_jiangjieDoc)// Attributespublic:// Operationspublic:int nCount;// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CHomework_jiangjieDoc)public:virtual BOOL OnNewDocument();virtual void Serialize(CArchive& ar);//}}AFX_VIRTUAL// Implementationpublic:virtual ~CHomework_jiangjieDoc();#ifdef _DEBUGvirtual void AssertValid() const;virtual void Dump(CDumpContext& dc) const;#endifprotected:// Generated message map functionsprotected://{{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_MSGDECLARE_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_)


其他文件不用变化。

原创粉丝点击