MFC获取指定视图的指针

左右的右手 2014-10-31 03:24:01
项目是单文档多视图(SDI)。
我自己定义了一个类,myclass.
现在需要在myclass中获取指定视图的指针。

代码如下
1.在Cmydoc文档类中实现视图的获取

CView *CMyDoc::GetView(CRuntimeClass*pClass)
{
CView *pView;
POSITION pos=GetFirstViewPosition();
while(pos!=NULL)
{
pView=GetNextView(pos);
if( pView->IsKindOf(pClass) )
{
break;
}
if( !pView->IsKindOf(pClass) )
{
//AfxMessageBox( "Connot locate the View.");
return NULL;
}
}

return pView;
}


2.在自定义类myclass中获取指定的CView类指针
(1).先获取CMyDoc的指针

CMainFrame *pMain=(CMainFrame*)AfxGetApp()->m_pMainWnd;
CMyDoc *pDoc = (CMyDoc *)pMain->GetActiveDocument();

(2).通过CMyDoc类的GetView获取指定视图类指针

CMyView *pView = (CMyView *)pDoc->GetView(RUNTIME_CLASS(CMyView ));


问题:我获取的文档类指针是错误或者空的!????这里该如何调用获得单文档指针???
...全文
157 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
笨笨仔 2014-10-31
  • 打赏
  • 举报
回复
引用 7 楼 wangdongdong198910 的回复:
谢谢大家的恢复,我找到问题了! 是因为这 if( !pView->IsKindOf(pClass) ) { //AfxMessageBox( "Connot locate the View."); return NULL; } 删了就行了!!
呵呵,程序转向了
左右的右手 2014-10-31
  • 打赏
  • 举报
回复
谢谢大家的恢复,我找到问题了! 是因为这 if( !pView->IsKindOf(pClass) ) { //AfxMessageBox( "Connot locate the View."); return NULL; } 删了就行了!!
左右的右手 2014-10-31
  • 打赏
  • 举报
回复
引用 5 楼 wxhxj0268 的回复:
补充一点: 不过要注意在doc中要取得view的指针C*View要注意类C*View声明的问题, 因为默认情况下,mfc在*View.h中已经包含了*Doc.h,如果在*Doc.h中包含 *View.h,就会引起嵌套包含问题,这样要在*Doc.h中加入 class C*View; 而在*Doc.cpp中加入 #include "*View.h" 这段话的意思应该懂的吧。
这个知道!我就想保存单文档多视图中的每个视图的指针或者句柄,然后在自己定义的类中调用任意一个视图类,去更新窗口数据
笨笨仔 2014-10-31
  • 打赏
  • 举报
回复
补充一点: 不过要注意在doc中要取得view的指针C*View要注意类C*View声明的问题, 因为默认情况下,mfc在*View.h中已经包含了*Doc.h,如果在*Doc.h中包含 *View.h,就会引起嵌套包含问题,这样要在*Doc.h中加入 class C*View; 而在*Doc.cpp中加入 #include "*View.h" 这段话的意思应该懂的吧。
左右的右手 2014-10-31
  • 打赏
  • 举报
回复
引用 3 楼 wxhxj0268 的回复:
提供一个文档,供参考: 关于获得MFC窗口其它类指针的方法(CSDN) 访问应用程序的其它类 获得CWinApp: -在CMainFrame,CChildFrame,CDocument,CView中直接调用AfxGetApp()或用theApp -在其它类中只能用AfxGetApp() 获得CMainFrame: -在CMinApp中用AfxGetMainWnd()或者m_pMainWnd -在CChildFrame中可用GetParentFrame() -在其它类中用AfxGetMainWnd() 获得CChildFrame: -在CView中用GetParentFrame() -在CMainFrame中用MDIGetActive()或GetActiveFrame() -在其它类中用AfxGetMainWnd()->MDIGetActive()或AfxGetMainWnd()->GetActiveFrame() 获得CDocument: -在CView中用GetDocument() -在CChildFrame中用GetActiveView()->GetDocument() -在CMainFrame中用 -if SDI:GetActiveView()->GetDocument() -if MDI:MDIGetActive()->GetActiveView()->GetDocument() -在其它类中 -if SDI:AfxGetMainWnd()->GetActiveView()->GetDocument() -if MDI:AfxGetMainWnd()->MDIGetActive()->GetActiveView()->GetDocument() 获得CView: -在CDocument中 POSITION pos = GetFirstViewPosition();GetNextView(pos) -在CChildFrame中 GetActiveView() -在CMainFrame中 -if SDI:GetActiveView() -if MDI:MDIGetActive()->GetActiveView() -在其它类中 -if SDI:AfxGetMainWnd()->GetActiveView() -if MDI:AfxGetMainWnd()->MDIGetActive()->GetActiveView()
这个已经参考过了,但是获取的不对!
笨笨仔 2014-10-31
  • 打赏
  • 举报
回复
提供一个文档,供参考: 关于获得MFC窗口其它类指针的方法(CSDN) 访问应用程序的其它类 获得CWinApp: -在CMainFrame,CChildFrame,CDocument,CView中直接调用AfxGetApp()或用theApp -在其它类中只能用AfxGetApp() 获得CMainFrame: -在CMinApp中用AfxGetMainWnd()或者m_pMainWnd -在CChildFrame中可用GetParentFrame() -在其它类中用AfxGetMainWnd() 获得CChildFrame: -在CView中用GetParentFrame() -在CMainFrame中用MDIGetActive()或GetActiveFrame() -在其它类中用AfxGetMainWnd()->MDIGetActive()或AfxGetMainWnd()->GetActiveFrame() 获得CDocument: -在CView中用GetDocument() -在CChildFrame中用GetActiveView()->GetDocument() -在CMainFrame中用 -if SDI:GetActiveView()->GetDocument() -if MDI:MDIGetActive()->GetActiveView()->GetDocument() -在其它类中 -if SDI:AfxGetMainWnd()->GetActiveView()->GetDocument() -if MDI:AfxGetMainWnd()->MDIGetActive()->GetActiveView()->GetDocument() 获得CView: -在CDocument中 POSITION pos = GetFirstViewPosition();GetNextView(pos) -在CChildFrame中 GetActiveView() -在CMainFrame中 -if SDI:GetActiveView() -if MDI:MDIGetActive()->GetActiveView() -在其它类中 -if SDI:AfxGetMainWnd()->GetActiveView() -if MDI:AfxGetMainWnd()->MDIGetActive()->GetActiveView()
左右的右手 2014-10-31
  • 打赏
  • 举报
回复
引用 1 楼 healer_kx 的回复:
没有SetActiveView的话,GetActiveView是会返回NULL的 把代码放在document里面,要更新界面的话用UpdateAllViews
谢谢你的答复,我的问题是获取的文档类指针为空!如果把代码放在文档类中,那还是需要获取文档类的指针才行啊!
healer_kx 2014-10-31
  • 打赏
  • 举报
回复
没有SetActiveView的话,GetActiveView是会返回NULL的 把代码放在document里面,要更新界面的话用UpdateAllViews

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧