theEagles

I am sailing, to be with you, to be free.
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

MFC MDI miscellaneous

Posted on 2012-08-28 11:39  theEagles  阅读(186)  评论(0)    收藏  举报

截获MDI tab 的关闭按钮事件:

重载ChildFrame的OnClose函数,在其中拦截关闭事件。

例子:在其中判断是否是最后一个View,如果是最后一个,则关闭之前创建一个新的View,确保文档对象不被关闭。

创建新View可以重载ChildFrame的OnWindowNew函数:

OnWindowNew
void CGraphFrame::OnWindowNew()
{
   CMainFrame* pMainFrame = (CMainFrame*) AfxGetMainWnd();
   
   CDocument* pDoc=pMainFrame->MDIGetActive()->GetActiveDocument();
   pMainFrame->CreateNewView(IDI_CLASS_VIEW, RUNTIME_CLASS(CGraphFrame),RUNTIME_CLASS(CGraphExView),pDoc);

   //CMDIFrameWnd::OnWindowNew();
}

重载OnClose函数:

OnClose
void CGraphFrame::OnClose()
{
   // TODO: Add your message handler code here and/or call default
   CGraphExDoc* pDoc = dynamic_cast<CGraphExDoc*>( GetActiveDocument() );
   
   POSITION pos = pDoc->GetFirstViewPosition();
   int nView=0;
   while(pDoc->GetNextView(pos)) 
   {
      nView++;
   }

   if( nView==1 )
   {
      OnWindowNew();
      CGraphExView* pView = dynamic_cast<CGraphExView*>( GetActiveView() );
      SetActiveView(pView);
   }

   CMDIChildWndEx::OnClose();
}