Solved

about m_viewList

Posted on 2001-06-05
1
776 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

623 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