A: How to start dialog-based app hidden 
Author Message
 A: How to start dialog-based app hidden

Q: How do I start a dialog-based application hidden?

Standard desclaimer: This is one of many solutions, and other
solutions might be better/more efficient. If it works, that's because
I wrote it. If not, er... I don't know whose shoddy job it is.

A: If you make a dialog-based application using AppWizard, it does
something like the following.

  BOOL CNonameApp::InitInstance()
    CNonameDlg dlg;
    m_pMainWnd = &dlg;
    int nResponse = dlg.DoModal();
    return FALSE;

Basically it displays a dialog using DoModal and return FALSE. If
CWinApp::InitInstance() returns FALSE, the application exits. When you
close the dialog, you are done with the application, so this is
exactly what you want.

Displaying a modal dialog hidden is somewhat tricky. You could move
the window to a far off the screen (e.g. (5000, 5000)), but that poses
another problem: What if the dialog has an icon _and_ you like the
icon so much that you want to keep it? It still shows up when you
press ALT+TAB. Playing with ShowWindow(SW_HIDE) may or may not work,
and there is a chance it might show up a fraction of a second and then
disappear. Simply turning off "visible" check box from the dialog's
style won't do the job either.

The following method shows you how solve the problem by making the
dialog modeless.

1) Use AppWizard to create a new dialog-based application.

2) Modify InitInstance().

  BOOL CNonameApp::InitInstance()
    // comment out DoModal() and co.

    m_pMainWnd = new CNonameDlg;

    return TRUE;  // go on with the application

3) Modify the dialog's constructor

  // comment out arguments to CDialog's constructor
  CNonameDlg::CNonameDlg(CWnd* pParent /*=NULL*/)
    : CDialog(/*CNonameDlg::IDD, pParent*/),
    // create dialog here

4) Override CDialog::OnCancel()

  void CNoname::OnCancel()

5) Override CDialog::PostNcDestroy()

  void CFreeResourceDlg::PostNcDestroy()
    delete this;

    // don't call base class's function

6) Turn off "visible" checkbox from the dialog's "More Styles" tab.

Since there is no window showing, it is a good idea to put an icon on
the notify area by ::Shell_NotifyIcon(). For example, you could bring
up the dialog when the user clicks on the notify icon.

  LRESULT CNonameDlg::WindowProc(UINT message,
    WPARAM wParam, LPARAM lParam)
    switch (message)
      // #define WM_NOTIFYICON WM_USER+7 (7 for good luck)
      case WM_NOTIFYICON:  
        if ((UINT)wParam == IDC_NOTIFY_ICON)
          if ((UINT)lParam == WM_LBUTTONDOWN)

    return CDialog::WindowProc(message, wParam, lParam);

While you are at it, check out the following article.

  PRB: Menus for Notification Icons Don't Work Correctly
  Article ID: Q135788

Sat, 26 May 2001 03:00:00 GMT  
 [ 1 post ] 

 Relevant Pages 

1. HOWTO: hide dialog when a dialog-based app starts

2. How to Start Dialog App Hidden?

3. Can I start a dialog app initially hidden?

4. Q:How do I start a Dialog App in a HIDE state

5. hiding an mfc dialog based app

6. How to hide a messagebox before it pops up, in a dialog based app

7. Hide dialog based app on startup

8. Hiding a dialog-based app

9. How can I tell when a dialog based app gets hidden or exposed

10. Hide dialog based apps

11. Hiding a dialog based app on startup

12. Help please - Dialog-based app won't start - Acc Vio C0000005 - VC 5 - Win200


Powered by phpBB® Forum Software