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
  1. Home
  2. C / C++ / MFC
  3. Getting the pointer to a CView from CMyApp

Getting the pointer to a CView from CMyApp

Scheduled Pinned Locked Moved C / C++ / MFC
3 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.
  • D Offline
    D Offline
    David Wulff
    wrote on last edited by
    #1

    Ok, I have two classes, eg, CView1 and CView2. I want to call a function in one view to get some text, then call a function in the second view to set the text to what I just got. Following so far? I need to do this in a function in my CWinApp derived class. The program is set up to be MDI, and both views are attached to the same CMDIChildWnd frame, switched between with tabs. I am need to do this so I can update one document based on the other when the views are switched. For example, when the tab is clicked to switch the view to view1 from view2, I call "theApp.SetVew1_from_View2()", which needs to be like: void CMyApp::SetVew1_from_View2() { CString strView2 = CView2::GetText(); CView1::SetText(strView2); } Except that I obviously need to get valid pointers to each view. This is my question (finally:)), how do I get pointers to each view (attached to the same child frame)? I have tried getting the CMDIFrameWnd, then the active child frame, but I can't get pointers to each view from here. Any light you could shed on this would be greatly appreciated. Thanks in advance, David Wulff Battleaxe Software

    B 1 Reply Last reply
    0
    • D David Wulff

      Ok, I have two classes, eg, CView1 and CView2. I want to call a function in one view to get some text, then call a function in the second view to set the text to what I just got. Following so far? I need to do this in a function in my CWinApp derived class. The program is set up to be MDI, and both views are attached to the same CMDIChildWnd frame, switched between with tabs. I am need to do this so I can update one document based on the other when the views are switched. For example, when the tab is clicked to switch the view to view1 from view2, I call "theApp.SetVew1_from_View2()", which needs to be like: void CMyApp::SetVew1_from_View2() { CString strView2 = CView2::GetText(); CView1::SetText(strView2); } Except that I obviously need to get valid pointers to each view. This is my question (finally:)), how do I get pointers to each view (attached to the same child frame)? I have tried getting the CMDIFrameWnd, then the active child frame, but I can't get pointers to each view from here. Any light you could shed on this would be greatly appreciated. Thanks in advance, David Wulff Battleaxe Software

      B Offline
      B Offline
      Brian Hart
      wrote on last edited by
      #2

      An approach is, instead of the app getting pointers to the views, how about the views giving the app pointers back to themselves? Add two public member variables to CMyApp, and #include lines, like this: #include "View1.h" #include "View2.h" class CMyApp : public CWinApp { ... public: CView1* m_pView1; CView2* m_pView2; } // IN MYAPP.CPP CMyApp::CMyApp() { // Initialize view pointers to NULL m_pView1 = NULL; m_pView2 = NULL; } void CMyApp::SetView1FromView2() { if (m_pView1 == NULL || m_pView2 == NULL) return; m_pView1->SetText(m_pView2->GetText()); } // NOW IN View1.cpp CView1::~CView1() { theApp.m_pView1 = NULL; } void CView1::OnInitialUpdate() { CView::OnInitialUpdate(); ... theApp.m_pView1 = this; } // AND IN View2.cpp CView2::~CView2() { theApp.m_pView2 = NULL; } ... void CView2::OnInitialUpdate() { CView::OnInitialUpdate(); ... theApp.m_pView2 = this; } And voila! :) Cheers, Brian

      D 1 Reply Last reply
      0
      • B Brian Hart

        An approach is, instead of the app getting pointers to the views, how about the views giving the app pointers back to themselves? Add two public member variables to CMyApp, and #include lines, like this: #include "View1.h" #include "View2.h" class CMyApp : public CWinApp { ... public: CView1* m_pView1; CView2* m_pView2; } // IN MYAPP.CPP CMyApp::CMyApp() { // Initialize view pointers to NULL m_pView1 = NULL; m_pView2 = NULL; } void CMyApp::SetView1FromView2() { if (m_pView1 == NULL || m_pView2 == NULL) return; m_pView1->SetText(m_pView2->GetText()); } // NOW IN View1.cpp CView1::~CView1() { theApp.m_pView1 = NULL; } void CView1::OnInitialUpdate() { CView::OnInitialUpdate(); ... theApp.m_pView1 = this; } // AND IN View2.cpp CView2::~CView2() { theApp.m_pView2 = NULL; } ... void CView2::OnInitialUpdate() { CView::OnInitialUpdate(); ... theApp.m_pView2 = this; } And voila! :) Cheers, Brian

        D Offline
        D Offline
        David Wulff
        wrote on last edited by
        #3

        Thanks. I'll play around with this idea, but just one question: If I have more than child frame open, wont the pointer 'point' to the most recently created one. Actually forget this, I have just thought of adding the 'theApp.m_pViewX = this;' each time the child frame is selected. After all, only one can be shown at a time. Thanks again, David

        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