VC++ (DocView.cpp):
#include <afxwin.h> #include "DocView.h" class DocBoxy : public CDocument //Document { //在此撰寫Document內容 DECLARE_DYNCREATE(DocBoxy) //宣告run-time類別 }; IMPLEMENT_DYNCREATE(DocBoxy, CDocument) //宣告DocBoxy為run-time類別 class FrameBoxy : public CFrameWnd //Frame { public: afx_msg void OnExit() //宣告關閉檔案事件 { if(MessageBox( TEXT("Are you sure you want to close this window?"), TEXT("DocViewBoxy"), MB_OKCANCEL | MB_ICONQUESTION ) == 1 ) { DestroyWindow(); } } //在此撰寫Frame內容 DECLARE_DYNCREATE(FrameBoxy) //宣告run-time類別 DECLARE_MESSAGE_MAP() //宣告訊息映射表 }; //宣告FrameBoxy為run-time類別 IMPLEMENT_DYNCREATE(FrameBoxy, CFrameWnd) //建立訊息映射表 BEGIN_MESSAGE_MAP(FrameBoxy, CFrameWnd) ON_COMMAND(ID_FILE_EXIT, OnExit) END_MESSAGE_MAP() class ViewBoxy : public CView //View { public: void OnDraw(CDC * aDC) //必須覆寫的虛擬函數 { for(int i=0;i<10;i++) { aDC->TextOut(20 * i, 20 * i, L"HelloBoxy!!"); } } //在此撰寫View內容 DECLARE_DYNCREATE(ViewBoxy) //宣告run-time類別 }; IMPLEMENT_DYNCREATE(ViewBoxy, CView) //宣告ViewBoxy為run-time類別 class AppBoxy : public CWinApp { public: BOOL InitInstance() //程式進入點 { CDocument *doc; CSingleDocTemplate* DocTemplate; DocTemplate = new CSingleDocTemplate( IDR_MENU, RUNTIME_CLASS(DocBoxy), //Document RUNTIME_CLASS(FrameBoxy), //Frame RUNTIME_CLASS(ViewBoxy) //View ); AddDocTemplate(DocTemplate); doc = DocTemplate->CreateNewDocument(); m_pMainWnd = DocTemplate->CreateNewFrame( doc, NULL ); DocTemplate->InitialUpdateFrame ( (CFrameWnd*)m_pMainWnd, doc ); m_pMainWnd->ShowWindow(SW_SHOW); return true; } }; AppBoxy appboxy; //建立應用程式物件範例結果:
說明:
在Win32模式下建立MFC之Document/View架構之範例程式碼, 須建立『DocView.rc』資源檔及自動產生『DocView.h』並加入Menu指定ID為『IDR_MENU』。
沒有留言:
張貼留言