C++ MainFrame.cpp中的MFC MDI打开视图

C++ MainFrame.cpp中的MFC MDI打开视图,c++,mfc,mdi,C++,Mfc,Mdi,我正试图在点击按钮时打开一个新视图。首先显示一个对话框,当我单击ok时,应该显示视图。它是在MainFrame.cpp中实现的。 代码如下: void CMainFrame::OnBtnUnosProizvoda() { // TODO: Add your command handler code here UnosProizvodaDlg dlg; if (dlg.DoModal() == IDOK) { CView* pCurrentView

我正试图在点击按钮时打开一个新视图。首先显示一个对话框,当我单击ok时,应该显示视图。它是在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();
    }
    }

}
新视图应与其他视图一样固定。然而,我得到的结果是:


它似乎卡在主机的左上角,而不是子框架。如何修复此问题?

您的项目有一个派生自
CWinAppEx
的类。此类的名称类似于
CMyApp
(您的名称可能会有所不同)。为
OnFileNew

class CMyApp : public CWinAppEx
{
public:
    CMyApp();
    virtual BOOL InitInstance();
    virtual int ExitInstance();

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

请参阅mainframe标记的说明,以了解我删除它的原因。您是否试图调用
OnFileNew()
来创建新的MDI子项或选项卡?来创建新的MDI子项。这样就行了!不过我还有一件事,这个OnFileNew()将在返回IDOK的对话框后调用。我需要将一些数据从对话框传递到子框架。怎么做?你的第二个问题是无关的。你可以接受这个问题并提出一个单独的问题。提供有关设置的更多信息。您通常需要更改文档数据,但是
CFrameWnd::GetActiveDocument()
可能返回
NULL
,您必须采取不同的路径。检查返回值。它返回“NULL”,因为此时没有活动文档或视图。您能给我提供更多关于如何实现它的信息吗?我在做这件事时遇到了困难?
CFrameWnd::GetActiveDocument()
即使在添加了新文档之后,也可能返回
NULL
。你应该提供更多的信息。不清楚您拥有什么和想要什么,我假设您想要一个指向
CMyDocument
CMyChild
void CMainFrame::OnBtnUnosProizvoda()
{
    theApp.OnFileNew();
}