Click to See Complete Forum and Search --> : How to get a doc pointer in SDI from a docked view


cbrenn
April 29th, 1999, 01:12 PM
Hi all !

Ok let me explain all that ! I have an SDI application with a Docked window like in Visual C++ for the class view and a CView. In the docked window i have a tree view. Now what i want is to get a pointer to my one and only document from that window. I have try alot of stuff but nothing is working... My last thing is to try to post a message and make the document class map this message. But i would prefer to be able to get a pointer to my document class inside my treeview class. I have the same application but its MDI. From my tree view i am able to get a pointer using this code :

// 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();

// Get the active view attached to the active MDI child
// window.
CSDITreeTestView *pView = (CSDITreeTestView *) pChild->GetActiveView();
CSDITreeTestDoc* pDoc = pView->GetDocument();
pDoc->ChangeString("Yo !");

But this code is not working for my SDI application !
How should i do this ?
Hope someone can understand what i'm trying to do... ;)

Thanx
Charles Brenn

Joerg Nowak
April 29th, 1999, 01:59 PM
I have a program that requires a similar interface and I handle it as follows

First I define a pointer to my document class as a member variable of the view class

class CMyTreeView
{
...
protected:
CMyDocument *m_pDoc;
...
}

Then I do the Following cast

CMyView::OnInitialUpdate()
{
CFormView::OnInitialUpdate(); //placed by class wizard
m_pDoc=STATIC_DOWNCAST(CMyDocument, GetDocument());
...
}

I can then use the document anywhere within the class. I also make the a pointer tp the CMainFrame class a member variable of the document class and allocate the variable in the OnNewDocument() method of the CMyDocument Class with the following cast

class CMyDocument
{
...
CMainFrame *m_pMainFrame;
...
}

CMyDocument::OnNewDocument()
{
...
m_pMainFrame=STATIC_DOWNCAST(CMainFrame, pView->GetParent());
...
}
This has the additional effect of making the CMainFrame class available anywhere the CMyDocument class is available

Hope this helps
Joerg