Getting document pointer
-
I have made a sdi application in MFC. I added a menu item, and a routine when it is clicked. That routine creates an instance of, and displays, an information dialog I created. What I need to do though is display some information in that dialog from the loaded in document. The biggest problem I always seem to have is finding out how to access the document in another class like my dialog class. So I am wondering what I need to do to get a document pointer to my dialog class. Either by passing it in from MainFrm when I create the dialog, or better yet, have my dialog class itself get a pointer to the document. Anyone have any ideas or code samples? I notice my view class has something like: CDXtoXDDoc* CDXtoXDView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDXtoXDDoc))); return (CDXtoXDDoc*)m_pDocument; }
-
I have made a sdi application in MFC. I added a menu item, and a routine when it is clicked. That routine creates an instance of, and displays, an information dialog I created. What I need to do though is display some information in that dialog from the loaded in document. The biggest problem I always seem to have is finding out how to access the document in another class like my dialog class. So I am wondering what I need to do to get a document pointer to my dialog class. Either by passing it in from MainFrm when I create the dialog, or better yet, have my dialog class itself get a pointer to the document. Anyone have any ideas or code samples? I notice my view class has something like: CDXtoXDDoc* CDXtoXDView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDXtoXDDoc))); return (CDXtoXDDoc*)m_pDocument; }
yes, i supose that you have defined the message handler of this menu item in the view class. I supose too that you call de DoModal() function of the dialog class in this message handler. Therefore your´s dialog parent window is the view window. If you call the dialogs method: CWnd * GetParentWnd() and cast teh result to a (CDXtoXDView *). Just doing this: (CDXtoXDView *)GetParentWnd(), then you have a pointer to the view object. You just have to call GetDocument() view´s method to obtain the document pointer. This should work.;)
-
I have made a sdi application in MFC. I added a menu item, and a routine when it is clicked. That routine creates an instance of, and displays, an information dialog I created. What I need to do though is display some information in that dialog from the loaded in document. The biggest problem I always seem to have is finding out how to access the document in another class like my dialog class. So I am wondering what I need to do to get a document pointer to my dialog class. Either by passing it in from MainFrm when I create the dialog, or better yet, have my dialog class itself get a pointer to the document. Anyone have any ideas or code samples? I notice my view class has something like: CDXtoXDDoc* CDXtoXDView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDXtoXDDoc))); return (CDXtoXDDoc*)m_pDocument; }
If the menu's handler function is in the view, try:
CDXtoXDView::OnMenuItem()
{
CDXtoXDDoc *pDoc = GetDocument();
CMyDialog dlg(pDoc->somedata);dlg.DoModal();
}
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
yes, i supose that you have defined the message handler of this menu item in the view class. I supose too that you call de DoModal() function of the dialog class in this message handler. Therefore your´s dialog parent window is the view window. If you call the dialogs method: CWnd * GetParentWnd() and cast teh result to a (CDXtoXDView *). Just doing this: (CDXtoXDView *)GetParentWnd(), then you have a pointer to the view object. You just have to call GetDocument() view´s method to obtain the document pointer. This should work.;)
I'll give the suggestions a try. Thank you. When I use the classwizard to create the onclick event, it put it in MainFrame: void CMainFrame::OnToolsShpproperties() { CDLGShpFileBreakdown dlgSHPBreakdown; dlgSHPBreakdown.DoModal(); } Your code will still work?
-
I'll give the suggestions a try. Thank you. When I use the classwizard to create the onclick event, it put it in MainFrame: void CMainFrame::OnToolsShpproperties() { CDLGShpFileBreakdown dlgSHPBreakdown; dlgSHPBreakdown.DoModal(); } Your code will still work?
no, it won´t work for sure. All is about getting the current view when creating the dialog. If you have the view, you have the doc with GetDocument(). From the dialog, you can get a pointer to the main frame with the method: GetParentFrame( ). Once you have the pointer to the frame, you can call its GetActiveView() method to get the current view. Thats all, i hope i have been usefull to you.
-
no, it won´t work for sure. All is about getting the current view when creating the dialog. If you have the view, you have the doc with GetDocument(). From the dialog, you can get a pointer to the main frame with the method: GetParentFrame( ). Once you have the pointer to the frame, you can call its GetActiveView() method to get the current view. Thats all, i hope i have been usefull to you.
It's fine. I figured it out. In the past when I have added a command handler for a menu item, I have always just used the default in the drop down which is mainfrm. I removed that one, and selected my dialog in the list this time. It put the menu command handler in my document class this time, and all the data I want to display is in that class, so easy solution. :) Thanks for all the help.