Getting the pointer to a CView from CMyApp
-
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
-
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
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
-
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
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