#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)//声明run-time类 DECLARE_MESSAGE_MAP() }; IMPLEMENT_DYNCREATE(MyDocument,CDocument)//声明MyDocument为run-time类 BEGIN_MESSAGE_MAP(MyDocument,CDocument) END_MESSAGE_MAP(); class MyView : public CView { public: void OnDraw(CDC * aDC)//必须重载的虚函数,重绘函数 { MyDocument *doc = (MyDocument *)GetDocument();//取得当前Document的指针 int num = doc->GetSize(); int i; for(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_MESSAGE_MAP() DECLARE_DYNCREATE(MyView)//声明为run-time类 }; BEGIN_MESSAGE_MAP(MyView,CView) ON_WM_LBUTTONDOWN() ON_WM_MOUSEMOVE() ON_WM_LBUTTONUP() END_MESSAGE_MAP() IMPLEMENT_DYNCREATE(MyView,CView)//声明MyView为run-time类 class MyFrame:public CFrameWnd { DECLARE_DYNCREATE(MyFrame)//声明run-time类 DECLARE_MESSAGE_MAP() }; BEGIN_MESSAGE_MAP(MyFrame,CFrameWnd) END_MESSAGE_MAP() IMPLEMENT_DYNCREATE(MyFrame,CFrameWnd)//声明MyFrame为run-time类 class MyApp:public CWinApp { public: BOOL InitInstance() { CDocument *doc;//指向文件的指针 CSingleDocTemplate * DocTemplate;//文件指向单文档模板对象的指针 DocTemplate = new CSingleDocTemplate( IDR_MENU1,//用于单文档框架之资源标志符 RUNTIME_CLASS(MyDocument),//单文档窗口的Document类 RUNTIME_CLASS(MyFrame),//单文档窗口的窗口框架类 RUNTIME_CLASS(MyView)//单文档窗口的View类 );//建立单文档模板对象 AddDocTemplate(DocTemplate);//将单文档模板对象设置给MyApp; doc = DocTemplate->CreateNewDocument();//建立一个新的文档 m_pMainWnd = DocTemplate->CreateNewFrame(doc,NULL);//建立一个窗口框架 DocTemplate->InitialUpdateFrame((CFrameWnd*)m_pMainWnd,doc);//初始化窗口框架并连接view m_pMainWnd->ShowWindow(SW_SHOW);//显示窗口 return true; } } a_app;//建立应用程序对象