Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting an existing but non-active view in MFC

Tags:

view

mfc

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?

like image 564
Tae-Sung Shin Avatar asked Oct 24 '25 04:10

Tae-Sung Shin


1 Answers

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.

like image 179
Eddie Paz Avatar answered Oct 26 '25 22:10

Eddie Paz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!