Starting an app minimized on the system tray 
Author Message
 Starting an app minimized on the system tray

Have a dialog-based application using Shell_NotifyIcon etc to minimize to
and restore from the system tray at runtime.

Question: how to get the app to start on the tray immediately. I have a
::RegisterWindowMessage() entry to send to myself that handles the actions
now ... I'm guessing OnInitInstance() is the place to go - OnInitDialog()
using a PostMessage() works but cause a flicker when the dialog is initially
created/shown. Suggestions?



Tue, 31 Jul 2001 03:00:00 GMT  
 Starting an app minimized on the system tray

Quote:

>Have a dialog-based application using Shell_NotifyIcon etc to minimize to
>and restore from the system tray at runtime.

>Question: how to get the app to start on the tray immediately. I have a
>::RegisterWindowMessage() entry to send to myself that handles the actions
>now ... I'm guessing OnInitInstance() is the place to go - OnInitDialog()
>using a PostMessage() works but cause a flicker when the dialog is
initially
>created/shown. Suggestions?

We are talking about a modeless dialog, yes? If so, remove the WS_VISIBLE
style bit. (In case you don't know modal dialogs must be visible, modeless
dialogs need not be). Then as long as you don't call ShowWindow() the dialog
remains invisible. Goodbye flicker. That's a double edged-sword though. When
you want to show the dialog you must explicitly make it visible.

Regards,
Will



Tue, 31 Jul 2001 03:00:00 GMT  
 Starting an app minimized on the system tray

Quote:

>Question: how to get the app to start on the tray immediately. I have a
>::RegisterWindowMessage() entry to send to myself that handles the actions
>now ... I'm guessing OnInitInstance() is the place to go - OnInitDialog()
>using a PostMessage() works but cause a flicker when the dialog is initially
>created/shown. Suggestions?

As William suggest you need to use a modeless dialog. MFC dialog-based
apps default to using a modal dialog, which causes problems if your
application requires to be invisible. You can make a modeless version
of your app as follows :

1. Make your dialog resource invisible using the styles tab in the
dialog properties.

2. Remove all the DoModal gunk from your InitInstance, so you end up
with something like this :

BOOL CMyApp::InitInstance()
{
   #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;

Quote:
}

3. I place the call to Create (...) in my dialog constructor and make
it public. Other people organise things differently but it works for
me...

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

    Create (IDD, pParent);   // Create modeless dialog.  

Quote:
}

4. 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;

Quote:
}

5. 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. Warch the debug output from your app running in
debug mode - if this is the case MFC will helpfully give you a warning
there.

Now you have a dialog based application that can be shown or hidden as
necessary, and need not be visible ever if that is required.

You can call Shell_NotifyIcon to install a tray icon from your
InitDialog handler, that function gets called whether the dialog is
visible or not.

Bob Moore [MVP]
http://www.mooremvp.freeserve.co.uk
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Due to an unreasonable amount of queries, I no
longer answer unsolicited email questions. Sorry,
no exceptions.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



Tue, 31 Jul 2001 03:00:00 GMT  
 Starting an app minimized on the system tray

?t
I wound up using a hybrid of these ideas that is as follows:

InitInstance()
{

...

    SetRegistryKey( IDS_COMPANY_NAME );    // Use old API to access registry

    CMainSheet *pSheet = new CMainSheet;    // Property sheet is the main app dialog
    m_pMainWnd = pSheet;

    CString strSection, strEntry;
    strSection.Format( IDS_INI_SECTION_STARTUP );
    strEntry.Format( IDS_INI_SYSTEM_TRAY );

// Do we start on the system tray?
    BOOL bStartOnTaskBarAllowed = GetProfileInt ( strSection, strEntry, FALSE );
    DWORD dwStyle =
        ( bStartOnTaskBarAllowed &&    
        GetProfileInt( strSection, strEntry, TRUE) ) ? WS_MINIMIZE : WS_VISIBLE;

// Add a minimize gadget
    dwStyle |= WS_MINIMIZEBOX;

// Kick-off the application
    pSheet->Create( NULL, WS_SYSMENU | WS_POPUP | WS_CAPTION | DS_MODALFRAME | dwStyle );
    return (TRUE);

Quote:
}

I appreciate all the help...


Wed, 01 Aug 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Minimize apps in System tray

2. CoolHide - Help you hide/minimize window to Tray and hide icons from the system tray

3. System Tray App Visible At Start

4. System Tray app and auto start up...

5. minimize to system tray

6. Minimizing a window to the system tray

7. Minimize an application to system tray

8. Minimizing to system tray

9. Minimizing to system tray

10. Minimize into System Tray

11. Minimizing to system tray

12. property sheet based app: minimize to tray?

 

 
Powered by phpBB® Forum Software