Solved

about m_viewList

Posted on 2001-06-05
1
730 Views
Last Modified: 2013-11-20
In document/view frame, I want to execute view's function in document. I write the code as:

((CView*)m_viewList.GetHead())->...

But error happens. When I step into these code, it is shown that return value about "m_viewList.GetHead()" is NULL.

What should I handle it? Are there other methods to accomplish it? Why don't I find m_viewList in MSDN?

need your help.
0
Comment
Question by:changxing
1 Comment
 
LVL 2

Accepted Solution

by:
GloriousRain earned 50 total points
ID: 6155401
<MSDN>
HOWTO: Get Current CDocument or CView from Anywhere

Q108587


--------------------------------------------------------------------------------
The information in this article applies to:

The Microsoft Foundation Classes (MFC), used with:
Microsoft Visual C++ for Windows, 16-bit edition, versions 1.0, 1.5, 1.51, 1.52
Microsoft Visual C++, 32-bit Editions, versions 1.0, 2.0, 2.1, 4.0, 4.1

--------------------------------------------------------------------------------


SUMMARY
In a Microsoft Foundation Classes (MFC) Windows-based application, it is often useful to be able to get a pointer to the currently active document or view from anywhere in the program. For example, being able to access the current document from a dialog box is sometimes useful. This article discusses the simplest methods for doing so, using the relationships between MFC objects, and basic MFC architecture.



MORE INFORMATION
One of the cases where you might need a pointer to the currently active view or document is in a modal or modeless dialog box. Generally, a dialog box should be created by the view class, because the view is what deals with the application's user interface.

Because the view class is creating the dialog box, it can pass a pointer to itself, or the active document [obtained with the GetActiveDocument() function] to the dialog box. This could be done through the dialog box's constructor or some other member function. For modal dialog boxes, the view could also put data from the dialog box into the document when DoModal() returns.

These methods are generally preferable to relying on generic functions to return pointers to the currently active view or document.

To allow you to get a pointer to the currently active document from anywhere in the program, add a static member function to your CDocument derived class as follows:

Edit the document's header file as follows to add a static member function, GetDoc():


   // Document header file
   class CMyDoc : public CDocument
   {
      ...
      public:
      static CMyDoc * GetDoc();
      ...
   };
For a single document interface (SDI) application, add the following code to your SDI document's implementation file for CMyDoc::GetDoc():

   // SDI document implementation file
   CMyDoc * CMyDoc::GetDoc()
   {
      CFrameWnd * pFrame = (CFrameWnd *)(AfxGetApp()->m_pMainWnd);
      return (CMyDoc *) pFrame->GetActiveDocument();
   }
For a multiple document interface (MDI) application, the CMyDoc::GetDoc() code should be the following:

   CMyDoc * CMyDoc::GetDoc()
   {
      CMDIChildWnd * pChild =
          ((CMDIFrameWnd*)(AfxGetApp()->m_pMainWnd))->MDIGetActive();

      if ( !pChild )
          return NULL;

      CDocument * pDoc = pChild->GetActiveDocument();

      if ( !pDoc )
         return NULL;

      // Fail if doc is of wrong kind
      if ( ! pDoc->IsKindOf( RUNTIME_CLASS(CMyDoc) ) )
         return NULL;

      return (CMyDoc *) pDoc;
   }
To allow you to get a pointer to the currently active view from anywhere in the program, add a static member function to your CView derived class as follows:

Edit the view's header file as follows to add a static member function, GetView():

   // View header file
   class CMyView
   {
      ...
      public:
      static CMyView * GetView();
      ...
   };
For an SDI application, add the following code to your SDI view's implementation file for CMyView::GetView():

   // View implementation file
   CMyView * CMyView::GetView()
   {
      CFrameWnd * pFrame = (CFrameWnd *)(AfxGetApp()->m_pMainWnd);

      CView * pView = pFrame->GetActiveView();

      if ( !pView )
         return NULL;

      // Fail if view is of wrong kind
      // (this could occur with splitter windows, or additional
      // views on a single document
      if ( ! pView->IsKindOf( RUNTIME_CLASS(CMyView) ) )
         return NULL;

      return (CMyView *) pView;
   }
For an MDI application, the CMyView::GetView() code should be the following:

   // MDI view implementation file
   CMyView * CMyView::GetView()
   {
      CMDIChildWnd * pChild =
          ((CMDIFrameWnd*)(AfxGetApp()->m_pMainWnd))->MDIGetActive();

      if ( !pChild )
          return NULL;

      CView * pView = pChild->GetActiveView();

      if ( !pView )
         return NULL;

      // Fail if view is of wrong kind
      if ( ! pView->IsKindOf( RUNTIME_CLASS(CMyView) ) )
         return NULL;

      return (CMyView *) pView;
   }
Now, from anywhere in your program, where the document or view header files have been included, you can call:

   CMyDoc::GetDoc();
- Or -

   CMyView::GetView();
to get a pointer to the currently active document or view, respectively. These functions return NULL if there is no active document or view.

Note that a pointer to the application's CDocument or CView derived class is returned, not just a generic CDocument or CView pointer. This allows you to access members specific to your new class. The MDI versions of these functions, and the SDI version of the GetView() function, will use run-time type checking to verify that the document or view is of the correct class.



REFERENCES
For more information on this subject, and on the member functions used, please see:

Visual C++ 1.0 and Visual C++ for Windows NT "Class Library Reference, Volume I," page 35, "Relationships Among Documents, Views, Frame Windows, Templates, and the Application."

CFrameWnd documentation in the same manual.

Additional query words: kbSweptVC600

Keywords : kbDocView kbMFC kbVC kbDSupport kbGrpMFCATL
Issue type : kbhowto
Technology : kbvc


Last Reviewed: March 10, 2000
? 2001 Microsoft Corporation. All rights reserved. Terms of Use.
 



--------------------------------------------------------------------------------
Send feedback to MSDN.Look here for MSDN Online resources.
<MSDN>
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now