If we have an existing project one needs to do the following steps to add an non-modal dialog.

Example Project:

  ModelessDialog.zip (45,5 KiB)
  1. Add a new dialog to the project and add a class for that dialog
  2. Add a member variable for the dialog to the application class.
    If our project is called ‘SecondDialog’ the application class would then look like this
    class CSecondDialogApp : public CWinApp
    {
    public:
    	CSecondDialogApp();
     
    	DlgSecond* m_pDlgSecond;
     
    // Überschreibungen
    	public:
    	virtual BOOL InitInstance();
     
    // Implementierung
     
    	DECLARE_MESSAGE_MAP()
    };
     
    extern CSecondDialogApp theApp;

    with m_pDlgSecond the member variable of the new dialog.

    Also add the header file with #include "DlgSecond.h" to the same file.

  3. Add an Button (or Menu entry) to the project which shall show the dialog
    This function should then have the following content to open the dialog when the button is clicked
    void CSecondDialogDlg::OnBnClickedButtonShowSecondDialog()
    {
     
    	if (theApp.m_pDlgSecond != NULL) {
    		theApp.m_pDlgSecond->SetForegroundWindow();
    		theApp.m_pDlgSecond->SetFocus();
    	}
    	else {		
    		theApp.m_pDlgSecond = new DlgSecond;
    		theApp.m_pDlgSecond->Create(DlgSecond::IDD);				
    		// Ausgabe initialisieren		
    		theApp.m_pDlgSecond->ShowWindow(SW_SHOW);	
    	}
     
    }
  4. Make sure the variable is deleted
    CSecondDialogDlg::~CSecondDialogDlg()
    {
    	if (theApp.m_pDlgSecond != NULL) 
    	{
    		delete [] theApp.m_pDlgSecond;
    	}
    }