-
December 9th, 2003, 04:58 PM
#1
What is the Equivalent of AfxGetApp to the Application Document?
I have an SDI document that has several dialogs. I declare a member variable from my app.h. I access this member from any dialog of the application using AfxGetApp.
What I want to do, I want to declare a member variable on my doc.h where I want to access that member from other dialog. Since AfxGetApp return a pointer to the application class and I am working in the document class, I wonder if I can use GetDocTemplate to access the member of the document class from any other dialog as I did from the application class.
-
December 9th, 2003, 07:06 PM
#2
Since you say this is an SDI app, you could use GetActiveDocument()
Good luck.
Be sure to rate those who help!
-------------------------------------------------------------
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
-
December 11th, 2003, 12:34 PM
#3
I tried to use GetActiveDocument, however I received an error message stated that the function is not defined.
-
December 11th, 2003, 12:50 PM
#4
GetActiveDocument is a member of the CFrameWnd class. If you're trying to call it from a View, you would have to use
Code:
GetParent()->GetActiveDocument();
Good luck.
Be sure to rate those who help!
-------------------------------------------------------------
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
-
December 11th, 2003, 01:07 PM
#5
-
December 11th, 2003, 01:12 PM
#6
Still doesn't work, but is seems like I am on the right track
what you suggested to me doesn't work. I order to make it work, I have to do more coding. What I did, I
1. I added up a static member function to my document header file on the form of
Code:
class CMyDocumentClass: public CDocument
{
static CMyDocumentClass *GetDoc();
}
2. I implemented the following function in my doc.cpp
Code:
CMydocumentClass *CMyDocumentClass::GetDoc()
{
CFrameWnd * pFrame = (CFrameWnd *)(AfxGetApp()->m_pMainWnd);
return (CMyDocumentClass *) pFrame->GetActiveDocument();
}
3. I access member variable from doc.h on the form of. I also added doc.h to where I call the function below.
Code:
CMyDocumentClass::GetDoc()->m_MemberVariable;
Question:
Was it necessary to do all this coding? Was there another way to access member variable from the doc.h anywhere from my program?
The problem was, I wanted to access member variable from my doc.h anywhere from my program.
-
December 11th, 2003, 03:50 PM
#7
To answer the question somewhat depends on where you create the dialog from.
In many cases, the handler that creates the dialog will be in your view class (or perhaps the MainFrame). When I create the dialog, I generally add a member variable to the dialog header as:
Code:
CMyDocumentClass* m_pDoc;
If the handler that creates the dialog is in MainFrame (perhaps a menu item that shows the dialog) then I would do this:
Code:
void CMyMainFrm::OnMyButton()
{
CMyDialog dlg;
dlg.m_pDoc = GetActiveDocument();
if (dlg.DoModal() == IDOK)
{
....
}
}
This also allows the dialog (if it creates another dialog or something) to pass the m_pDoc pointer down the chain.
Wherever your code is that creates the dialog should probably already have (or can easily get) the doc pointer and store it as a member variable.
Hope that helps.
Be sure to rate those who help!
-------------------------------------------------------------
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
-
December 11th, 2003, 04:20 PM
#8
I used the following code once before to get the document:
Code:
CWinApp* pApp = ::AfxGetApp();
POSITION pos = pApp->GetFirstDocTemplatePosition();
CDocTemplate* pTmp = pApp->GetNextDocTemplate(pos);
POSITION docPos = pTmp->GetFirstDocPosition();
CMyDocument* pDoc = static_cast<CMyDocument*>(pTmp->GetNextDoc(docPos));
Mike B
-
December 11th, 2003, 04:27 PM
#9
There mightbe many ways to do it, but since GetActiveDocument is a virtual function, that was the reason I did it that way.
-
December 11th, 2003, 04:31 PM
#10
I could have reinstantiate this class
CMyDocumentClass inside my dialog like
CMyDocumentClass *pDocument;
pDocumen->m_MemberFucntionFromDocument;
That might work, but I don't think it would be good. By reinstantiate that class, would I get my data erased from my member variable?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|