C++ MFC MDI change child window variables on creat

2019-08-24 02:47发布

问题:

I have an MDI application where a dialog is called when the OnFileNew() function (processed by the theApp object) is called. This dialog allows the user to set values to some variables that then need to be passed on to the CChildFrame object that is created when the ->CreateNewChild() function is called.

How do I pass these variables onto the CChildFrame object that is created by the ->CreateNewChild() function?

EDIT: In response to an answer I got, here are the results for using ->Create() vs ->CreateNewChild().

Link: CMainFrame *pFrame; - pFrame->CreateNewChild()

Link: CChildFrame *childFrame; - childFrame->Create()

How do I get the tabbed windows shown in the first link with the function declarations described in the second link?

回答1:

You can pass the data via a customized document template. Derive a class from CMultiDocTemplate to add additional data members, then add a pointer to your derived document template class to your CWinApp-derived app class. Initialize your document template in the usual way, except when you finish, save the new document template object to the pointer in your app class.

Now in your CreateNewChild function, instead of calling CWinApp::OnFileNew, you can just get the data from the current frame, then assign to the data member in the document template saved in the app class, before calling OpenDocumentFile(NULL). You can clear the data members when OpenDocumentFile returns.

The document template will in turn create the child frame and pass the doc template in the create context. To get the create context in the child frame, you can either override CChildFrame::OnCreateClient, or read the create structure in OnCreate:

MDICREATESTRUCT * pMDICreateStruct=(MDICREATESTRUCT * )lpCreateStruct->lpCreateParams;
CCreateContext *pCreateContext=(CCreateContext *)pMDICreateStruct->lParam;

Instead of passing the initialization data in the document template, you could also pass data to the new document. You will basically copy the code from CMultiDocTemplate::OpenDocumentFile and add the code to get the initialization data from the active document of the main frame.