关于MDI多视图的问题 我建立了一个MDI程序,视窗是基于FORM的。我建立了两个视类,然后将这两个类基于同一个文档,在该文档中添加了一个公有变量。问题是:我在其中一个视图中修改了该变量的值,为什么在另一个视图中读该量的值时没有改变? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在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; } }}然后,我在其中一个视图中修改了文档中的成员变量的值,完成以后,我在另一个视中读取该变量的值却没有变化。 在其中一个视图中修改了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; ... ... } 将源代码给我发过来,我帮你调一下![email protected] qynum123() 源码已经给你邮出了 实际上,hello和good分别生成了两个doc,注意,是两个!虽然他们基于同一个类,却是两个实例。正确的做法是先判断有无doc实例存在,若无,才new一个用CDocTemplate::GetFirstDocPosition(),CDocTemplate::GetNectDocument()判断 看了一下你的源程序,你似乎并不是没有产生对应于同一文档的两个视图而是将同一个文件把开了两次: 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两个函数就行了。 以下代码仅供参考: 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); MFC 如何把一个窗体嵌入到另一个窗体中 关于tree control控件的问题 请问如何去除CString里的符号 求教char[]的中数据所占的长度问题问题? VC高手进,紧急求助,在线等待? 请问五天内有没有一般性的可能用NETMEETING的SDK做好语音和视频通讯? word自动服务器编程——关于Find类 请大家讨讨论一下VC的用途的用途:VC适合做应用软件吗? CopyFile拷贝文件成功 CFile Read提示找不到文件 <opengl编程指南>的第一个例子,终于能编译通过了,但是,运行中提示少文件 怎样在PICTUREBOX中划曲线!,急! 这条SQL语句如何写?
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;
} }
}
然后,我在其中一个视图中修改了文档中的成员变量的值,完成以后,我在另一个视中读取该变量的值却没有变化。
pDoc->m_strMsg="new value";
pDoc->UpdateAllViews(...); // ...是什么,查MSDNCMyOtherView就会接到UPDATE通知
CMyOtherView::OnUpdate(...) // ...是什么,查MSDN
{
... ... CString str=pDoc->m_strMsg; ... ...
}
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两个函数就行了。
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);