MFC里的静态拆分窗口是一种很有意思的效果,但它们毕竟不是独立的窗口,还框在一个大窗口中, 那么要使用真正完全独立的窗口怎么做呢?本文参考侯捷的《深入浅出MFC》的第13章的思路,实现了多个独立的视图窗口,代码如下:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->classCCY457App:publicCWinApp
{
public:
CCY457App();
CMultiDocTemplate*m_pTemplate_ch3;
CMultiDocTemplate*m_pTemplate_ch4;
//重写
public:
virtualBOOLInitInstance();
virtualintExitInstance();
//实现
afx_msgvoidOnAppAbout();
DECLARE_MESSAGE_MAP()
};
BOOLCCY457App::InitInstance()
{
……
CMultiDocTemplate*pDocTemplate;
pDocTemplate=newCMultiDocTemplate(IDR_CY457TYPE,
RUNTIME_CLASS(CCY457Doc),
RUNTIME_CLASS(CChildFrame),//自定义MDI子框架
RUNTIME_CLASS(CCY457View));
if(!pDocTemplate)
returnFALSE;
AddDocTemplate(pDocTemplate);
m_pTemplate_ch3=newCMultiDocTemplate(IDR_CY457TYPE,
RUNTIME_CLASS(CCY457Doc),
RUNTIME_CLASS(CChildFrame),//自定义MDI子框架2
RUNTIME_CLASS(CCY457View_ch3));
m_pTemplate_ch4=newCMultiDocTemplate(IDR_CY457TYPE,
RUNTIME_CLASS(CCY457Doc),
RUNTIME_CLASS(CChildFrame),//自定义MDI子框架3
RUNTIME_CLASS(CCY457View_ch4));
……
returnTRUE;
}
intCCY457App::ExitInstance()
{
deletem_pTemplate_ch3;
deletem_pTemplate_ch4;
returnCWinApp::ExitInstance();
}
{
public:
CCY457App();
CMultiDocTemplate*m_pTemplate_ch3;
CMultiDocTemplate*m_pTemplate_ch4;
//重写
public:
virtualBOOLInitInstance();
virtualintExitInstance();
//实现
afx_msgvoidOnAppAbout();
DECLARE_MESSAGE_MAP()
};
BOOLCCY457App::InitInstance()
{
……
CMultiDocTemplate*pDocTemplate;
pDocTemplate=newCMultiDocTemplate(IDR_CY457TYPE,
RUNTIME_CLASS(CCY457Doc),
RUNTIME_CLASS(CChildFrame),//自定义MDI子框架
RUNTIME_CLASS(CCY457View));
if(!pDocTemplate)
returnFALSE;
AddDocTemplate(pDocTemplate);
m_pTemplate_ch3=newCMultiDocTemplate(IDR_CY457TYPE,
RUNTIME_CLASS(CCY457Doc),
RUNTIME_CLASS(CChildFrame),//自定义MDI子框架2
RUNTIME_CLASS(CCY457View_ch3));
m_pTemplate_ch4=newCMultiDocTemplate(IDR_CY457TYPE,
RUNTIME_CLASS(CCY457Doc),
RUNTIME_CLASS(CChildFrame),//自定义MDI子框架3
RUNTIME_CLASS(CCY457View_ch4));
……
returnTRUE;
}
intCCY457App::ExitInstance()
{
deletem_pTemplate_ch3;
deletem_pTemplate_ch4;
returnCWinApp::ExitInstance();
}
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
voidCMainFrame::OnOpenglCh3()
{//打开第一个视图
CMDIChildWnd*pActiveChild=MDIGetActive();
CDocument*pDocument;
if(pActiveChild==NULL||(pDocument=pActiveChild->GetActiveDocument())==NULL)
{
TRACE0("warning:Noactivedocument/n");
AfxMessageBox(AFX_IDP_COMMAND_FAILURE);
return;
}
CDocTemplate*pTemplate=((CCY457App*)AfxGetApp())->m_pTemplate_ch3;
ASSERT_VALID(pTemplate);
CFrameWnd*pFrame=pTemplate->CreateNewFrame(pDocument,pActiveChild);
if(pFrame==NULL)
{
TRACE0("warning:failuretocreatenewframe/n");
AfxMessageBox(AFX_IDP_COMMAND_FAILURE);
return;
}
pTemplate->InitialUpdateFrame(pFrame,pDocument);}
voidCMainFrame::OnOpenglCh4()
{//打开第二个视图
CMDIChildWnd*pActiveChild=MDIGetActive();
CDocument*pDocument;
if(pActiveChild==NULL||(pDocument=pActiveChild->GetActiveDocument())==NULL)
{
TRACE0("warning:Noactivedocument/n");
AfxMessageBox(AFX_IDP_COMMAND_FAILURE);
return;
}
CDocTemplate*pTemplate=((CCY457App*)AfxGetApp())->m_pTemplate_ch4;
ASSERT_VALID(pTemplate);
CFrameWnd*pFrame=pTemplate->CreateNewFrame(pDocument,pActiveChild);
if(pFrame==NULL)
{
TRACE0("warning:failuretocreatenewframe/n");
AfxMessageBox(AFX_IDP_COMMAND_FAILURE);
return;
}
pTemplate->InitialUpdateFrame(pFrame,pDocument);
}
voidCMainFrame::OnOpenglCh3()
{//打开第一个视图
CMDIChildWnd*pActiveChild=MDIGetActive();
CDocument*pDocument;
if(pActiveChild==NULL||(pDocument=pActiveChild->GetActiveDocument())==NULL)
{
TRACE0("warning:Noactivedocument/n");
AfxMessageBox(AFX_IDP_COMMAND_FAILURE);
return;
}
CDocTemplate*pTemplate=((CCY457App*)AfxGetApp())->m_pTemplate_ch3;
ASSERT_VALID(pTemplate);
CFrameWnd*pFrame=pTemplate->CreateNewFrame(pDocument,pActiveChild);
if(pFrame==NULL)
{
TRACE0("warning:failuretocreatenewframe/n");
AfxMessageBox(AFX_IDP_COMMAND_FAILURE);
return;
}
pTemplate->InitialUpdateFrame(pFrame,pDocument);}
voidCMainFrame::OnOpenglCh4()
{//打开第二个视图
CMDIChildWnd*pActiveChild=MDIGetActive();
CDocument*pDocument;
if(pActiveChild==NULL||(pDocument=pActiveChild->GetActiveDocument())==NULL)
{
TRACE0("warning:Noactivedocument/n");
AfxMessageBox(AFX_IDP_COMMAND_FAILURE);
return;
}
CDocTemplate*pTemplate=((CCY457App*)AfxGetApp())->m_pTemplate_ch4;
ASSERT_VALID(pTemplate);
CFrameWnd*pFrame=pTemplate->CreateNewFrame(pDocument,pActiveChild);
if(pFrame==NULL)
{
TRACE0("warning:failuretocreatenewframe/n");
AfxMessageBox(AFX_IDP_COMMAND_FAILURE);
return;
}
pTemplate->InitialUpdateFrame(pFrame,pDocument);
}