CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jun 2002
    Posts
    936

    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.

  2. #2
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421
    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

  3. #3
    Join Date
    Jun 2002
    Posts
    936
    I tried to use GetActiveDocument, however I received an error message stated that the function is not defined.

  4. #4
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421
    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

  5. #5
    Join Date
    May 2003
    Location
    Corvallis, OR
    Posts
    315

  6. #6
    Join Date
    Jun 2002
    Posts
    936

    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.

  7. #7
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421
    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

  8. #8
    Join Date
    Feb 2001
    Posts
    2,455
    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

  9. #9
    Join Date
    Jun 2002
    Posts
    936
    There mightbe many ways to do it, but since GetActiveDocument is a virtual function, that was the reason I did it that way.

  10. #10
    Join Date
    Jun 2002
    Posts
    936
    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
  •  





Click Here to Expand Forum to Full Width

Featured