文档类.h文件
// SaveDoc.h : interface of the CSaveDoc class
//
/
#if !defined(AFX_SAVEDOC_H__F46718E5_7FE3_4F02_B33E_41A71EAFA510__INCLUDED_)
#define AFX_SAVEDOC_H__F46718E5_7FE3_4F02_B33E_41A71EAFA510__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CSaveDoc : public CDocument
{
protected: // create from serialization only
CSaveDoc();
DECLARE_DYNCREATE(CSaveDoc)
// Attributes
public:
int nCount;
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CSaveDoc)
public:
virtual BOOL OnNewDocument();
virtual void Serialize(CArchive& ar);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CSaveDoc();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:
//{{AFX_MSG(CSaveDoc)
// 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_SAVEDOC_H__F46718E5_7FE3_4F02_B33E_41A71EAFA510__INCLUDED_)
文档类.cpp文件
// SaveDoc.cpp : implementation of the CSaveDoc class
//
#include "stdafx.h"
#include "Save.h"
#include "SaveDoc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// CSaveDoc
IMPLEMENT_DYNCREATE(CSaveDoc, CDocument)
BEGIN_MESSAGE_MAP(CSaveDoc, CDocument)
//{{AFX_MSG_MAP(CSaveDoc)
// 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()
/
// CSaveDoc construction/destruction
CSaveDoc::CSaveDoc():nCount(0)
{
// TODO: add one-time construction code here
}
CSaveDoc::~CSaveDoc()
{
}
BOOL CSaveDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
nCount=0;//此函数可以清零,在窗口中点击文件然后新建,nCount清零
return TRUE;
}
/
// CSaveDoc serialization
void CSaveDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
ar<<nCount; //存盘,输出流写入盘内
}
else
{
// TODO: add loading code here
ar>>nCount;//读盘,输入流从盘里读出
}
}
/
// CSaveDoc diagnostics
#ifdef _DEBUG
void CSaveDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CSaveDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
在视图类中声明一个中间变量str;
// SaveView.h : interface of the CSaveView class // / #if !defined(AFX_SAVEVIEW_H__864AADDD_DF38_46E7_A7DD_6B9C80F904A2__INCLUDED_) #define AFX_SAVEVIEW_H__864AADDD_DF38_46E7_A7DD_6B9C80F904A2__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 class CSaveView : public CView { protected: // create from serialization only CSaveView(); DECLARE_DYNCREATE(CSaveView) // Attributes public: CSaveDoc* GetDocument(); // Operations public: CString str; // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CSaveView) public: virtual void OnDraw(CDC* pDC); // overridden to draw this view virtual BOOL PreCreateWindow(CREATESTRUCT& 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 ~CSaveView(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif protected: // Generated message map functions protected: //{{AFX_MSG(CSaveView) afx_msg void OnLButtonDown(UINT nFlags, CPoint point); afx_msg void OnRButtonDown(UINT nFlags, CPoint point); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; #ifndef _DEBUG // debug version in SaveView.cpp inline CSaveDoc* CSaveView::GetDocument() { return (CSaveDoc*)m_pDocument; } #endif / //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_SAVEVIEW_H__864AADDD_DF38_46E7_A7DD_6B9C80F904A2__INCLUDED_)
/// CSaveDoc comman视图类.cpp文件
// SaveView.cpp : implementation of the CSaveView class // #include "stdafx.h" #include "Save.h" #include "SaveDoc.h" #include "SaveView.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif / // CSaveView IMPLEMENT_DYNCREATE(CSaveView, CView) BEGIN_MESSAGE_MAP(CSaveView, CView) //{{AFX_MSG_MAP(CSaveView) 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() / // CSaveView construction/destruction CSaveView::CSaveView() { // TODO: add construction code here } CSaveView::~CSaveView() { } BOOL CSaveView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CView::PreCreateWindow(cs); } / // CSaveView drawing void CSaveView::OnDraw(CDC* pDC) { CSaveDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here CRect rect; this->GetWindowRect(&rect); int x,y; x=rect.Width()/2; y=rect.Height()/2; str.Format("%d",pDoc->nCount); pDC->TextOut(x,y,str); } / // CSaveView printing BOOL CSaveView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CSaveView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CSaveView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } / // CSaveView diagnostics #ifdef _DEBUG void CSaveView::AssertValid() const { CView::AssertValid(); } void CSaveView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CSaveDoc* CSaveView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSaveDoc))); return (CSaveDoc*)m_pDocument; } #endif //_DEBUG / // CSaveView message handlers void CSaveView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CSaveDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); pDoc->nCount+=1; Invalidate(); pDoc->SetModifiedFlag(); //提示关闭时是否保存 CView::OnLButtonDown(nFlags, point); } void CSaveView::OnRButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CSaveDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); pDoc->nCount-=1; Invalidate(); CView::OnRButtonDown(nFlags, point); }