How do I make an invisible dialog-based application?

MFC dialog-based apps default to using a modal dialog, which causes problems if your application requires to be invisible. This situation most commonly occurs when developing applications which place icons in the system tray, and therefore need their main dialog to be invisible until the user interacts with the icon. You can make a modeless dialog version of your app as follows:

Step 1 - hiding the resource

Make your dialog resource invisible using the dialog properties.

Step 2 - removing the modal stuff

Remove all the DoModal gunk from your app's InitInstance, so you end up with something like this:

BOOL  CMyApp::InitInstance()
{
   // Only needed for older compilers
   #ifdef _AFXDLL
      Enable3dControls(); // using MFC in a shared DLL
   #else
      Enable3dControlsStatic(); // linking to MFC statically
   #endif

   m_pMainWnd = new CMyModelessDlg;
    
   if (m_pMainWnd)
      return TRUE;
   else
      return FALSE;
 }

Step 3 - creating

We need to create the dialog. I place the call to Create (...) in my dialog constructor and make it public. Other people organise things differently but it works for me... curse the OOP thought police <g>

CMyModelessDlg::CMyModelessDlg (CWnd* pParent /*=NULL*/)
   : CDialog(CMyModelessDlg::IDD, pParent)
{
   //{{AFX_DATA_INIT(CMyModelessDlg)
   //}}AFX_DATA_INIT
   m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
   Create (IDD, pParent); // Create modeless dialog. 
}

Step 4 - cleaning up

You will need to handle PostNcDestroy and delete the dialog object yourself (ClassWizard can set up the handler for you) :

void CMyModelessDlg::PostNcDestroy() 
{
   delete this;
}

You may find that if you call DestroyWindow from within your dialog in order to shut down, you will have to call your own PostNcDestroy in order to avoid a leak. Watch the debug output from your app running in debug mode - if this is the case MFC will helpfully give you a warning there. Here's the normal WM_CLOSE handler:

void CMyModelessDlg::OnClose()
{
   if (m_bOkToClose)
   {
      CleanUp ();
      DestroyWindow ();
   }
   else
   {
      ShowWindow (SW_HIDE);
   }
}

I use the m_bOkToClose flag to distinguish when I'm permitting the user to close down and when I want that action to simply hide the window. Cleanup is a function that does any necessary CloseHandles, removes any tray icons and deletes any allocated memory.

Now you have a dialog based application that can be shown or hidden as necessary.

Download