1.在有多个分视图的doc中,取得指定类型的view的指针
CView* CXXXDoc::GetView(CRuntimeClass* pClass)
{
CView* pView;
POSITION pos=GetFirstViewPosition();
//取得第一个视图的位置,到pos
while(pos!=NULL){
pView=GetNextView(pos);
//取得pos指向的视图指针,然后将pos指向下一个视图
if(pView->IsKindOf(pClass))
//如果pView就是要找的那个view,则break,即跳出循环,已经找到
break;
}
if(!pView->IsKindOf(pClass)){
AfxMessageBox("No view");
return NULL;
}
return pView;
}
pClass传个你想的view做参数:
CXXXView* pXXXView=(CXXXView*)GetView(RUNTIMDE_CLASS(CXXXView));
void CM32Doc::Ontest()
{
// TODO: Add your command handler code here
CMyListView* pMyListView=(CMyListView*)GetView(RUNTIME_CLASS(CMyListView));
TRACE("%d",pMyListView->listview_var);
}
http://4develop.in/page/CPPLanguage/20110519_20_7d4f3ef2-efa6-42de-a800-ef97082d71b3/%E8%AF%B7%E9%97%AEMFC%E5%8D%95%E6%96%87%E6%A1%A3%E6%80%8E%E4%B9%88%E5%9C%A8DOC%E7%B1%BB%E4%B8%AD%E8%8E%B7%E5%BE%97VIEW%E7%B1%BB%E7%9A%84%E6%8C%87%E9%92%88.html
2.在多文档中建立的一个对话框中,即外部,获取文档或视图的指针。
//需要包含各个头文件
#include "MainFrm.h"
#include "SMSAlarmDoc.h"
#include "MyListView.h"
#include "ChildFrm.h"
void CComSettingDlg::OnOK()
{
//CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;//获取主框架的指针
//CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();//获取活动子框架的指针
CMainFrame *pFrame = (CMainFrame*)AfxGetApp()->m_pMainWnd;
CChildFrame *pChild = (CChildFrame *) pFrame->GetActiveFrame();
CMyListView *pView = (CMyListView *) pChild->GetActiveView();//获取活动视图CMyListView 的指针
CSMSAlarmDoc* pDocument=(CSMSAlarmDoc*) pChild -> GetActiveDocument();
TRACE("%d",pView->listview_var);//获取视图的一个public变量
}
http://zhidao.baidu.com/question/251413205.html
一个通用的步骤
1.CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
// CMainFrame *pFrame = (CMainFrame*)AfxGetApp()->m_pMainWnd;//#include "MainFrm.h"
2.CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();
3.
3.1获得视类:CMyView *pView = (CMyView *) pChild->GetActiveView();
3.2获得文档类:CDocument* pDocument=pChild -> GetActiveDocument();
句柄获取:
1.凡是派生于CWnd的类,即窗口类,都自动继承了m_hWnd。所以只要知道这个类的指针,直接使用m_hWnd 就访问到句柄了