Вы находитесь на странице: 1из 4

MFC MDI Application Glue - CodeProject https://www.codeproject.com/Tips/28369/MFC-MDI-Application-Glu...

Articles » Desktop Development » Document / View » General

Christian Kleinheinz, 24 Jul 2010

Retrieving the correct pointer anytime and anywhere in MFC MDI applications

Download source - 1.8 KB

The MfcMdiAppGlue class is an abstract class which implements several static methods for retrieving the pointer
to the created documents, views or frame windows as well as for getting the currently active document/view/frame pointers.

The MFC framework provides the CWinApp* AFXAPI AfxGetApp( ); method for retrieving the pointer to the
application object anywhere inside your MFC application. There is another method CView::GetDocument() to get the
pointer to the currently active document within a CView derived class. When developing MFC applications, you shouldn’t need
anything else in order to develop your application if you did follow the pattern behind the framework. But once you are working
with multiple document types in combination with non-modal dialogs which should operate on these document instances, you
will come across the problem that you can't get the pointer of the documents and views by simply calling a single method. I
came across this problem multiple times and the third time I did implement nearly the same method, I decided to add this
functionality to my class libraries.

The class header is shown below:

class ACMfcMdiAppGlue
{
public:
virtual ~ACMfcMdiAppGlue();

//Get the current active view window
static CView* GetActiveViewWnd();
    
//Get the current active document
static CDocument* GetActiveDoc();

1 of 4 10/1/2018, 11:15 AM
MFC MDI Application Glue - CodeProject https://www.codeproject.com/Tips/28369/MFC-MDI-Application-Glu...

    
//create a new instance of the document with a doc/view/frame triple 
static BOOL CreateNewDocumentInstanceTriple(CDocument*& pDoc);

// Function fills a list of all document objects in the application
static void GetAllDocuments(t_DocumentList &Docs);

// Function fills a list of all views in the application
static void GetAllViews(t_ViewList &Views);

// Function fills a list of all Child frames in the application
// i.e. all CFrameWnd's except that returned by AfxGetMainWnd().
static void GetAllFrameWnds(t_FrameList &Frames);
};

The first method of this class GetActiveViewWnd() returns a pointer to the currently active view window if there is one.
Otherwise, this method returns a NULL pointer.

The implementation of this method is straight forward with the usage of the GetActiveView() method of the
CMDIChildWnd class.

GetActiveDoc() is implemented in the same way as its counterpart for retrieving the active document with the usage of
the GetActiveDocument() method of the CMDIChildWnd class.

CView* ACMfcMdiAppGlue::GetActiveViewWnd()
{
  CWinApp* pApp = AfxGetApp();
    
if((!pApp) || (!pApp‐>m_pMainWnd))
return(NULL);

// Get a pointer to the CMDIChildWnd by casting the main window pointer
  CMDIChildWnd * pChild = ((CMDIFrameWnd*)(pApp‐>m_pMainWnd))‐>MDIGetActive();

if ( !pChild )
return NULL;
      
// Get the active view window
  CView* pView = pChild‐>GetActiveView();

if(!pView)
return(NULL);

return(pView);
}

Simple, isn’t it? The MFC framework provides everything you need… Let’s take a look at the other methods of this class.

The method:

static BOOL CreateNewDocumentInstanceTriple(CDocument*& pDoc) 

is used to create a new document with a new view window correctly embedded into its child frame window.

The parameter of this method is a reference to a pointer of the type CDocument which is used as output parameter of this
method.

The method returns the first document template found within the MFC- MDI document manager CDocManager. If you did
add additional document templates, you may want to modify this method in order to return the correct one.

Additional information about the document/view/frame- window architecture can be found on CodeProject or on MSDN.

The methods GetAllDocuments, GetAllViews and GetAllFrameWnds can be used in order to get the pointers to
all currently opened documents views or frame- windows.

Let’s just look at one of these three methods as this is enough to understand how to implement all three of them.

2 of 4 10/1/2018, 11:15 AM
MFC MDI Application Glue - CodeProject https://www.codeproject.com/Tips/28369/MFC-MDI-Application-Glu...

void ACMfcMdiAppGlue::GetAllViews(t_ViewList &Views)
{
// Get the MFC application pointer
  CWinApp *pApp = AfxGetApp();
    
// Get the pointer to the document manager of this application
  CDocManager *pDocMgr = pApp‐>m_pDocManager;

// For all document templates
for(POSITION TmplPos = pDocMgr‐>GetFirstDocTemplatePosition();TmplPos;)
  {
// Get the next doc‐ template
    CDocTemplate *pTmpl = pDocMgr‐>GetNextDocTemplate(TmplPos);

    ASSERT_VALID(pTmpl);  
if(pImpl == NULL)
continue;
    
// For All open documents of this document template type.
for(POSITION Pos = pTmpl‐>GetFirstDocPosition();Pos;) 
    {
      CDocument *pDoc = pTmpl‐>GetNextDoc(Pos);            
// For all views of this document
      POSITION ViewPos = pDoc‐>GetFirstViewPosition();
while(ViewPos)
      {
        CView *pView =  pDoc‐>GetNextView(ViewPos);         
        Views.AddTail(pView);
      }
    }
  }
}

To use this class within your MFC- MDI application, simply include the two files within the zip archive into your project and
include the MfcMdiAppGlue.h header file.

Once you have included the header, you can use the methods of the abstract class by simply calling the method with the
use of the ACMfcMdiAppGlue namespace.

To get the currently active view of the type CScrollView, simply call the method and cast the pointer like shown below:

CScrollView* pView = (CScrollView*)ACMfcMdiAppGlue::GetActiveViewWnd(); 

To create a new document / view / frame- window triple, just create a pointer of the type CDocument and pass it to the
method. The triple will be added correctly to the MFC document manager.

CDocument* pDoc = NULL;

ACMfcMdiAppGlue::CreateNewDocumentInstanceTriple(pDoc);

//do what every you need to do with your new document, view, frame window triple
//...

The class is licensed under the terms of the The Code Project Open License (CPOL) 1.02 and is free for private and commercial
use, modification and distribution with your own source code.

If you have additional questions, do not hesitate to contact us by visiting our homepage.

3 of 4 10/1/2018, 11:15 AM
MFC MDI Application Glue - CodeProject https://www.codeproject.com/Tips/28369/MFC-MDI-Application-Glu...

Please feel free to give some constructive feedback if there is something in your mind.

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CEO MasterSoft Software Solutions Ltd.


Austria

Chris is one of the founders of MasterSoft Software Solutions Ltd., a software


development company located in Austria.

He has worked as a C++ developer in the fields of 3D computer simulations and


studied Business Informatics before the company opening.

Chris is using C++/C# intensely but also has experience in Java or PHP.

To get additional information about him or his company feel free to visit the webpage
of his company or visit his profiles in the social networks like G+, Twitter or Xing for all
german networkers.

You are also welcome to visit the webpage for all hosting services his company offers at
MasterServer

First Prev Next

My vote of 4 varandas79 11-Dec-10 3:00 

Not easy to use, but a necessary tool.

Last Visit: 30-Sep-18 19:48     Last Update: 1-Oct-18 11:14 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin   

1 message has been posted for this article Visit https://www.codeproject.com/Tips/28369/MFC-MDI-Application-Glue


to post and view comments on this article, or click here to get a print view with messages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile Article Copyright 2008 by Christian Kleinheinz
Web01-2016 | 2.8.180920.1 | Last Updated 24 Jul 2010 Everything else Copyright © CodeProject, 1999-2018

4 of 4 10/1/2018, 11:15 AM

Вам также может понравиться