MFC中如何获得View指针
1个回答
展开全部
对于SDI程序来说, 如果;CMainFrame中只有一个view的话, 那很好办, 直接用函数获得view指针即可,
先获得CFrameWnd指针, 方法是CMainFrame *pFrame = AfxGetMainWnd();
然后CView *pView = pFrame-GetAcitveView();就ok了
但有时候有多个view存在, 这时候获得view指针就会出问题了, 可以用如下方法:
应该是没有问题的
CView *GetView(){CWinApp *winApp = AfxGetApp();
if(winApp == NULL) return NULL;
CDocManager *pDocManager = winApp-m_pDocManager;
if(pDocManager == NULL) return NULL;
POSITION pos1 = pDocManager-GetFirstDocTemplatePosition();
while (pos1){CDocTemplate *pDocTemplate = pDocManager-GetNextDocTemplate(pos1);
if(pDocTemplate == NULL) continue;
POSITION pos2 = pDocTemplate-GetFirstDocPosition();
while (pos2){CDocument *pDoc = pDocTemplate-GetNextDoc(pos2);
if(pDoc == NULL) continue;
POSITION pos3 = pDoc-GetFirstViewPosition();
先获得CFrameWnd指针, 方法是CMainFrame *pFrame = AfxGetMainWnd();
然后CView *pView = pFrame-GetAcitveView();就ok了
但有时候有多个view存在, 这时候获得view指针就会出问题了, 可以用如下方法:
应该是没有问题的
CView *GetView(){CWinApp *winApp = AfxGetApp();
if(winApp == NULL) return NULL;
CDocManager *pDocManager = winApp-m_pDocManager;
if(pDocManager == NULL) return NULL;
POSITION pos1 = pDocManager-GetFirstDocTemplatePosition();
while (pos1){CDocTemplate *pDocTemplate = pDocManager-GetNextDocTemplate(pos1);
if(pDocTemplate == NULL) continue;
POSITION pos2 = pDocTemplate-GetFirstDocPosition();
while (pos2){CDocument *pDoc = pDocTemplate-GetNextDoc(pos2);
if(pDoc == NULL) continue;
POSITION pos3 = pDoc-GetFirstViewPosition();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询