Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

To keep track of the MDI child pointers in MDI Application

hello.
   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.

thanx
0
Poojam15
Asked:
Poojam15
  • 2
1 Solution
 
MrNoNameCommented:
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)
protected:
   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)
{
    CMDIChildWnd::OnSetFocus(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
0
 
aphillipsCommented:
> 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;
        else
          ASSERT(0);  // What is this view?
      }
    }
  }
  return NULL;
}
0
 
aphillipsCommented:
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.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now