MainFrame.cpp中的MFC MDI打开视图

时间:2016-08-11 14:58:55

标签: c++ mfc mdi

我正在尝试打开按钮点击的新视图。首先显示一个对话框,当我单击确定时,应显示视图。它是在MainFrame.cpp中实现的。 这是代码:

void CMainFrame::OnBtnUnosProizvoda()
{
    // TODO: Add your command handler code here
    UnosProizvodaDlg dlg;
    if (dlg.DoModal() == IDOK)
    {
        CView* pCurrentView = GetActiveView();

    // We are about to change the view, so we need a pointer to the runtime class
    CRuntimeClass* pNewView;

    // We will process a form
    pNewView = RUNTIME_CLASS(CPrikaz2View);
        CCreateContext crtContext;

    // We have a new view now. So we initialize the context
        crtContext.m_pNewViewClass = pNewView;
    // No need to change the document. We keep the current document
    crtContext.m_pCurrentDoc   = GetActiveDocument();

    CPrikaz2View* pNewViewer = STATIC_DOWNCAST(CPrikaz2View, CreateView(&crtContext));

    // Now we can create a new view and get rid of the previous one
    if( pNewViewer != NULL )
    {
        pNewViewer->ShowWindow(SW_SHOW);

        pNewViewer->OnInitialUpdate();
        SetActiveView(pNewViewer);
        RecalcLayout();

    //  pCurrentView->DestroyWindow();
    }
    }

}

新视图应作为其余视图停靠。然而,这是我得到的结果:

Click here to see the image

它似乎停留在大型机的左上角而不是子框架。如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您的项目有一个派生自CWinAppEx的类。此类的名称类似于CMyApp(您的名称将以不同的名称命名)。为OnFileNew

添加以下覆盖
class CMyApp : public CWinAppEx
{
public:
    CMyApp();
    virtual BOOL InitInstance();
    virtual int ExitInstance();

    void OnFileNew() //<== add this
    {
        CWinAppEx::OnFileNew();
    }
    ...
};

现在你可以创建新的mdi孩子

void CMainFrame::OnBtnUnosProizvoda()
{
    theApp.OnFileNew();
}