【问题标题】:Closing all child windows in MFC MDI applications关闭 MFC MDI 应用程序中的所有子窗口
【发布时间】:2014-06-09 06:29:29
【问题描述】:

我正在使用 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();
            }
        }
    }

执行此代码时,在调试模式下,它看起来正在关闭所有窗口,并且 UI 在整个子窗口区域显示黑屏。
我想在关闭所有子窗口后更新这个窗口区域。
如何更新此区域?

【问题讨论】:

  • “更新”是什么意思?
  • 我猜,您可以为子框架调用 Invalidate()。 msdn.microsoft.com/en-us/library/ax04k970.aspx
  • 正常情况下,当一个窗口关闭或销毁时,相应的父窗口区域会被重绘。无需采取任何行动。如果重绘时出现问题,则您的 WM_PAINT 处理程序中存在一些故障。

标签: visual-c++ mfc mdi mdichild


【解决方案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...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多