void CFrameA::OnClose() { // TODO: Add your message handler code here and/or call default CView *pView; CDocument *pDoc; CViewSwitchApp *pApp=(CViewSwitchApp*)AfxGetApp(); //Close all the ViewA POSITION posDoc=pApp->m_pDocTemplateA->GetFirstDocPosition(); while(NULL!=posDoc) { pDoc=pApp->m_pDocTemplateA->GetNextDoc(posDoc); POSITION posView=pDoc->GetFirstViewPosition(); while(NULL!=posView) { pView=pDoc->GetNextView(posView); pView->GetParent()->PostMessage(WM_CLOSE); } } CMDIChildWnd::OnClose(); //Close all the ViewB posDoc=pApp->m_pDocTemplateB->GetFirstDocPosition(); while(NULL!=posDoc) { pDoc=pApp->m_pDocTemplateB->GetNextDoc(posDoc); POSITION posView=pDoc->GetFirstViewPosition(); while(NULL!=posView) { pView=pDoc->GetNextView(posView); pView->GetParent()->PostMessage(WM_CLOSE); } } } ---------------------- 给你一个例子 只要你获得了文档魔板的指针就好办了
下面是以前回答的一个帖子: ------------------------ 我按照你的要求作了一个demo工程! 工程ViewSwitch,为了便于操作,我在winapp中添加了几个文档魔板: class CViewSwitchApp : public CWinApp { public: CMultiDocTemplate *m_pDocTemplateMain; CMultiDocTemplate *m_pDocTemplateA; CMultiDocTemplate *m_pDocTemplateB; CMultiDocTemplate *m_pDocTemplateC; CViewSwitchApp();.... };BOOL CViewSwitchApp::InitInstance() { ..... m_pDocTemplateMain = new CMultiDocTemplate( IDR_VIEWSWTYPE, RUNTIME_CLASS(CViewSwitchDoc), RUNTIME_CLASS(CChildFrame), // custom MDI child frame RUNTIME_CLASS(CViewSwitchView)); AddDocTemplate(m_pDocTemplateMain); m_pDocTemplateA = new CMultiDocTemplate( IDR_VIEWSWTYPE, RUNTIME_CLASS(CViewSwitchDoc), RUNTIME_CLASS(CFrameA), // custom MDI child frame RUNTIME_CLASS(CViewA)); AddDocTemplate(m_pDocTemplateA); m_pDocTemplateB = new CMultiDocTemplate( IDR_VIEWSWTYPE, RUNTIME_CLASS(CViewSwitchDoc), RUNTIME_CLASS(CFrameB), // custom MDI child frame RUNTIME_CLASS(CViewB)); AddDocTemplate(m_pDocTemplateB); m_pDocTemplateC = new CMultiDocTemplate( IDR_VIEWSWTYPE, RUNTIME_CLASS(CViewSwitchDoc), RUNTIME_CLASS(CFrameC), // custom MDI child frame RUNTIME_CLASS(CViewC)); AddDocTemplate(m_pDocTemplateC); .... } 另外添加了几个类:CFrameA,CFrameB,CFrameC,CViewA,CViewB,CViewC 问题1:如何在关闭A视时同时关闭B视 void CFrameA::OnClose() { // TODO: Add your message handler code here and/or call default CView *pView; CDocument *pDoc; CViewSwitchApp *pApp=(CViewSwitchApp*)AfxGetApp(); //Close all the ViewA POSITION posDoc=pApp->m_pDocTemplateA->GetFirstDocPosition(); while(NULL!=posDoc) { pDoc=pApp->m_pDocTemplateA->GetNextDoc(posDoc); POSITION posView=pDoc->GetFirstViewPosition(); while(NULL!=posView) { pView=pDoc->GetNextView(posView); pView->GetParent()->PostMessage(WM_CLOSE); } } CMDIChildWnd::OnClose(); //Close all the ViewB posDoc=pApp->m_pDocTemplateB->GetFirstDocPosition(); while(NULL!=posDoc) { pDoc=pApp->m_pDocTemplateB->GetNextDoc(posDoc); POSITION posView=pDoc->GetFirstViewPosition(); while(NULL!=posView) { pView=pDoc->GetNextView(posView); pView->GetParent()->PostMessage(WM_CLOSE); } } } 问题2:(A,B视显示时)现又显示一个视C,但现示C时要使A视使去焦点(即鼠标点不到A视,但能点到B视),关闭C视时让A视重获焦点. 我的做法是使在ViewC显示的时候ViewA失效!这样ViewA 就不可能得到焦点了!关闭ViewC后,再使ViewA 激活,并使其成为当前窗口! //同时产生两个窗口 void CMainFrame::OnViewaViewb() { // TODO: Add your command handler code here CViewSwitchApp *pApp=(CViewSwitchApp*)AfxGetApp(); pApp->m_pDocTemplateA->OpenDocumentFile(NULL); pApp->m_pDocTemplateB->OpenDocumentFile(NULL); } //产生ViewC,并且使ViewA失去焦点 void CMainFrame::OnViewc() { // TODO: Add your command handler code here CViewSwitchApp *pApp=(CViewSwitchApp*)AfxGetApp(); pApp->m_pDocTemplateC->OpenDocumentFile(NULL); CView *pView; CDocument *pDoc; POSITION posDoc=pApp->m_pDocTemplateA->GetFirstDocPosition(); while(NULL!=posDoc) { pDoc=pApp->m_pDocTemplateA->GetNextDoc(posDoc); POSITION posView=pDoc->GetFirstViewPosition(); while(NULL!=posView) { pView=pDoc->GetNextView(posView); pView->GetParent()->EnableWindow(false); } } }//激活ViewA void CFrameC::OnClose() { // TODO: Add your message handler code here and/or call default CView *pView; CDocument *pDoc; CViewSwitchApp *pApp=(CViewSwitchApp*)AfxGetApp();
POSITION posDoc=pApp->m_pDocTemplateA->GetFirstDocPosition(); while(NULL!=posDoc) { pDoc=pApp->m_pDocTemplateA->GetNextDoc(posDoc); POSITION posView=pDoc->GetFirstViewPosition(); while(NULL!=posView) { pView=pDoc->GetNextView(posView); pView->GetParent()->EnableWindow(true); } } CMDIChildWnd::OnClose();
//activate one view of CViewA! pView->GetParent()->BringWindowToTop(); } 问题3: 用鼠标点击不同视时要弹出信息(说明在XX框架或视加具体的消息即可) 很简单呀! void CViewA::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default MessageBox("Hi my friend,I am from the viewA,Enjoy yourself!"); CView::OnLButtonDown(nFlags, point); } void CViewB::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default MessageBox("Hi my friend,I am from the viewB,Enjoy yourself!"); CView::OnLButtonDown(nFlags, point); } void CViewC::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default MessageBox("Hi my friend,I am from the viewC,Enjoy yourself!"); CView::OnLButtonDown(nFlags, point); }4、切换视图时要连同View对应的框架一同切换 切换视图的时候,当然会连同View对应的框架一同切换。View离不开自己的框架呀!
{
// TODO: Add your message handler code here and/or call default CView *pView;
CDocument *pDoc;
CViewSwitchApp *pApp=(CViewSwitchApp*)AfxGetApp();
//Close all the ViewA
POSITION posDoc=pApp->m_pDocTemplateA->GetFirstDocPosition();
while(NULL!=posDoc)
{
pDoc=pApp->m_pDocTemplateA->GetNextDoc(posDoc);
POSITION posView=pDoc->GetFirstViewPosition();
while(NULL!=posView)
{
pView=pDoc->GetNextView(posView);
pView->GetParent()->PostMessage(WM_CLOSE);
}
}
CMDIChildWnd::OnClose(); //Close all the ViewB
posDoc=pApp->m_pDocTemplateB->GetFirstDocPosition();
while(NULL!=posDoc)
{
pDoc=pApp->m_pDocTemplateB->GetNextDoc(posDoc);
POSITION posView=pDoc->GetFirstViewPosition();
while(NULL!=posView)
{
pView=pDoc->GetNextView(posView);
pView->GetParent()->PostMessage(WM_CLOSE);
}
}
}
----------------------
给你一个例子
只要你获得了文档魔板的指针就好办了
------------------------
我按照你的要求作了一个demo工程!
工程ViewSwitch,为了便于操作,我在winapp中添加了几个文档魔板:
class CViewSwitchApp : public CWinApp
{
public:
CMultiDocTemplate *m_pDocTemplateMain;
CMultiDocTemplate *m_pDocTemplateA;
CMultiDocTemplate *m_pDocTemplateB;
CMultiDocTemplate *m_pDocTemplateC;
CViewSwitchApp();....
};BOOL CViewSwitchApp::InitInstance()
{
.....
m_pDocTemplateMain = new CMultiDocTemplate(
IDR_VIEWSWTYPE,
RUNTIME_CLASS(CViewSwitchDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(CViewSwitchView));
AddDocTemplate(m_pDocTemplateMain); m_pDocTemplateA = new CMultiDocTemplate(
IDR_VIEWSWTYPE,
RUNTIME_CLASS(CViewSwitchDoc),
RUNTIME_CLASS(CFrameA), // custom MDI child frame
RUNTIME_CLASS(CViewA));
AddDocTemplate(m_pDocTemplateA); m_pDocTemplateB = new CMultiDocTemplate(
IDR_VIEWSWTYPE,
RUNTIME_CLASS(CViewSwitchDoc),
RUNTIME_CLASS(CFrameB), // custom MDI child frame
RUNTIME_CLASS(CViewB));
AddDocTemplate(m_pDocTemplateB); m_pDocTemplateC = new CMultiDocTemplate(
IDR_VIEWSWTYPE,
RUNTIME_CLASS(CViewSwitchDoc),
RUNTIME_CLASS(CFrameC), // custom MDI child frame
RUNTIME_CLASS(CViewC));
AddDocTemplate(m_pDocTemplateC);
....
}
另外添加了几个类:CFrameA,CFrameB,CFrameC,CViewA,CViewB,CViewC
问题1:如何在关闭A视时同时关闭B视
void CFrameA::OnClose()
{
// TODO: Add your message handler code here and/or call default CView *pView;
CDocument *pDoc;
CViewSwitchApp *pApp=(CViewSwitchApp*)AfxGetApp();
//Close all the ViewA
POSITION posDoc=pApp->m_pDocTemplateA->GetFirstDocPosition();
while(NULL!=posDoc)
{
pDoc=pApp->m_pDocTemplateA->GetNextDoc(posDoc);
POSITION posView=pDoc->GetFirstViewPosition();
while(NULL!=posView)
{
pView=pDoc->GetNextView(posView);
pView->GetParent()->PostMessage(WM_CLOSE);
}
}
CMDIChildWnd::OnClose(); //Close all the ViewB
posDoc=pApp->m_pDocTemplateB->GetFirstDocPosition();
while(NULL!=posDoc)
{
pDoc=pApp->m_pDocTemplateB->GetNextDoc(posDoc);
POSITION posView=pDoc->GetFirstViewPosition();
while(NULL!=posView)
{
pView=pDoc->GetNextView(posView);
pView->GetParent()->PostMessage(WM_CLOSE);
}
}
}
问题2:(A,B视显示时)现又显示一个视C,但现示C时要使A视使去焦点(即鼠标点不到A视,但能点到B视),关闭C视时让A视重获焦点.
我的做法是使在ViewC显示的时候ViewA失效!这样ViewA 就不可能得到焦点了!关闭ViewC后,再使ViewA 激活,并使其成为当前窗口!
//同时产生两个窗口
void CMainFrame::OnViewaViewb()
{
// TODO: Add your command handler code here
CViewSwitchApp *pApp=(CViewSwitchApp*)AfxGetApp();
pApp->m_pDocTemplateA->OpenDocumentFile(NULL);
pApp->m_pDocTemplateB->OpenDocumentFile(NULL);
}
//产生ViewC,并且使ViewA失去焦点
void CMainFrame::OnViewc()
{
// TODO: Add your command handler code here
CViewSwitchApp *pApp=(CViewSwitchApp*)AfxGetApp();
pApp->m_pDocTemplateC->OpenDocumentFile(NULL); CView *pView;
CDocument *pDoc;
POSITION posDoc=pApp->m_pDocTemplateA->GetFirstDocPosition();
while(NULL!=posDoc)
{
pDoc=pApp->m_pDocTemplateA->GetNextDoc(posDoc);
POSITION posView=pDoc->GetFirstViewPosition();
while(NULL!=posView)
{
pView=pDoc->GetNextView(posView);
pView->GetParent()->EnableWindow(false);
}
}
}//激活ViewA
void CFrameC::OnClose()
{
// TODO: Add your message handler code here and/or call default CView *pView;
CDocument *pDoc;
CViewSwitchApp *pApp=(CViewSwitchApp*)AfxGetApp();
POSITION posDoc=pApp->m_pDocTemplateA->GetFirstDocPosition();
while(NULL!=posDoc)
{
pDoc=pApp->m_pDocTemplateA->GetNextDoc(posDoc);
POSITION posView=pDoc->GetFirstViewPosition();
while(NULL!=posView)
{
pView=pDoc->GetNextView(posView);
pView->GetParent()->EnableWindow(true); }
}
CMDIChildWnd::OnClose();
//activate one view of CViewA!
pView->GetParent()->BringWindowToTop();
}
问题3: 用鼠标点击不同视时要弹出信息(说明在XX框架或视加具体的消息即可)
很简单呀!
void CViewA::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
MessageBox("Hi my friend,I am from the viewA,Enjoy yourself!");
CView::OnLButtonDown(nFlags, point);
}
void CViewB::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
MessageBox("Hi my friend,I am from the viewB,Enjoy yourself!");
CView::OnLButtonDown(nFlags, point);
}
void CViewC::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
MessageBox("Hi my friend,I am from the viewC,Enjoy yourself!");
CView::OnLButtonDown(nFlags, point);
}4、切换视图时要连同View对应的框架一同切换
切换视图的时候,当然会连同View对应的框架一同切换。View离不开自己的框架呀!
你可以去找一下有关MDI程序的例子来参考!
www.vckbase.com上面就有很多了!
可是我想做的是在单文档框架中切换多个文档,就是连frame,doc,view一起换成新的.当然用多文档类就不会很麻烦了,但是我的项目要求单文档框架.我试了试,结果老是弹出新的顶层框架窗口.也难怪.我想看看能不能在弹出新的顶层框架窗口同时关闭原来的框架,而原来的框架是和m_pMainFrame相连的,把他关闭了,结果程序就退出了.
不知道有没有人有过这方面的经验?