2 non-modal CDialogs; getting active Dialog-Window

Environment: Windows NT 3.51, MS Visual C++ 4.0

My problem:

I have an application with MFC:
There can be TWO non-modal Dialogs visible at the screen at the same time.
The menu item "File | Close" should close the actual ACTIVE Window Dialog.

How can I get a pointer/reference to the ACIVE Dialog ???

My current basic approach solution:

void CTestView::OnFileClose() {

    HWND pActiveWnd = (HWND) GetActiveWindow;
    if (pActiveWnd == m_pDialog1)

  class Dialog1 public CDialog {...}
  class Dialog2 public CDialog {...}

  class CTestView public CView {
      Dialog1 m_pDialog1;
      Dialog2 m_pDialog2;

Unfortunately, at this time the main window i.e. the menu window is the active window and so  I don't get the pointer I wish to receive.
Where is a the best palce to get the pointer of the active Dialog ???

I hope for an answer to this problem.

Thanks   Matthias

Who is Participating?
MFCGuyConnect With a Mentor Commented:
Try using:
GetWindow(GW_CHILD) and
GetWindow(GW_NEXT) in a loop

This will loop through all the children of your View using the Z-Order so the window that was last active window should be found first.

CWnd* tmpWnd = GetWindow(GW_CHILD);
while (tmpWnd != NULL)
   if(tmpWnd == m_pDialog1 || tmpWnd == m_pDialog2)

Also, GetActiveWindow does not return an HWND since
you called CWnd::GetActiveWindow() in your code in:
HWND pActiveWnd = (HWND) GetActiveWindow();

Therefore, you tried to compare a CWnd* to an HWND in:
if (pActiveWnd == m_pDialog1)
which wouldn't of worked anyway...

Hope I helped,
All Courses

From novice to tech pro — start learning today.