C ++ MFC MDI数据作为文档不在磁盘上

时间:2018-07-04 15:46:08

标签: c++ mfc mdi

我想使用MFC MDI,但是我的数据(文档)不在磁盘上。

我具有根据输入生成数据的功能。 A,B等数据

我想为生成的每个新数据打开一个新标签。

例如,如果生成的数据输入为X,我想打开一个新的标签,其名称为X。如果X存在,我想像MDI一样激活(放在最前面)X选项卡。

我的想法是拥有多个CMultiDocTemplate。每个由不同的CDocument / CView派生类生成。

我正在寻找使open / new函数超载的解决方案,因此,与其从磁盘上打开文件,不如从我的函数中请求生成数据(CDocument)。

此外,我不希望用户在CMultiDocTemplate / Cview之间进行选择,但我想选择用于新选项卡的那个。

1 个答案:

答案 0 :(得分:1)

我不确定这是否是您要查找的内容,但是我使用了如下代码,根据加载的文档将视图更改为另一视图:

BOOL CCommunityTalksDoc::SwitchToView(CRuntimeClass* pNewViewClass)
{
    POSITION        rPos;
    CView           *pOldActiveView;
    CFrameWnd       *pChild;
    CCreateContext  context;
    BOOL            bAutoDelete;

    rPos = GetFirstViewPosition();
    pOldActiveView = GetNextView(rPos);
    pChild = pOldActiveView->GetParentFrame();

    // If we're already displaying this kind of view, no need to go further.
    if (pOldActiveView->IsKindOf(pNewViewClass))
        return TRUE;

    // Set flag so that document will not be deleted when view is destroyed.
    bAutoDelete = m_bAutoDelete;
    m_bAutoDelete = FALSE;
    // Delete existing view
    pOldActiveView->DestroyWindow();
    // restore flag
    m_bAutoDelete = bAutoDelete;

    // Create new view.
    m_pScriptView = (CScriptParseView*)pNewViewClass->CreateObject();
    if (m_pScriptView == nullptr)
    {
        TRACE1("Warning: Dynamic create of view type %s failed\n", pNewViewClass->m_lpszClassName);
        return FALSE;
    }

    // we must ensure the popup dialogues don't display
    m_pScriptView->SetBuildMode(FALSE);

    // Draw new view.
    context.m_pNewViewClass = pNewViewClass;
    context.m_pCurrentDoc = this;
    context.m_pNewDocTemplate = nullptr;
    context.m_pLastView = nullptr;
    context.m_pCurrentFrame = pChild;
    if (!m_pScriptView->Create(nullptr, nullptr, AFX_WS_DEFAULT_VIEW, CRect(0, 0, 0, 0), 
                pChild, AFX_IDW_PANE_FIRST, &context))
    {
        TRACE0("Warning: couldn't create view for frame\n");
        delete m_pScriptView;
        m_pScriptView = nullptr;
        return FALSE; 
    }

    m_pScriptView->SendMessage(WM_INITIALUPDATE, 0, 0);  // WM_INITIALUPDATE is defined in afxpriv.h
    pChild->RecalcLayout();
    m_pScriptView->UpdateWindow();
    pChild->SetActiveView(m_pScriptView);
    return TRUE;
}

您的使用方式如下:

CRuntimeClass *pNewViewClass = RUNTIME_CLASS(CScriptParseView);
if (!SwitchToView(pNewViewClass))
{
    // fail, don't know why it would fail
    ASSERT(FALSE);
}