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?
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...
}
}
}
}
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.
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 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With