Share via

Calling View inside view in MFC

ashish khowal 6 Reputation points
2022-09-13T07:55:11.837+00:00

Hi Experts,

I am having one MFC App class with multidoctemplate initialized (AView+ADoc+CFrame) and (BView+BDoc+CFrame). Now in the ondraw() method of AView class , I want to initialize/call BView class such that from the onDraw() of AView can call onopendocument of BDoc class and then oninitialupdate and ondraw() of BView class. Can someone suggest if it is possible and if yes then how to do it.

Ideally this flow should be done automatic but due to the nature of the structure of the code i have to do manual calling of onOpenDocument of BDoc , but it was failing in ondraw of BView class. Can someone suggest how to approach it correctly?

Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.


2 answers

Sort by: Most helpful
  1. Guido Franzke 2,191 Reputation points
    2022-09-13T08:44:21.537+00:00

    And if you want to call OnOpenDocumentFile from AView, you must save the CMultiDocTemplate pointer to a member of your CWinApp class.
    Like this:

    1. class CMyApp : public CWinAppEx
      {
      public:
      CMultiDocTemplate* m_pDocTemplateB;

    }

    1. in InitInstance save the pointer:
      m_pDocTemplateB = new CMultiDocTemplate(IDR_xxxTYPE,
      RUNTIME_CLASS(BDoc),
      RUNTIME_CLASS(CFrame),
      RUNTIME_CLASS(BView));
      if (!m_pDocTemplateB) return FALSE;
      AddDocTemplate(m_pDocTemplateB);
    2. Whereever you want to create a new BView client window:
      ((CMyApp*)::AfxGetApp())->m_pDocTemplateB->OpenDocumentFile(NULL);

    Regards, Guido

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments

  2. Guido Franzke 2,191 Reputation points
    2022-09-13T08:29:00.193+00:00

    Hello,
    you must search for the client view window of BView, like this:

     for (CMDIChildWnd* pChild = MDIGetActive(); pChild != NULL; pChild = (CMDIChildWnd*)pChild->GetWindow(GW_HWNDNEXT))  
     {  
       CRuntimeClass* prr = pChild->GetRuntimeClass(); if (!prr) continue;  
       if (CString(prr->m_lpszClassName) != L"CFrame") continue;  
       CDocument* ppp = pChild->GetActiveDocument(); if (!ppp) continue;  
       prr = ppp->GetRuntimeClass(); if (!prr) continue;  
       if (CString(prr->m_lpszClassName) == L"BDoc")  
       {  
         BDoc* pdoc = (BDoc*)ppp;  
         pChild->MDIActivate();  
         pChild->BringWindowToTop();  
         POSITION position = pdoc->GetFirstViewPosition();  
         if (position != NULL)  
         {  
           BView* pView = (BView*)pdoc->GetNextView(position);  
           if (pView != NULL) pView->CallYourFunction();  
         }  
         break;  
       }  
    }  
      
    

    Regards, Guido

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.