在MFC中获取现有但非活动的视图 [英] getting an existing but non-active view in MFC
问题描述
说我想访问MFC MDI应用程序中的doc类中的特定视图(如果已打开).该视图可以当前处于活动状态,也可以不处于活动状态.如果我可以假定视图始终处于活动状态,则可以按照以下说明进行操作
Say I want to access a specific view in a doc class in MFC MDI application if it is already opened. The view can be currently active or not. If I can assume the view is always active, I can follow this instruction
http://support.microsoft.com/kb/108587
,但视图也可以处于非活动状态.有什么办法吗?
but the view can be also non-active. Is there any way to do this?
推荐答案
执行此操作的方法较短,但这是简单的方法.假定以下代码是CMainFrame类中的菜单处理程序:
There shorter ways to do this, but here's the straight up way. Assume the following code is a menu handler in your CMainFrame class:
for( POSITION pos = AfxGetApp()->GetFirstDocTemplatePosition(); pos != NULL; )
{
CDocTemplate* pTempl = AfxGetApp()->GetNextDocTemplate( pos );
for( POSITION pos1 = pTempl->GetFirstDocPosition(); pos1!= NULL; )
{
CDocument* pDoc = pTempl->GetNextDoc( pos1 );
for( POSITION pos2 = pDoc->GetFirstViewPosition(); pos2 != NULL; )
{
CView* pView = pDoc->GetNextView( pos2 );
if( pView->IsKindOf( RUNTIME_CLASS(...) ) )
{
// Do what you need with the view...
}
}
}
}
基本上,您必须获得一个指向模板的指针,查看与之关联的CDocument,对于每个CDocument,遍历该文档所附的所有视图.
You basically have to get a pointer to the Template, look at the CDocuments associated with it, and for each CDocument, traverse through all the views attached to the document.
如果仅使用一个模板,一个文档和附加的多个视图,则可以将该模板保存在CMainFrame类中,并可以通过调用AfxGetApp()-> m_pTemplate来更快地获取它.
If you only use one template, one document, and multiple views attached, you can save the template in CMainFrame class and get to it faster by calling AfxGetApp()->m_pTemplate.
MFC在某些方面很时髦,但是如果需要,它可以使您进入Doc/View体系结构的任何部分.
MFC is funky in some ways, but it lets you get to any part of the Doc/View architecture if you needed.
希望这会为您指明正确的方向.
Hope this points you in the right direction.
这篇关于在MFC中获取现有但非活动的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!