VC++ (DocView.cpp):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | #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』。