Modeless Dialogs
Site Map Feedback

Download:

Up Controls Files Moving Data
If you derive your dialog class from the following tiny helper class instead of CDialog your dialog box will be "Modeless" instead of "Modal"...
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() {try{delete this;}catch(..){}}
};
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 exist, but it won't be visible unless your dialog resource has its Visible flag checked: [Properties][More Styles Tab][Visible].
The code that normally goes in OnInitDlg now goes in the constructor.
To deal with the dialog before you show it, create it, deal with it, then show it.
You can leave Visible unchecked and use 'ShowWindow(SW_SHOW);' in the constructor.
Use 'OnCancel();' to end the dialog.

To stop the Dialog being opened 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;

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.