To keep track of the MDI child pointers in MDI Application

Posted on 2003-03-26
Medium Priority
Last Modified: 2013-11-20
   I am designing an MDI application in which on selecing 'new' from the menu ..I create a new MDi child and store its pointer in my parentMDI class object(derived from MDIframeWnd),now the problem is if i click on new second time the earlier child pointer gets replaced by the new child created in parent object.How can I keep track of the all child windows created.so that whenever a child window is selected ,it becomes the active window with its pointer in the parent wnd object.

Question by:Poojam15
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
  • 2

Expert Comment

ID: 8214754
Hi Poojam,

Assuming you used appwizard to generate the basic shell,
Here's how you do it:

// include the child frame header file in your MainFrm.h
   #include "ChildFrm.h"

// Add a CChildFrame member to the same file (MainFrm.h)
   CChildFrame *m_pActiveChild;

// create a member function belonging to your CMainFrame
// like this
void CMainFrame::SetActiveChild(CChildFrame *pChild)
     m_pActiveChild = pChild;

// Now, in your ChildFrm.cpp file, include the main frame
   #include "MainFrm.h"
// Since you are using appwizard, WM_SETFOCUS should
// already be overridden, if not do it
void CChildFrame::OnSetFocus(CWnd* pOldWnd)
    // get a pointer to your main frame
    CMainFrame *pFrame = (CMainFrame*) AfxGetMainWnd ();
    // set your main frame's child object like this
    pFrame->SetActiveChild (this);

Now, whenever one of your child window receives focus, the main frame's m_pActiveChild will point to it

Expert Comment

ID: 8214804
> How can I keep track of the all child windows created.

MFC does that anyway.  I recommend you just access their data.

> so that whenever a child window is selected ,it becomes the active window ...

When a window is selected it automatically becomes the active window.

Getting the active view with MFC is not easy but here is a global function to use.  It assumes the app only has one type of view (called CMyView here).

CMyView *GetView()
  CMainFrame *mm = (CMainFrame *)AfxGetMainWnd();

  if (mm != NULL)
    CMDIChildWnd *pwind = mm->MDIGetActive();

    // Ignore if there are no views open
    if (pwind != NULL)
      CView *pv = pwind->GetActiveView();
      if (pv != NULL) // May be NULL if in print preview?
        if (pv->IsKindOf(RUNTIME_CLASS(CMyView)))
          return (CMyView *)pv;
          ASSERT(0);  // What is this view?
  return NULL;

Accepted Solution

aphillips earned 120 total points
ID: 8222968
Sorry I was thinking of views.  To get the active child window just call MDIGetActive.  I still don't think you should be storing these pointers when MFC can give them back to you.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

770 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