MFC里的静态拆分窗口是一种很有意思的效果,但它们毕竟不是独立的窗口,还框在一个大窗口中, 那么要使用真正完全独立的窗口怎么做呢?本文参考侯捷的《深入浅出MFC》的第13章的思路,实现了多个独立的视图窗口,代码如下:
class
CCY457App :
public
CWinApp
{
public :
CCY457App();
CMultiDocTemplate * m_pTemplate_ch3;
CMultiDocTemplate * m_pTemplate_ch4;
// 重写
public :
virtual BOOL InitInstance();
virtual int ExitInstance();
// 实现
afx_msg void OnAppAbout();
DECLARE_MESSAGE_MAP()
};
BOOL CCY457App::InitInstance()
{
……
CMultiDocTemplate * pDocTemplate;
pDocTemplate = new CMultiDocTemplate(IDR_CY457TYPE,
RUNTIME_CLASS(CCY457Doc),
RUNTIME_CLASS(CChildFrame), // 自定义MDI 子框架
RUNTIME_CLASS(CCY457View));
if ( ! pDocTemplate)
return FALSE;
AddDocTemplate(pDocTemplate);
m_pTemplate_ch3 = new CMultiDocTemplate(IDR_CY457TYPE,
RUNTIME_CLASS(CCY457Doc),
RUNTIME_CLASS(CChildFrame), // 自定义MDI 子框架2
RUNTIME_CLASS(CCY457View_ch3));
m_pTemplate_ch4 = new CMultiDocTemplate(IDR_CY457TYPE,
RUNTIME_CLASS(CCY457Doc),
RUNTIME_CLASS(CChildFrame), // 自定义MDI 子框架3
RUNTIME_CLASS(CCY457View_ch4));
……
return TRUE;
}
int CCY457App::ExitInstance()
{
delete m_pTemplate_ch3;
delete m_pTemplate_ch4;
return CWinApp::ExitInstance();
}
{
public :
CCY457App();
CMultiDocTemplate * m_pTemplate_ch3;
CMultiDocTemplate * m_pTemplate_ch4;
// 重写
public :
virtual BOOL InitInstance();
virtual int ExitInstance();
// 实现
afx_msg void OnAppAbout();
DECLARE_MESSAGE_MAP()
};
BOOL CCY457App::InitInstance()
{
……
CMultiDocTemplate * pDocTemplate;
pDocTemplate = new CMultiDocTemplate(IDR_CY457TYPE,
RUNTIME_CLASS(CCY457Doc),
RUNTIME_CLASS(CChildFrame), // 自定义MDI 子框架
RUNTIME_CLASS(CCY457View));
if ( ! pDocTemplate)
return FALSE;
AddDocTemplate(pDocTemplate);
m_pTemplate_ch3 = new CMultiDocTemplate(IDR_CY457TYPE,
RUNTIME_CLASS(CCY457Doc),
RUNTIME_CLASS(CChildFrame), // 自定义MDI 子框架2
RUNTIME_CLASS(CCY457View_ch3));
m_pTemplate_ch4 = new CMultiDocTemplate(IDR_CY457TYPE,
RUNTIME_CLASS(CCY457Doc),
RUNTIME_CLASS(CChildFrame), // 自定义MDI 子框架3
RUNTIME_CLASS(CCY457View_ch4));
……
return TRUE;
}
int CCY457App::ExitInstance()
{
delete m_pTemplate_ch3;
delete m_pTemplate_ch4;
return CWinApp::ExitInstance();
}
void CMainFrame::OnOpenglCh3()
{ // 打开第一个视图
CMDIChildWnd * pActiveChild = MDIGetActive();
CDocument * pDocument;
if (pActiveChild == NULL || (pDocument = pActiveChild -> GetActiveDocument()) == NULL)
{
TRACE0( " warning: No active document\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: failure to create new frame\n " );
AfxMessageBox(AFX_IDP_COMMAND_FAILURE);
return ;
}
pTemplate -> InitialUpdateFrame(pFrame,pDocument);}
void CMainFrame::OnOpenglCh4()
{ // 打开第二个视图
CMDIChildWnd * pActiveChild = MDIGetActive();
CDocument * pDocument;
if (pActiveChild == NULL || (pDocument = pActiveChild -> GetActiveDocument()) == NULL)
{
TRACE0( " warning: No active document\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: failure to create new frame\n " );
AfxMessageBox(AFX_IDP_COMMAND_FAILURE);
return ;
}
pTemplate -> InitialUpdateFrame(pFrame,pDocument);
}