Document/View
5. 從資料的角度來看, Document物件與View物件 其實是一體的, 只是將資料的處理與顯示的工
作分開
Document/View
Document/View 架構圖 架構圖
應用程式物件
Pointer
指向
文件樣版物件物件 Pointer
文件物件 Pointer 視窗框架物件
Pointer 1.產生
3.產生 2.產生 瀏覽物件
Pointer 4.產生
Document/View
Document/View 程式範例 程式範例
#include <afxwin.h>
#include "Doc_View.h"
class MyDocument : public CDocument
{DECLARE_DYNCREATE(MyDocu ment) //宣告run-time類別};
IMPLEMENT_DYNCREATE(MyDoc ument, CDocument)
//宣告MyDocument為run-time類別 class MyView : public CView
{public:
void OnDraw(CDC * aDC)
//必須過載的虛擬函數 { }
DECLARE_DYNCREATE(MyView)/
/宣告run-time類別};
IMPLEMENT_DYNCREATE(MyVie w, CView)
//宣告MyView為run-time類 別
class MyFrame : public CFrameWnd {
DECLARE_DYNCREATE(MyFra me) //宣告run-time類別
};
IMPLEMENT_DYNCREATE(MyFra me, CFrameWnd)
//宣告MyFrame為run-time 類別
Document/View
Document/View 程式範例 程式範例
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(MyVie w)); //單文件視窗的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;//建立應用程 式物件
程式說明 程式說明
宣告Run-Time類別
因為CSignleDocument在無法得知視窗框架物件.Vie w 類別.Document類別的情況下,等到執行時才決定運用哪 幾個類別的應用程式.
宣告
DECLARE_DYNAMIC(類別名稱) 然後在該類別外做如下宣告
IMPLEMENT_DYNAMIC(類別名稱,衍生類別名稱)
Document/View
Document/View 架構的應用 架構的應用 視窗的重繪
視窗的重繪
#include <afxwin.h>
#include "repaint.h"
#include <afxtempl.h> //定義樣 版類別的標頭檔
class MyDocument : public CDocument
{
public:
CArray<CPoint, CPoint &> pArray;
//容納滑鼠軌跡點的Array 容器
void AddPoint(CPoint p) //將軌跡 點加到容器內
{ pArray.Add(p); }
CPoint GetPoint(int i) //將軌跡 點從容器中取出
{ return pArray[i]; }
int GetSize()
{ return pArray.GetSize(); } //取得容器的大小
DECLARE_DYNCREATE(MyDo cument) //宣告為run-time 類別
DECLARE_MESSAGE_MAP() //宣告訊息映射表
};
IMPLEMENT_DYNCREATE(MyDoc ument, CDocument)
//建立run-time類別
BEGIN_MESSAGE_MAP(MyDocum ent, CDocument)
END_MESSAGE_MAP() //建立訊息映射表
Document/View
Document/View 架構的應用 架構的應用 視窗的重繪
視窗的重繪
class MyView : public CView {public:
void OnDraw(CDC * aDC) //過載 OnDraw虛擬函數
{MyDocument *doc =
(MyDocument *)GetDocument();
//取得目前Document物件的指標 int num = doc->GetSize();
//取得目前儲存的軌跡點點數 int i;
for(i = 0; i < num; ++i) //將
Document中儲存的軌跡點重繪到 視窗上
{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();//
取得目前Document物件的指標 doc->AddPoint(point); //將軌跡 點加入Document物件中
}}
Document/View
Document/View 架構的應用 架構的應用 視窗的重繪
視窗的重繪
afx_msg void OnLButtonUp(UINT, CPoint point){ ReleaseCapture(); } //釋放滑鼠訊息的接收權
DECLARE_DYNCREATE(MyVie w) //宣告為run-time類別
DECLARE_MESSAGE_MAP() //宣告訊息映射表};
IMPLEMENT_DYNCREATE(MyVie w, CView)//建立run-time類別 BEGIN_MESSAGE_MAP(MyView,
CView)
ON_WM_LBUTTONDOWN() ON_WM_MOUSEMOVE() ON_WM_LBUTTONUP() END_MESSAGE_MAP()
//建立訊息映射表
class MyFrame : public CFrameWnd {
public:
DECLARE_DYNCREATE(MyFra me) //宣告為run-time類別
DECLARE_MESSAGE_MAP() //宣告訊息映射表
};
IMPLEMENT_DYNCREATE(MyFra me, CFrameWnd)
//建立run-time類別
BEGIN_MESSAGE_MAP(MyFrame, CFrameWnd)
END_MESSAGE_MAP() //建立訊息映射表
Document/View
Document/View 架構的應用 架構的應用 視窗的重繪
視窗的重繪
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 );
//起始化View物件
m_pMainWnd->ShowWindow ( SW_SHOW ); //顯示視窗 return true;
} } a_app;