How to get Main View from Mainframe

Posted on 2000-02-22
Last Modified: 2013-11-20
I have writtten a n MDI app. Now i want to access my default or first view/window from the Mainframe. I have created many Child window any of which i can access by thier frame pointers but how can i get the frame pointer of the default or first view/window.
Question by:kakamna
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 23

Expert Comment

ID: 2549227
Call CMDIFrameWnd::MDIGetActive() to get the currently active CMDIChildWnd.

Author Comment

ID: 2556944
What i want is that how can i get the main window or frame cordinates so that i can get the co-ordinates of all the child windows with respect to the main window co-ordinate. I want to save the postion of all the child window relative to the main window when the ap is closed and are restores when ever the aplication is being started again.

Accepted Solution

VinExpert earned 100 total points
ID: 2557887

I assume that Ur
App class is CTestApp
main/default view is CTestView

Say U want to access a variable say intVal which is a int from the CTestView in mainframe.

Follow the steps.

1. Open the TestView.cpp. Remove the include headers for the TestView.h and TestDoc.h

2. Open MainFrame.h and include the TestView.h
3. Open the Test.h and include the TestDoc.h
4. Open Test.cpp go to InitInstance(), there U will find the default view's document template. Remove the first line i,e,, remove
CMultiDocTemplate* pDocTemplate;
and add it as the public member in Test.h
5. Open the mainfrm.h and add the following function as public:
BOOL FrameCreateOrActivate(CMDIFrameWnd* pMDIFrameWnd, CDocTemplate* pTemplate,CRuntimeClass* pViewClass);

6. Then open the mainfrm.cpp and add the function as
BOOL CMainFrame::FrameCreateOrActivate(CMDIFrameWnd* pMDIFrameWnd,CDocTemplate* pTemplate,
                                                                                    CRuntimeClass* pViewClass)
      CMDIChildWnd* pMDIActiveWnd = (CMDIChildWnd*)pMDIFrameWnd->MDIGetActive();
      CMDIChildWnd* pNewFrameWnd = NULL;

      if(pMDIActiveWnd != NULL)
            CDocument* pDoc = pMDIActiveWnd->GetActiveDocument();
            ASSERT(pDoc != NULL);
            CView* pView;
            POSITION pos = pDoc->GetFirstViewPosition();

                  pView = pDoc->GetNextView(pos);
                        ((CTestView *)pView)->intVal = 10;                  
                        return TRUE;

            //No views So dont handle anything
      if(pNewFrameWnd == NULL)
            return FALSE;

      return TRUE;

7. Whenever u want to access that variable just call it as
CTestApp *pApp = (CTestApp *)AfxGetApp();
      FrameCreateOrActivate((CMDIFrameWnd*)AfxGetMainWnd(), pApp->pDocTemplate,(RUNTIME_CLASS(CTestView)));

Thats it. Here I have shown U to access only one variable. If U want to access more that one, then just modify the function little bit to handle that. In the similar way u can call the functions too.

Hope this helps.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 2557951

One more simple method.

1. Open the TestView.cpp, Remove the TestView.h and TestDoc.h includes.
2. Open the MainFrm.h and include TestView.h
3. Open the Test.h and include TestDoc.h
4. In the MainFrm.h have the pointer as
CTestView *m_pMainView;
5. In the mainframe's constuctor set it to NULL as
m_pMainView = NULL;
6. Open the TestView.cpp, include MainFrm.h and in one of the initialisation functions say OnDraw put the following line
((CMainFrame *)AfxGetMainWnd())->m_MainView = this;

7. Then U can access all the variables and functions of that view class from the mainframe using m_pMainView pointer.

Try it out.

Author Comment

ID: 2564165
VineExpert is really a expert.

Expert Comment

ID: 2564198

No, No, No such comments plz :-).

(Not VineExpert)

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Include multiple hostnames in this command? 3 56
viewing source code from eclipse 13 111
abstract class with all non abstract mentods 6 87
Problem to event 3 123
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question