12/e1998/04MFC1/eWindowsMFCMFC2/e1998/051998UNALIS3/e3/e2/e2/e3/e3/eMFC2/eMFC3/eMFCMFC2/eVC5+MFC42VC6+MFC421MFC2/eMFC2/ePDF://expert.csdn.net/jjhouGBKmirror-anywaysolutionMFC1/eMFC2/ejjhou@ccca.nctu.edu.tw3dissectingMFC2/epart1.pdfchap1~chap33,384,209dissectingMFC2/epart2.pdfchap42,448,990dissectingMFC2/epart3.pdfchap5~chap72,158,594dissectingMFC2/epart4.pdfchap8~chap165,171,266dissectingMFC2/epart5.pdfappendixA,B,C,D1,527,111--theend4534MFC深入淺出MFC2ndEdition第㆕篇深入MFC程式設計454455Document-ViewAppWziardScribblestep0DocumentViewDocument/ViewDocumentTemplateMFCApplicationFrameworkMFCDocument/ViewDocument/ViewMFCMFCOLEcompounddocumentDocument/ViewDocumentViewDocument/View第㆕篇深入MFC程式設計4561.2.3.4.5.6.Document/ViewXeroxPARCSmalltalkModel-View-ControllerMVCModelMFCDocumentControllerMFCDocumentTemplateApplicationFramework100100ASCIIRegistry¡KUIModel-View-ControllerMVCMFCDocument/View第8章Document-View深入探討457DocumentclassCScribbleDoc:publicCDocument{DECLARE_DYNCREATE(CScribbleDoc)...virtualvoidSerialize(CArchive&ar);DECLARE_MESSAGE_MAP()};voidCScribbleDoc::Serialize(CArchive&ar){if(ar.IsStoring()){//TODO:addstoringcodehere}else{//TODO:addloadingcodehere}}--DocumentDocumentdatasetdatasourceDocumentMFCCDocumentCDocumentCDocumentDocumentSerializeAppWizardScribblestep0CDocumentCObjectCObjectRTTIDynamicCreationSerializationCCmdTargetWM_COMMAND第㆕篇深入MFC程式設計458ViewclassCScribbleView:publicCView{DECLARE_DYNCREATE(CScribbleView)...virtualvoidOnDraw(CDC*pDC);DECLARE_MESSAGE_MAP()};voidCScribbleView::OnDraw(CDC*pDC){CScribbleDoc*pDoc=GetDocument();ASSERT_VALID(pDoc);//TODO:adddrawcodefornativedatahere}ViewDocumentViewMFCCViewCViewCViewViewOnDrawOnPrintAppWizardScribblestep0CViewCWndWindowsWM_SIZEWM_PAINTCCmdTargetWM_COMMANDC/SDKWM_PAINTBeginPaintDeviceContextDCDCDCMFCWM_PAINTFrameworkOnDrawViewFrame第8章Document-View深入探討459DocumentFrameViewFrameTEXTBITMAPTEXTTEXTBITMAPBITMAPFrameUIViewFrameUISDIMDIOLEin-placeeditingViewDocumentTemplateMFCDocument/View/FrameDocumentViewFrameDocumentTemplateMFCCDocTemplateCMultiDocTemplateCSingleDocTemplateDocumentTemplateAddDocTemplateMDISDIMDISDKMDICMultiDocTemplateSDICSingleDocTemplateCDocTemplateDocument/View/Frame第㆕篇深入MFC程式設計460CDocTemplate管理CDocument/CView/CFrameWndBOOLCScribbleApp::InitInstance(){...CMultiDocTemplate*pDocTemplate;pDocTemplate=newCMultiDocTemplate(IDR_SCRIBTYPE,RUNTIME_CLASS(CScribbleDoc),RUNTIME_CLASS(CChildFrame),RUNTIME_CLASS(CScribbleView));AddDocTemplate(pDocTemplate);...}BEGIN_MESSAGE_MAP(CScribbleApp,CWinApp)ON_COMMAND(ID_APP_ABOUT,OnAppAbout)ON_COMMAND(ID_FILE_NEW,CWinApp::OnFileNew)ON_COMMAND(ID_FILE_OPEN,CWinApp::OnFileOpen)ON_COMMAND(ID_FILE_PRINT_SETUP,CWinApp::OnFilePrintSetup)END_MESSAGE_MAP()DocumentTemplateDocumentTemplateCWinAppInitInstanceFile/NewFile/OpenViewCWinAppDocumentDocumentTemplate第8章Document-View深入探討461【File/New】【File/Open】CMyViewCMyDocCChildFrameCWinAppDocumentTemplateDocumentFrameFrameViewViewFile/OpenViewView“View”“View”ViewWindowsMFCViewC++CViewconstructViewcreateViewFrameFrameDynamicCreation8-1Document/View/Frame8-1DocumentTemplateDocument/View/FrameMFCCMultiDocTemplate::OpenDocumentFileSerializeSDICSingleDocTemplate::OpenDocumentFileScribbleCMultiDocTemplateCSingleDocTemplate第㆕篇深入MFC程式設計462classCSingleDocTemplate:publicCDocTemplate{...protected://standardimplementationCDocument*m_pOnlyDoc;};CMultiDocTemplateclassCMultiDocTemplate:publicCDocTemplate{...protected://standardimplementationCPtrListm_docList;};//inAFXWIN.HclassCDocTemplate:publicCCmdTarget{...UINTm_nIDResource;//IDR_forframe/menu/accelaswellCRuntimeClass*m_pDocClass;//classforcreatingnewdocumentsCRuntimeClass*m_pFrameClass;//classforcreatingnewframesCRuntimeClass*m_pViewClass;//classforcreatingnewviewsCStringm_strDocStrings;//'\n'separatednames...}//inDOCMULTI.CPPCDocument*CMultiDocTemplate::OpenDocumentFile(LPCTSTRlpszPathName,BOOLbMakeVisible){CDocument*pDocument=CreateNewDocument();...CFrameWnd*pFrame=CreateNewFrame(pDocument,NULL);...if(lpszPathName==NULL){//createanewdocument-withdefaultdocumentnameFile/NewAppWizardMessageMapCWinApp::OnFileNewCDocManager::OnFileNewCWinApp::OpenDocumentFileCDocManager::OpenDocumentFileCMultiDocTemplate::OpenDocumentFileMFC第8章Document-View深入探討463...}else{//openanexistingdocument...}InitialUpdateFrame(pFrame,pDocument,bMakeVisible);returnpDocument;}//inDOCTEMPL.CPPCDocument*CDocTemplate::CreateNewDocument(){...CDocument*pDocument=(CDocument*)m_pDocClass-CreateObject();...AddDocument(pDocument);returnpDocument;}CFrameWnd*CDocTemplate::CreateNewFrame(CDocument*pDoc,CFrameWnd*pOther){//createaframewiredtothespecifieddocumentCCreateContextcontext;context.m_pCurrentFrame=pOther;context.m_pCurrentDoc=pDoc;context.m_pNewViewClass=m_pViewClass;context.m_pNewDocTemplate=this;...CFrameWnd*pFrame=(CFrameWnd*)m_pFrameClass-CreateObject();...//createnewfromresourcepFrame-LoadFrame(m_nIDResource,WS_OVERLAPPEDWINDOW|FWS_ADDTOTITLE,//defaultframestylesNULL,&context)...returnpFrame;}CreateNewDocumentDocumentCreateNewFrameDocumentFrameCRuntimeCla