Dialog which can be used as MDI child window

-->

Many people have asked for a dialog that can be used as an MDI child window. So here it is! I hope this will make some people happy :-)

Usage:

1.) Derive a dialog class from CMDIDialog

2.) Create a pointer to an object of the derived class: e.g., CMyMDIDialog* pDlg = new CMyMDIDialog(IDD_MYDIALOG);

3.) Call the DoInsert() function: pDlg->DoInsert()

It's important that a pointer to this dialog be created. Don't destroy the dialog by delete(pDlg). Instead, call the DestroyWindow() function and the object will be destroyed automatically. pDlg->DestroyWindow();

In addition, don't call EndDialog(ID). In order to dismiss the dialog use either the OnCancel or OnOK member functions.

Things that don't work yet: 1.) I couldn't find out how to disable the WS_VISIBLE style. If you set this style in the resource editor and also set the DS_CENTER style the dialog will jump on the screen from the normal position to the centered position. 2.) The CenterWindow() function doesn't center the dialog correctly. Therefore I added my own centering code. 3.) The window title don't appear in the window menu.

If someone fixes these problems or adds more functionality to this code please send me an eMail at: lhornbach@aol.com Regards Martin Horst


// ******************************************************************
// ** CMDIDialog: Dialog which can be used as MDI child 
// ** window
// 
// ** Written 1998 by Martin Horst
// ******************************************************************

//
========================================================================
// == CMDIDialog header file
==============================================
//
========================================================================

#if
!defined(AFX_MDIDIALOG_H__C18B3761_03C6_11D2_92BA_0040053CC5C2__INCLUDED_)
#define AFX_MDIDIALOG_H__C18B3761_03C6_11D2_92BA_0040053CC5C2__INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
// MDIDialog.h : header file
//

////////////////////////////////////////////////////////////////////////////
/
// CMDIDialog dialog

class CMDIDialog : public CDialog
{
// Construction
public:
        CMDIDialog(LPCTSTR lpszTemplateName, CWnd* pParent = NULL);
  CMDIDialog(UINT nIDTemplate, CWnd* pParent = NULL);

  void DoInsert();
  void CenterWindow(CWnd* pAlternateOwner = NULL);
  bool IsWndInserted() { return bInserted; };

// Dialog Data
        //{{AFX_DATA(CMDIDialog)
                // NOTE: the ClassWizard will add data members here
        //}}AFX_DATA


// Overrides
        // ClassWizard generated virtual function overrides
        //{{AFX_VIRTUAL(CMDIDialog)
        //}}AFX_VIRTUAL

// Implementation
protected:

        // Generated message map functions
        //{{AFX_MSG(CMDIDialog)
        virtual void OnCancel();
        virtual void OnOK();
        afx_msg void OnDestroy();
        //}}AFX_MSG
        DECLARE_MESSAGE_MAP()

private:
  bool bInserted;
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations
immediately before the previous line.

#endif //
!defined(AFX_MDIDIALOG_H__C18B3761_03C6_11D2_92BA_0040053CC5C2__INCLUDED_)



//
========================================================================
// == MDIDialog.cpp : implementation file
=================================
//
========================================================================

// MDIDialog.cpp : implementation file
//

#include "stdafx.h"
#include "Unp70.h"
#include "MDIDialog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

////////////////////////////////////////////////////////////////////////////
/
// CMDIDialog dialog

CMDIDialog::CMDIDialog(LPCTSTR lpszTemplateName, CWnd* pParent /*=
NULL*)
        : CDialog(lpszTemplateName, pParent)
{
  bInserted = false;
}

CMDIDialog::CMDIDialog(UINT nIDTemplate, CWnd* pParent /*= NULL*)
        : CDialog(nIDTemplate, pParent)
{
  bInserted = false;
}



BEGIN_MESSAGE_MAP(CMDIDialog, CDialog)
        //{{AFX_MSG_MAP(CMDIDialog)
        ON_WM_DESTROY()
        //}}AFX_MSG_MAP
END_MESSAGE_MAP()

////////////////////////////////////////////////////////////////////////////
/
// CMDIDialog message handlers

void CMDIDialog::OnCancel()
{
  if ( bInserted )
     DestroyWindow();
  else
     CDialog::OnCancel();
}

void CMDIDialog::OnOK()
{
  if ( bInserted )
     DestroyWindow();
  else
     CDialog::OnOK();
}

void CMDIDialog::OnDestroy()
{
  CDialog::OnDestroy();
  if ( bInserted )
     delete(this);
}

void CMDIDialog::DoInsert()
{
CMDIFrameWnd* pWnd = (CMDIFrameWnd*) AfxGetMainWnd();
CWnd* pChildWnd    = FromHandle(pWnd -> m_hWndMDIClient);

  bInserted = true;
  Create(m_lpszTemplateName, NULL);

  SetParent(pChildWnd);

  if ( GetStyle() & DS_CENTER )
     CenterWindow();

  SetFocus();
  ShowWindow(SW_SHOW);
}

void CMDIDialog::CenterWindow(CWnd* pAlternateOwner)
{
CWnd* pWnd = AfxGetMainWnd();
CRect Rect;
int nX, nY;

  if ( bInserted || )
     if ( pAlternateOwner != NULL )
        CDialog::CenterWindow(pAlternateOwner);
     else
        {
        pWnd = AfxGetMainWnd();

        pWnd -> GetClientRect(&Rect);
        nX = Rect.Width() / 2;
        nY = Rect.Height() / 2;

        GetWindowRect(&Rect);
        nX -= Rect.Width() / 2;
        if ( nX < 0 ) nX = 0;
        nY -= Rect.Height() / 2;
        if ( nY < 0 ) nY = 0;

        SetWindowPos(NULL, nX, nY, 0, 0, SWP_NOSIZE);
        }
  else
     CDialog::CenterWindow(pAlternateOwner);
}