文档、视图架构的应用

窗口重绘,用Document/view机制保存显示数据

#include <afxwin.h>
#include "resource.h"
#include <afxtempl.h>

class MyDocument:public CDocument
{
public:
 CArray<CPoint,CPoint &>  pArray;
 void AddPoint(CPoint p)
 {
  pArray.Add(p);
 }
 CPoint GetPoint(int i)
 {
  return pArray[i];
 }
 int GetSize()
 {
  return pArray.GetSize();
 }
  


 DECLARE_DYNCREATE(MyDocument)
 DECLARE_MESSAGE_MAP()
};

IMPLEMENT_DYNCREATE(MyDocument,CDocument)
BEGIN_MESSAGE_MAP(MyDocument,CDocument)
 END_MESSAGE_MAP()

 


class MyView:public CView
{
public:

 void OnDraw(CDC* aDC)
 {
 MyDocument *doc=(MyDocument *)GetDocument();
 int num=doc->GetSize();
 int i;
 for(int i=0;i<num;i++)
 {
  CPoint point =doc->GetPoint(i);
  aDC->SetPixel(point,RGB(255,0,0));
 }
 }
 afx_msg void OnLButtonDown(UINT,CPoint point)
 {
  SetCapture();
 }
 afx_msg void OnMouseMove(UINT,CPoint point)
 {
  if(this==GetCapture());
  {
   CClientDC aDC(this);
   aDC.SetPixel(point,RGB(255,0,0));
   MyDocument *doc=(MyDocument *)GetDocument();
   doc->AddPoint (point);
  }
 }

 afx_msg void OnLButtonUp(UINT,CPoint point)
 {
  ReleaseCapture();
 }


 DECLARE_DYNCREATE(MyView)
 DECLARE_MESSAGE_MAP()
};

 

 
 
 
 

IMPLEMENT_DYNCREATE(MyView,CView)
BEGIN_MESSAGE_MAP(MyView,CView)
 ON_WM_LBUTTONDOWN()
 ON_WM_MOUSEMOVE()
 ON_WM_LBUTTONUP()
 END_MESSAGE_MAP()

 


class MyFrame:public CFrameWnd
{
public:
 DECLARE_DYNCREATE(MyFrame);
 DECLARE_MESSAGE_MAP()
};


IMPLEMENT_DYNCREATE(MyFrame,CFrameWnd)
BEGIN_MESSAGE_MAP(MyFrame,CFrameWnd)
 END_MESSAGE_MAP()

 


class MyApp: public CWinApp
{
public:
 BOOL InitInstance()
 {
  CDocument *doc;
  CSingleDocTemplate *DocTemplate;
  DocTemplate=new CSingleDocTemplate(IDR_MENU1,RUNTIME_CLASS(MyDocument),RUNTIME_CLASS(MyFrame),RUNTIME_CLASS(MyView));
  AddDocTemplate(DocTemplate);
  doc=DocTemplate->CreateNewDocument ();
  m_pMainWnd=DocTemplate->CreateNewFrame (doc,NULL);
  DocTemplate->InitialUpdateFrame ((CFrameWnd *)m_pMainWnd,doc);
  m_pMainWnd->ShowWindow (SW_SHOW);
  return true;
 }
};

MyApp a_app;

 文档视图架构的运作机制

1document 负责数据的保存,View对象负责显示document 对像的数据

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值