// Modeless.h : definition of CModelessDialog class #if _MSC_VER >= 1000 #pragma once #endif // _MSC_VER >= 1000 #ifndef Modelessh #define Modelessh #ifndef __AFXWIN_H__ #error include 'afxwin.h' before including this file! #endif /* CModelessDialog To use this, create a normal Dialog class. Replace all occurances of the text 'CDialog' with 'CModelessDialog' in the .cpp and the .h files. The dialog only needs to be created to be around, but it won't be visible unless your dialog resource has its Visible flag checked. Setting the dialog as visible doesn't always work. If it doesn't, leave visible unchecked and use 'ShowWindow(SW_SHOW);' in the constructor. Use 'OnCancel();' to end the dialog. To deal with the dialog before you show it, create it, deal with it, then show it. The code that normally goes in OnInitDlg now goes in the constructor. To stop the dialog being open twice, use the following code: //In the Header file: static CMyDlg* Active; //In the .cpp file: CMyDlg* CMyDlg::Active=NULL; //At the beginning of the constructor code MyDlg::MyDlg(: if(Active==NULL) Active=this; else { Active->SetActiveWindow(); OnCancel(); return; } ShowWindow(SW_SHOW); SetActiveWindow(); //At the beginning of the Destructor code MyDlg::~MyDlg(): if(Active==this) Active=NULL; */ class CModelessDialog : public CDialog { public: CModelessDialog( CWnd* /*pParent*/=NULL) {} CModelessDialog(UINT nIDTemplate, CWnd* pParent =NULL) {Create(nIDTemplate , pParent);} CModelessDialog(LPCSTR lpszTemplateName, CWnd* pParent =NULL) {Create(lpszTemplateName, pParent);} protected: virtual void OnCancel() {DestroyWindow();} virtual void OnOK() {if(UpdateData(TRUE)) DestroyWindow();} virtual void PostNcDestroy() {delete this;} }; #endif //ndef Modelessh