Re: Creating MDI Child Windows by Guido
Guido
Tue Jul 24 07:51:59 CDT 2007
Ok.
In your header file of the CWinApp derived class insert a new member:
MyWinApp.h:
class CMyWinApp : public CWinApp
{
// ....
public:
CMultiDocTemplate* m_pDocTemplateMyChildWnd;
};
in the source file
MyWinApp.cpp:
in the constructor:
CMyWinApp::CMyWinApp()
{
// ...
m_pDocTemplateMyChildWnd = NULL; // always initialize pointers!!
}
in OnInitInstance of your CMyWinApp class you must change the local pointer
to the member pointer:
before:
BOOL CMyWinApp::InitInstance()
{
// ...
CMultiDocTemplate* pDocT = new CMultiDocTemplate(IDR_CHILDWND_TYPE,
RUNTIME_CLASS(CMyChildDoc),
RUNTIME_CLASS(CMyChildFrame),
RUNTIME_CLASS(CMyChildView));
if (!pDocT) return FALSE;
AddDocTemplate(pDocT);
// ...
}
change to:
BOOL CMyWinApp::InitInstance()
{
// ...
m_pDocTemplateMyChildWnd = new CMultiDocTemplate(IDR_CHILDWND_TYPE,
RUNTIME_CLASS(CMyChildDoc),
RUNTIME_CLASS(CMyChildFrame),
RUNTIME_CLASS(CMyChildView));
if (!m_pDocTemplateMyChildWnd) return FALSE;
AddDocTemplate(m_pDocTemplateMyChildWnd);
// ...
}
Now you have the document template pointer which you can use to open a new
child window, e.g. the following way:
void CMyWinApp::OpenNewWindow()
{
m_pDocTemplateMyChildWnd ->OpenDocumentFile(NULL);
}
You could call this routine from a menu item in CMainFrame or using a
toolbar button for example. Whenever you call OpenNewWindow, a new mdi
window will be opened.
HTH Guido