窗口重绘,用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 对像的数据