2014-06-09 66 views
0

我正在使用MFC MDI應用程序。我想關閉通知上的所有子窗口。爲此,我使用此代碼:關閉MFC MDI應用程序中的所有子窗口

CMDIFrameWnd *pFrame = NULL; 
    CMDIChildWnd *pChild = NULL; 
    CDocTemplate* pDocTemplate = NULL; 
    CDocument* pDoc = NULL; 

    for (POSITION pos = AfxGetApp()->GetFirstDocTemplatePosition(); pos != NULL;) 
    { 
     pDocTemplate = AfxGetApp()->GetNextDocTemplate(pos); 

     for (POSITION pos1 = pDocTemplate->GetFirstDocPosition(); pos1 != NULL;) 
     { 
      if (pos1 == NULL) 
       break; 
      CDocument* pDoc = pDocTemplate->GetNextDoc(pos1); 

      for (POSITION pos2 = pDoc->GetFirstViewPosition(); pos2 != NULL;) 
      { 
       CView* pView = (CSignalWindow*)pDoc->GetNextView(pos2); 
       pView->CloseWindow(); 
      } 
     } 
    } 

當執行該代碼,在調試模式下,它看起來它關閉所有窗口和用戶界面顯示在整個子窗口區域黑屏。
我想在關閉所有子窗口後更新此窗口區域。
如何更新此區域?

+0

你是什麼意思「更新」? – user1793036

+0

我想,你可以調用子幀的Invalidate()。 http://msdn.microsoft.com/en-us/library/ax04k970.aspx – cha

+1

這是正常情況,當一個窗口關閉或銷燬時,相應的父窗口區域被重畫。沒有必要採取任何行動。如果重繪時出現問題,那麼您的WM_PAINT處理程序中會出現一些故障。 – xMRi

回答

1

您不應該關閉視圖。只需關閉父框架即可。

for (POSITION posTemplate = AfxGetApp()->GetFirstDocTemplatePosition(); pos != NULL;) 
{ 
    pDocTemplate = AfxGetApp()->GetNextDocTemplate(posTemplate); 

    POSITION posDoc; 
    while (posDoc = pDocTemplate->GetFirstDocPosition()) 
    { 
     CDocument* pDoc = pDocTemplate->GetNextDoc(posDoc); 

     POSITION posView; 
     while (posView=pDoc->GetFirstViewPosition()) 
     { 
      CView* pView = pDoc->GetNextView(posView); 
      pView->GetParentFrame()->DestroyWindow(); 
     } 
    } 
} 

因爲你想關閉所有,你只需要得到名單的頭並將其刪除。 如果您在一個子框架(即分隔窗口)中收集視圖,則使用DestroyWindow作爲框架可能會刪除多個視圖。

有絕不應重繪問題,因爲父窗口始終重繪它的客戶區,當一個子窗口被破壞,只要你不使用SetRedraw ...

相關問題