Convert modal dialogs to modeless

Many postings to CodeGuru and similar discussion groups are questions about modeless dialogs and how to keep them from blocking the parent thread. I use a user interface thread to start a conventional modal dialog that behaves modeless-ly and it's so minimalist and easy I thought others may be interested...

Instructions

  1. In your header file define a CWinThread-derived class...
      class CDialogThread : public CWinThread { DECLARE_DYNCREATE(CDialogThread) CDialogThread() {}; virtual BOOL InitInstance(); };
  2. Put this in your implementation file (where CSomeDialog is a conventional dialog class defined the usual way).
      IMPLEMENT_DYNCREATE(CDialogThread, CWinThread) BOOL CDialogThread::InitInstance() { CSomeDialog dlg; dlg.DoModal(); return FALSE; }
  3. To create an instance of your (now-modeless) modal dialog, do this...
      AfxBeginThread ( RUNTIME_CLASS(CDialogThread) );
  4. You can end the dialog the normal way by calling CDialog::EndDialog(), CDialog::OnCancel() or CDialog::OnOK(). All those special caveats about modeless dialogs (like "don't call EndDialog") no longer apply with this approach.

  5. One point to note is the return of 'FALSE' from the InitInstance() override. This tells MFC that the thread didn't start successfully, so MFC does our cleanup for us! Note that by this time the dialog has already returned and we're through with the thread. This is the same thing MFC-generated code does in the InitInstance() of dialog-based applications.


Comments

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

Top White Papers and Webcasts

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

  • Savvy enterprises are discovering that the cloud holds the power to transform IT processes and support business objectives. IT departments can use the cloud to redefine the continuum of development and operations—a process that is becoming known as DevOps. Download the Executive Brief DevOps: Why IT Operations Managers Should Care About the Cloud—prepared by Frost & Sullivan and sponsored by IBM—to learn how IBM SmartCloud Application services provide a robust platform that streamlines …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds