Home > Software engineering >  Transfer: MFC a document changes of two views
Transfer: MFC a document changes of two views

Time:01-21

1 - first create want to switch the two view class (the view class) such as:
The class CTestView1: public CFormView {} class CTestView2: public CView {}
2 - then at xx. CPP file (xx) on behalf of the project namely CxxApp class of CPP files contained in the switching of the two classes of header files, such as:
# include "TestView1. H"
# include "TestView2. H"
3 - in the header file of CxxApp application two CMultiDocTemplate Pointers such as:
CMultiDocTemplate * m_pTemplateTestView1;
CMultiDocTemplate * m_pTemplateTestView2;
4 - in CxxApp InitInstance () function, create the above two Pointers point to the instance
BOOL CXXApp: : InitInstance ()
{...
M_pTemplateTestView1=new CMultiDocTemplate (
IDR_MAINFRAME,
RUNTIME_CLASS (CTestViewDoc),
RUNTIME_CLASS (CChildFrame),//custom MDI child frame RUNTIME_CLASS (CTestView1)); AddDocTemplate (m_pTemplateTestView1);
M_pTemplateTestView2=new CMultiDocTemplate (
IDR_MAINFRAME,
RUNTIME_CLASS (CTestViewDoc),
RUNTIME_CLASS (CChildFrame),//custom MDI child frame RUNTIME_CLASS (CTestView2));
AddDocTemplate (m_pTemplateTestView2);//add to this app template management class
. }
Note CMultiDocTemplate () in the first parameter points to a string resource ID, if there are multiple strings, start in the pop-up dialog lets you choose use which template, then one of the ID can be change as does not point to a string resource ID, shielding the dialog box, can also be overloaded in the app OnFileNew () function, choose inside need first to create a template, such as:
Void CXXApp: : OnFileNew ()
{CDocTemplate * pTemplate=NULL;
int i;
PTemplate=m_pTemplateTestView1;
ASSERT (pTemplate!=NULL);
ASSERT_KINDOF (CDocTemplate pTemplate);
Return
PTemplate - & gt; OpenDocumentFile (NULL);//create the doc, create frame, to create the view}
Note CMultiDocTemplate () in the second parameter will be CChildFrame
Note CMultiDocTemplate () in the third argument for you create a need to switch the view view class
5 - program already contains multiple views of the template, switch is implemented in the Mainframe class, the Mainframe to add the following function
Void CMainFrame: : SwitchToView (CDocTemplate * pTemplate, CRuntimeClass * pViewClass)
{
CMDIChildWnd * pMDIActive=MDIGetActive ();//get activities child window
CDocument * pDoc=pMDIActive - & gt; GetActiveDocument ();//doc for activity
The CView * pView;
The POSITION pos=pDoc - & gt; GetFirstViewPosition ();//query all created under this doc view
While (pos!=NULL)
{
PView=pDoc - & gt; GetNextView (pos);
If (pView - & gt; IsKindOf (pViewClass))//if have created the view are set to show it, pView - & gt; GetParentFrame () - & gt; ActivateFrame ();//
PView - & gt; UpdateWindow ();//view update the display;
return;
}
PView - & gt; UpdateWindow ();
}
CMDIChildWnd * pNewFrame=(CMDIChildWnd *) (pTemplate - & gt; CreateNewFrame (pDoc, NULL));
//if not create, create this view childframe and view
If (pNewFrame==NULL) return; PTemplate - & gt; InitialUpdateFrame (pNewFrame, pDoc);
}
6 - response you switch button in the mainframe, call switchtoView such as
Void CMainFrame: : OnViewTestView1 ()//view 1 display, with a view like this method
{SwitchToView (theApp m_pVideoTemplate, RUNTIME_CLASS (CTestView1));
//the first pointer for app in your template, the second for ctestview1 class type recognition macros,} : RUNTIME_CLASS as a macro, it returns a pointer to the CRuntimeClass *, for the type of class identification, IsKindof (),

CodePudding user response:

Fly in the ointment is that before the view switch can not close all views, otherwise an error,
  • Related