CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Dec 2001
    Posts
    199

    Question Dialog Based System Tray App

    Hi Gurus,

    I have found an article in the Codeguru articles titled "SDI/MDI MFC Application in Windows System Tray", which explains to hide an App you simply call ShowWindow(SW_HIDE) in the application's InitInstance.

    My problem is I am using VC6 with a Dialog based application and want it to appear when run in the system Tray and only show up when the icon in taskbar is clicked. In a MFC Dialog based project the dialog is created with a DoModal call hence I can not call the ShowWindow(SW_HIDE) here, if I call it in the InitDialog, it does not work either.

    I need to application to hide on startup, what am I missing?

    Thanks in advance.

    KnNeeded

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Dialog Based System Tray App

    Post this code in OnInitDialog():

    Code:
    PostMessage(WM_SYSCOMMAND, SC_MINIMIZE);
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Dec 2001
    Posts
    199

    Question Re: Dialog Based System Tray App

    Thanks for the suggestion, I put the suggested code in the InitDialog function unfortunately it did not work.

    I put the ShowWindow(SW_HIDE) behind a button and it worked fine, I went into the MSDN documentation and it says:

    The first time an application calls ShowWindow, it should use the WinMain functions nCmdShow parameter as it's nCmdShow parameter. Subsequent calls to ShowWindow must use one of the values in the given list, instead of the one specifed by the WinMain function's nCmdShow parameter.
    Therefore I cannot see a way to get it to hide a Dialog based app automatically when it starts, maybe I will have to think of a way of getting an SDI to look like a dialog app to solve this?

    KnNeeded

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Dialog Based System Tray App

    Quote Originally Posted by KnNeeded
    Thanks for the suggestion, I put the suggested code in the InitDialog function unfortunately it did not work.
    What do you mean? It works for me...
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Dec 2001
    Posts
    199

    Re: Dialog Based System Tray App

    hmmmm.... I can get it to minimise, but not hide

    I have created a blank MFC AppWizard Dialog based project in VC6 and simply put in this code in the OnInitDialog:
    Code:
    PostMessage(WM_SYSCOMMAND, SC_MINIMIZE);
    ShowWindow(SW_HIDE);
    It still refuses to hide when it starts, yes it minimises but I need to have it not appear in the Taskbar as the access will soley be through the system tray icon. ShowWindow(SW_HIDE) seems to only hide the dialog properly after initial dialog, say behind a button press event. Any Ideas?

    By the way thanks for your continued responses.

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Dialog Based System Tray App

    There is an article on CG about this, written by Warren Young. I don't have time to find it now, but here is the demo project from it (see attachment).
    Attached Files Attached Files
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Dialog Based System Tray App

    Quote Originally Posted by KnNeeded
    ...but I need to have it not appear in the Taskbar as the access will soley be through the system tray icon. ShowWindow(SW_HIDE) seems to only hide the dialog properly after initial dialog, say behind a button press event. Any Ideas?
    It's so easy:
    Code:
    BOOL CMyAPp::InitInstance()
    {
       CFrameWnd* pFrame = new CFrameWnd;
       pFrame->Create( 0, TEXT("") );  // void frame will invisible forever
       m_pMainWnd = pFrame;
       CMyCoolTrayDlg dlg( pFrame );  // a child of invisible parent never appears in Taskbar
       dlg.DoModal();
       pFrame->DestroyWindow();
       return FALSE;
    }
    Suppose it will help.

    The problem could happen when you try to avoid of any blinking of a dialog. And the root of problem is hidden in the fact that you create a dialog as modal - MFC internals forces a modal dialog to be shown in any case. The solution is to create a modeless dialog and just don't show it.
    Code:
    BOOL CMyAPp::InitInstance()
    {
       CFrameWnd* pFrame = new CFrameWnd;
       pFrame->Create( 0, TEXT("") );  // void frame will invisible forever
       m_pMainWnd = pFrame;
       m_pTrayDlg = new CMyCoolTrayDlg( pFrame );
       m_pTrayDlg->Create( CMyCoolTrayDlg::IDD );
       return TRUE;
    }
    But extra care will be needed for proper m_pTrayDlg and m_pMainWnd termination. Some more writing...
    Best regards,
    Igor

  8. #8
    Join Date
    Jul 2004
    Posts
    2

    Re: Dialog Based System Tray App

    Quote Originally Posted by KnNeeded
    Hi Gurus,

    I have found an article in the Codeguru articles titled "SDI/MDI MFC Application in Windows System Tray", which explains to hide an App you simply call ShowWindow(SW_HIDE) in the application's InitInstance.

    My problem is I am using VC6 with a Dialog based application and want it to appear when run in the system Tray and only show up when the icon in taskbar is clicked. In a MFC Dialog based project the dialog is created with a DoModal call hence I can not call the ShowWindow(SW_HIDE) here, if I call it in the InitDialog, it does not work either.

    I need to application to hide on startup, what am I missing?

    Thanks in advance.

    KnNeeded

    You can try the following way

    1. Make a user define Message and map it to your main dialog. Say WM_USERWNDHIDE.
    2. Then Hide the window into it. Like
    void CDlgTrayDlg::OnHideWindow()
    {
    ShowWindow(SW_HIDE);
    }
    3. Declare a boolean flag into the dialog class and make it TRUE into your dialog class.

    4. add the following code to your OnPaint function
    if( m_bTrayIcon )
    {
    PostMessage(WM_COMMAND,WM_USERWNDHID);
    }

    5. Now handle the TrayIcon handling code in appropriate place and make the
    above m_bTrayIcon flag true false appropriately.

    I think above code will help

  9. #9
    Join Date
    Dec 2001
    Posts
    199

    Smile Re: Dialog Based System Tray App

    Got it now, to avoid the blinking problem I have decided to go with the modeless dialog suggested by Igor Vartanov, thanks. Its a bit more effort but worth it in my case to make it look a bit more professional.

    Thanks to all for your help.

  10. #10
    Join Date
    May 2004
    Posts
    109

    Re: Dialog Based System Tray App

    If I used Igor's solution, how would I go about exiting the app? I tried calling OnOK in the dialog class but no go. postmessage WM_CLOSE doesn't work either.

    any clues?

  11. #11
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Dialog Based System Tray App

    There are some points to remember:

    1. A dialog is modeless.
    So we should close it with DestroyWindow(). It would destroy window but not dialog object. For the latter we should overload PostNcDestroy() and insert a line
    Code:
    delete this;
    at the very end.

    2. If you had accepted the method dealing with the invisible parent of a dialog you should take care about main window or (if you don't care about it) just terminate message loop.
    Code:
    // gracefully close main window
    AfxGetMainWnd()->SendMessage(WM_CLOSE);
    
    // terminate message loop with no remorse
    PostQuitMessage(0);
    Any of that should be done right after dialog window was destroyed.
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured