我建立了一个MDI程序,视窗是基于FORM的。我建立了两个视类,然后将这两个类基于同一个文档,在该文档中添加了一个公有变量。问题是:我在其中一个视图中修改了该变量的值,为什么在另一个视图中读该量的值时没有改变?

解决方案 »

  1.   

    在App类里的初始化函数中://CHello1View 视图1 CMultiDocTemplate* pDocTemplate;
    pDocTemplate = new CMultiDocTemplate(
    IDR_HELLO1TYPE,
    RUNTIME_CLASS(CHello1Doc),
    RUNTIME_CLASS(CChildFrame), // custom MDI child frame
    RUNTIME_CLASS(CHello1View));
    AddDocTemplate(pDocTemplate);//CGood1View  视图2 CMultiDocTemplate* pDocTemplate1;
    pDocTemplate1 = new CMultiDocTemplate(
    IDR_GOOD1TYPE,
    RUNTIME_CLASS(CHello1Doc),
    RUNTIME_CLASS(CGood1Frame), // custom MDI child frame
    RUNTIME_CLASS(CGood1View));  
    AddDocTemplate(pDocTemplate1);
    然后,在CHello1Doc中添加了一个公有成员变量:CString str1;添加两个菜单,分别用来打开这两个视图,程序如下:void CMainFrame::OnOpengood() 
    {
    POSITION nPos =AfxGetApp()->GetFirstDocTemplatePosition();
    while(nPos!=NULL)
    { CString nStr; CDocTemplate* mTemplate=AfxGetApp()->GetNextDocTemplate(nPos);
    mTemplate->GetDocString(nStr,CDocTemplate::docName);
    //mTemplate->GetDocString(nStr,CDocTemplate::DocStringIndex
    if(nStr=="Good1")
    {
    //mTemplate->LoadTemplate();
    mTemplate->OpenDocumentFile(NULL);
    return;
    } }
    }
    void CMainFrame::OnOpenhello() 
    {
    // TODO: Add your command handler code here
    POSITION nPos =AfxGetApp()->GetFirstDocTemplatePosition();
    while(nPos!=NULL)
    { CString nStr; CDocTemplate* mTemplate=AfxGetApp()->GetNextDocTemplate(nPos);
    mTemplate->GetDocString(nStr,CDocTemplate::docName);
    //mTemplate->GetDocString(nStr,CDocTemplate::DocStringIndex// MessageBox(nStr); if(nStr=="Hello1")
    {
    //mTemplate->LoadTemplate();
    mTemplate->OpenDocumentFile(NULL);
    return;
    } }
    }
    然后,我在其中一个视图中修改了文档中的成员变量的值,完成以后,我在另一个视中读取该变量的值却没有变化。
      

  2.   

    在其中一个视图中修改了doc里变量的值,另一个视图并不知道,应通过doc给他一个通知;不要直接view对view乱调用,doc是各个view之间的中枢比如说CString m_strMsg属于CMyDocCMyFormView中
      pDoc->m_strMsg="new value";
      pDoc->UpdateAllViews(...);  // ...是什么,查MSDNCMyOtherView就会接到UPDATE通知
    CMyOtherView::OnUpdate(...)  // ...是什么,查MSDN
    {
      ... ...  CString str=pDoc->m_strMsg;  ... ...  
    }
      

  3.   

    将源代码给我发过来,我帮你调一下![email protected]
      

  4.   

    qynum123() 源码已经给你邮出了
      

  5.   

    实际上,hello和good分别生成了两个doc,注意,是两个!虽然他们基于同一个类,却是两个实例。正确的做法是先判断有无doc实例存在,若无,才new一个用CDocTemplate::GetFirstDocPosition(),CDocTemplate::GetNectDocument()判断
      

  6.   

    看了一下你的源程序,你似乎并不是没有产生对应于同一文档的两个视图而是将同一个文件把开了两次:
            mTemplate->OpenDocumentFile()被调用了二次。产生两个不同的文档。因此无论你怎么更改其中的一个,只要不存盘,另一个视图都不会改变。你的原意可能是要产生基于同一个文档的两个视图。应该这样做:
    在CWinApp类中加入下面的声明:CMultiDocTemplate *pTemplateGoodl;
                                 CMultiDocTemplate *pTemplateHello;
    后面new 两个文档模板就不用说了。
    然后在CDocment类的OnOpenDocumentFile中加入如下处理。CDocTemplate* pTemplate = ((CTextApp*) AfxGetApp())->pTemplateGoodl;
     ASSERT_VALID(pTemplate);
     CFrameWnd* pFrame = pTemplate->CreateNewFrame(this,pActiveChild);
     if (pFrame == NULL)
     {
        TRACE0("Warning: failed to create new frame\n");
         AfxMessageBox(AFX_IDP_COMMAND_FAILURE);
     }
     pTemplate->InitialUpdateFrame(pFrame, this);
    然后改变pDocTemplate用上面的方法产生第二个子窗口。并且不要调用基类的OnOpenDocmentFile.这样就产生了基于同一个文档的两个视图。
    现在要做的工作就是使两个视图同步了。按照前面bhw98(bhw98)的方法
    重载视图类的OnUpdate和文档类的UpdateAllViews两个函数就行了。
      

  7.   

    以下代码仅供参考:
        CMvDocTemplate *pDocTemplate = new CMDIMVDocTemplate( IDR_MVTESTTYPE, RUNTIME_CLASS( CMvTestDoc ) );
        pDocTemplate->AddFrameTemplate(
            new CFrameTemplate( 
                IDR_MV1, 
                RUNTIME_CLASS( CChildFrame1 ), 
                RUNTIME_CLASS( CView1 ), 
                ID_VIEW_VIEW1, 
                SW_SHOWNORMAL, 
                TRUE ) );    pDocTemplate->AddFrameTemplate(
            new CFrameTemplate( 
                IDR_MV2, 
                RUNTIME_CLASS( CChildFrame1 ), 
                RUNTIME_CLASS( CView2 ), 
                ID_VIEW_VIEW2, 
                SW_SHOWNORMAL, 
                TRUE ) );    pDocTemplate->AddFrameTemplate(
            new CFrameTemplate( 
                IDR_MV3, 
                RUNTIME_CLASS( CChildFrame2 ), 
                RUNTIME_CLASS( CView3 ), 
                ID_VIEW_VIEW3, 
                SW_SHOWNORMAL, 
                FALSE ) );    pDocTemplate->AddFrameTemplate(
            new CFrameTemplate( 
                IDR_MV4, 
                RUNTIME_CLASS( CChildFrame2 ), 
                RUNTIME_CLASS( CView4 ), 
                ID_VIEW_VIEW4, 
                SW_SHOWNORMAL, 
                FALSE ) ); AddDocTemplate(pDocTemplate);
    m_server.ConnectTemplate(clsid, pDocTemplate, FALSE);