AfxGetApp()-> m_pMainWnd为框架窗口指针
在框架窗口中:CFrameWnd::GetActiveDocument得到当前活动文档指针
在框架窗口中:CFrameWnd::GetActiveView得到当前活动视指针
在视中:CView::GetDocument得到对应的文档指针。
CLhstrackingcvDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
在文档中:CDocument::GetFirstViewPosition,CDocument::GetNextView用来遍历所有和文档关联的视。
方法一:
CLhstrackingcvView *pv;
CMainFrame *pFrame=(CMainFrame*)AfxGetMainWnd();
pv=(CLhstrackingcvView*)(pFrame->GetActiveView());
方法二:
CLhstrackingcvView *pv;
POSITION pos=this->GetFirstViewPosition();
pv=(CLhstrackingcvView*)this->GetNextView(pos);
方法三:
给你DOC里加个成员函数
c/c++ code
CView* CLhstrackingcvDoc::GetView(CRuntimeClass *pClass)
{
CView* pView;
POSITION pos=GetFirstViewPosition();
while(pos!=NULL)
{
pView=GetNextView(pos);
if(!pView->IsKindOf(pClass))
break;
}
if(!pView->IsKindOf(pClass))
{
AfxMessageBox("No view");
return NULL;
}
return pView;
}
用的时候:CLhstrackingcvView* pv=(CLhstrackingcvView*)GetView(RUNTIME_CLASS(CLhstrackingcvView));
当一个文档对多视或多Dlg时,可以:
CView* pv;
POSITION pos=this->GetFirstViewPosition();
pv=this->GetNextView(pos);
在Dlg类中得到Doc类指针:
CLhstrackingcvDoc* pDoc =(CLhstrackingcvDoc*)((CFrameWnd*)AfxGetApp()->m_pMainWnd)->GetActiveView()->GetDocument();
ASSERT_VALID(pDoc);
在文档中:CDocument::GetDocTemplate得到文档模板指针
在多文档界面中:CMDIFrameWnd::MDIGetActive得到当前活动的MDI子窗口
AfxGetMianWnd()得到当前主窗口指针,另外用 AfxGetApp()-> m_pMainWnd 所得到的也是当前主窗口的指针。