Click to See Complete Forum and Search --> : Getting a document pointer


Dallex
April 7th, 1999, 06:41 PM
I have a MDI app (Visual C++ 6.0) and I can get the document pointer in
each of the views. Is there a way for the MainFrame class and/or the
Application class to get the document pointer as well? I need to store
some data in the document in either the Frame class or Application class
that the views will use.

Karl
April 8th, 1999, 04:05 AM
To retrieve all the docs from your MainFrame, you may use this, for example:

CMyApp *myApp =(CMyApp *)AfxGetApp();
POSITION pos = myApp->m_pDocTemplate->GetFirstDocPosition();
CMyDoc *pDoc;

while(pos){
pDoc = (CMyDoc *)myApp->m_pDocTemplate->GetNextDoc(pos);
...
}

To get the active one, use CFrameWnd::GetActiveDocument() (see doc for more infos)

HTH.

K.

Ash to ash and clay to clay, if the enemy doesn't get you, your own folk may.

syh
April 8th, 1999, 06:54 PM
// I tried this and it seems to work:

CMDIFrameWnd *pFrame =
(CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;

// Get the active MDI child window.
CMDIChildWnd *pChild =
(CMDIChildWnd *) pFrame->GetActiveFrame();

// or CMDIChildWnd *pChild = pFrame->MDIGetActive();

// Get the active view attached to the active MDI child
// window.
CMyView *pView = (CMyView *) pChild->GetActiveView();
CMyDoc* pDoc = pView->GetDocument();
//
// you'll need to include: mydoc.h and myview.h in your mainframe header.

syh

Dong
April 8th, 1999, 07:16 PM
In MFC, the relation between View and Doc, is control by DocTemplate, if no DocTemplate at APP::InitInstance() , you can not get valide Doc pointer.
You have to direct use DocTemplate var or Doc var of APP.
Dong

Dallex
April 8th, 1999, 08:21 PM
Thanks to everyone. Program works fine now.

Jiasheng Zhu
July 18th, 1999, 02:29 PM
Hi,

I ran into the same problem with you. You said you have worked it out. Could you kindly send me a sample? Millions of thanks!

Jason