Virtual Developer Workshop: Containerized Development with Docker


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 :-)


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

#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
        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
                // NOTE: the ClassWizard will add data members here

// Overrides
        // ClassWizard generated virtual function overrides

// Implementation

        // Generated message map functions
        virtual void OnCancel();
        virtual void OnOK();
        afx_msg void OnDestroy();

  bool bInserted;

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

#endif //

// == 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__;

// CMDIDialog dialog

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

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


// CMDIDialog message handlers

void CMDIDialog::OnCancel()
  if ( bInserted )

void CMDIDialog::OnOK()
  if ( bInserted )

void CMDIDialog::OnDestroy()
  if ( bInserted )

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

  bInserted = true;
  Create(m_lpszTemplateName, NULL);


  if ( GetStyle() & DS_CENTER )


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

  if ( bInserted || )
     if ( pAlternateOwner != NULL )
        pWnd = AfxGetMainWnd();

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

        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);


  • Software Developer

    Posted by linliu1 on 02/03/2014 04:57am

    I do not know why people think it is no easy to use MDI with wpf. I used the LinsUIWPF Suite. It is so easy to create a MDI window. And even better, the user can switch from MDI to TDI during the run time. And it even support both Metro style and traditional windows style. Users can switch between these syle during run time too.

  • How can I do this in C language?

    Posted by Legacy on 11/14/1999 08:00am

    Originally posted by: Rafa Alonso

    Someone can explain me how can I do this in c language.

  • CWnd::GetDlgItem() returns temporary pointer

    Posted by Legacy on 02/16/1999 08:00am

    Originally posted by: Stephen K. Terry

    Your CMDIDialog has been a big help in converting my dialog based application to a MDIFrame application.
    However, I am having a couple of problems. 

    Most notably, I have some member variables that contain window handles. These handles are initialized in OnInitDialog() using GetDlgItem(IDC_xxxx). Unfortunately, the handles become invalid sometime after then I try and process a message. The code works fine when I open the dialog with .DoModal(). However, when I use DoInsert() to create an MDI child window, the window handles are invalid.

    In the MSDN documentation, Microsoft states: "The returned pointer may be temporary and should not be stored for later use.".

    This means that I will have to make some additional modifications.

  • How to create an MDI child window in a dll?

    Posted by Legacy on 02/09/1999 08:00am

    Originally posted by: Dmitry Babitsky

    I want to be able to dynamically extend my application at run time
    by allowing MDI child windows to be loaded from a Dll.
    Pre-conditions: the main application does not know what Child windows
    are available until the run time. The functionality should be similar
    to COM Component Categories.

    To complicate the matter even further the main application may not be
    an MFC app.

    Is that possible and if so does anybody have any ideas?


  • Hit "Enter" Key Will Colose the Frame.

    Posted by Legacy on 01/04/1999 08:00am

    Originally posted by: Wenfei Wu

    It's a very interesting idea. However, when I hit "Enter" key, the frame closed. Could you tell me how to fix it.


  • An easier way...

    Posted by Legacy on 11/21/1998 08:00am

    Originally posted by: Rossetti Gianluca

    There is no need to define any kind of new classes if you want to 
    use a dialog as a Child MDI Window, because the MDI Class CFormView
    do this for you.

    The first thing to do is create a new dialog not visible and with the
    style CHILD. Using class wizard you should create a new class derived
    from CFormView. Let me call this class CMyFormView.

    As second thing define a new Document template in this way:

    CMultiDocTemplate* pMyDocTemplate;
    pMyDocTemplate = new CMultiDocTemplate(
    CFrameWnd* MyFrame = CreateNewFrame( "pointer to your document or NULL",
    "pointer to yur frame or NULL").

    Some remarks:
    1) If you are working with some your Document and View classes, use them
    instead of classes CDocument, CMDIChildFrame;
    2) If you're not working with a document-View architecture, use NULL
    when you call CreateNewFrame();
    3) If you're not interested to create a new document template, don't
    call AddDocTemplate(). In this way you can call only one view and the
    dialog is fully simulated.

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date