Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Getting correct ChildFrame ptr in xxxDoc

Getting correct ChildFrame ptr in xxxDoc

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    EiSl
    wrote on last edited by
    #1

    Hello all, I really have the feeling that this is a stupid question, but for some reason I do not see the answer :-O Problem (Multi Doc Interface project): When creating a new document (OnNewDocument), I also need to have access to its ChildFrame object to set some data member (via public method). I used next code, but its unfortunaltely faulty. BOOL MyAppDoc::OnNewDocument() {   if (!CDocument::OnNewDocument())      return FALSE;   ...   CMainFrame *pMainFrame = ( CMainFrame* )AfxGetMainWnd();   ASSERT( pMainFrame );   CChildFrame *pChild = (CChildFrame *) pMainFrame->GetActiveFrame();   // Now using pChild to access a public methode which sets a datamember-> crash   ... } Where I'm going the wrong way? More background info: In my ChildFrame (not MainFrame) I've defined a statusbar, which should be updated with info I've available at the moment of creation doc (or view). Thanks in advance, EiSl

    A 1 Reply Last reply
    0
    • E EiSl

      Hello all, I really have the feeling that this is a stupid question, but for some reason I do not see the answer :-O Problem (Multi Doc Interface project): When creating a new document (OnNewDocument), I also need to have access to its ChildFrame object to set some data member (via public method). I used next code, but its unfortunaltely faulty. BOOL MyAppDoc::OnNewDocument() {   if (!CDocument::OnNewDocument())      return FALSE;   ...   CMainFrame *pMainFrame = ( CMainFrame* )AfxGetMainWnd();   ASSERT( pMainFrame );   CChildFrame *pChild = (CChildFrame *) pMainFrame->GetActiveFrame();   // Now using pChild to access a public methode which sets a datamember-> crash   ... } Where I'm going the wrong way? More background info: In my ChildFrame (not MainFrame) I've defined a statusbar, which should be updated with info I've available at the moment of creation doc (or view). Thanks in advance, EiSl

      A Offline
      A Offline
      Alois Kraus
      wrote on last edited by
      #2

      Perhaps You forgot to call the base class: BOOL CMyDoc::OnNewDocument() { if (!CDocument::OnNewDocument()) return FALSE; // Do initialization of new document here. return TRUE; } The MSDN says that this is called as a part during the File New Command. You can't be sure that the window already has been completely created. I did always keep a list of doc/view ptr in min App Object. It worked very well. class CMyApp { public: vector<CView *> m_ActiveViews; vector<CDocument *> m_ActiveDocs; } I always filled both in the members in void CMyView::OnInitialUpdate( ) { CView::OnOnInitialUpdate(); CMyApp *pMyApp = (CMyApp *) AfxGetApp(); if( pMyApp ) { pMyApp->m_ActiveViews.push_back(this); pMyApp->m_ActiveDocs.push_back(GetDocument()); } } where i could be sure that the window was created and the document ptr already existing. This would be a better place to change the frame window. Keep Your list up to date and all will work fine. CFrameWnd::InitialUpdateFrame( CDocument* pDoc, BOOL bMakeVisible ); would be the best place to change the frame window behaviour

      E 1 Reply Last reply
      0
      • A Alois Kraus

        Perhaps You forgot to call the base class: BOOL CMyDoc::OnNewDocument() { if (!CDocument::OnNewDocument()) return FALSE; // Do initialization of new document here. return TRUE; } The MSDN says that this is called as a part during the File New Command. You can't be sure that the window already has been completely created. I did always keep a list of doc/view ptr in min App Object. It worked very well. class CMyApp { public: vector<CView *> m_ActiveViews; vector<CDocument *> m_ActiveDocs; } I always filled both in the members in void CMyView::OnInitialUpdate( ) { CView::OnOnInitialUpdate(); CMyApp *pMyApp = (CMyApp *) AfxGetApp(); if( pMyApp ) { pMyApp->m_ActiveViews.push_back(this); pMyApp->m_ActiveDocs.push_back(GetDocument()); } } where i could be sure that the window was created and the document ptr already existing. This would be a better place to change the frame window. Keep Your list up to date and all will work fine. CFrameWnd::InitialUpdateFrame( CDocument* pDoc, BOOL bMakeVisible ); would be the best place to change the frame window behaviour

        E Offline
        E Offline
        EiSl
        wrote on last edited by
        #3

        Thanks for reply Alois, I've updated my example since I'm indeed first calling     if (!CDocument::OnNewDocument())       return FALSE; after I'm continueing with the rest of my stuff. More background info: In my ChildFrame (not mainframe) I've defined a statusbar (at bottom). This one should be updated with information I've available at the 'OnNewDocument' (so the Doc) or in the View. That's the reason I need a pointer to ChildFrame to be able access my CStatusBar member. Thanks, EiSl

        A 1 Reply Last reply
        0
        • E EiSl

          Thanks for reply Alois, I've updated my example since I'm indeed first calling     if (!CDocument::OnNewDocument())       return FALSE; after I'm continueing with the rest of my stuff. More background info: In my ChildFrame (not mainframe) I've defined a statusbar (at bottom). This one should be updated with information I've available at the 'OnNewDocument' (so the Doc) or in the View. That's the reason I need a pointer to ChildFrame to be able access my CStatusBar member. Thanks, EiSl

          A Offline
          A Offline
          Alois Kraus
          wrote on last edited by
          #4

          Did you try the opposite direction ? Overide CFrameWnd::InitialUpdateFrame() and get a ptr to your new created document in this function. There You can safely update your status bar because you can be sure that the frame window already exists. I think the problem is that when OnNewDocument is called only the CDocument is created. The View does not yet exist. The order is if I remember right first the document then the view with the frame is created. You are trying to update a frame which does not yet exist.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups