• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 341
  • Last Modified:

Getting an application to run without appearing on the task bar

I currently am wondering how do I get my app to run without it appearing on the windows task bar. I would like it to appear as an icon on the side of the task bar like eg virus scanning software or display control panels.

I am using MS VC++ 5.0

Thank You
0
manx
Asked:
manx
  • 6
  • 5
1 Solution
 
jkrCommented:
This is quite simple:
1. Don't call 'ShowWindow()'
2. Use the following code to add an icon:
BOOL TaskBarAddIcon     (   HWND    hwnd,
                            UINT    uID,
                            HICON   hicon,
                            LPSTR   lpszTip
                        )
{
    BOOL            bRC;
    NOTIFYICONDATA  nid;
 
    nid.cbSize              =   sizeof  (   NOTIFYICONDATA);
    nid.hWnd                =   hwnd;
    nid.uID                 =   uID;
    nid.uFlags              =   NIF_MESSAGE | NIF_ICON | NIF_TIP;
    nid.uCallbackMessage    =   WM_USER +   uID;
    nid.hIcon               =   hicon;

    if  (   lpszTip)
            lstrcpyn    (   nid.szTip,  lpszTip,    sizeof  (   nid.szTip));
    else
            nid.szTip[ 0]   =   '\0';
 
    bRC     =   Shell_NotifyIcon(   NIM_ADD,    &nid);
 
    if  (   hicon)
            DestroyIcon (   hicon);
 
    return( bRC);
}

0
 
manxAuthor Commented:
Yes the icon does appear in the task bar. But about 1 or 2 seconds later, it dissappears. How do I get it to remain running ?? Do I have to keep redrawing the icon to the task bar ??
0
 
jkrCommented:
No, but you'll have to handle the callback messages generated by the icon in the tray, or it will be removed by the system...
That's a WndProc that does it by passing them to DefWindowProc():

LRESULT CALLBACK WndProc    (   HWND          hWnd,
                                unsigned      uMsg,
                                WPARAM        wParam,
                                LPARAM        lParam
                            )
{

  //Process messages  
  switch( uMsg)
  {
    case WM_CREATE:     //Window creation actions
                        {
                        }    
                        break;



    case WM_CLOSE:      RemoveIcons  ();
                        break;
                                   
    case WM_DESTROY:    PostQuitMessage( 0);
                        break;
             
    default:            return  (   DefWindowProc   (   hWnd,
                                                        uMsg,
                                                        wParam,
                                                        lParam
                                                    )
                                );


  }

   return 0L;
}

0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
manxAuthor Commented:
I am sorry I don't quite get the reply. Where do you add the function, to the winapp ?? Do I have to call the function anywhere ?? Bec when I added it to the application.cpp file, there was no effect the icon still dissappeared. While tracing, the program never executed that bit of code.

By the way thanks for all the good suggestions so far.
0
 
jkrCommented:
Ooops - i forgot ;-)
A WndProc is a window procedure, i.e. the low-level way a window handles its messages. Using MFC, you'll have to provide a handler that returns non-zero when handling the message, so e.g. in your message map & .h:
// .h
class CMyWnd...
{
//...
afx_msg LONG OnMyMessage( UINT, LONG );
//...
}

#define WM_MYMESSAGE (WM_USER + <the ID you used in TaskBarAddIcon>)
BEGIN_MESSAGE_MAP( CMyWnd, CMyParentWndClass )
    //{{AFX_MSG_MAP( CMyWnd
    ON_MESSAGE( WM_MYMESSAGE, OnMyMessage )
    // ... Possibly more entries to handle additional messages
    //}}AFX_MSG_MAP
END_MESSAGE_MAP( )

LONG CMyWnd::OnMyMessage( UINT, LONG)
{
return TRUE;
}
0
 
manxAuthor Commented:
I still can't get it to work. Now the icon appears and remains indefinitely :), however it dissappears whenever I move the mouse near it :(

I think I am following your advice corectly. i.e. I defined a user message OnMyMesssage. Then I called the WndProc from the OnMyMessage functions. Is this correct ?? Also the OnMyMessage has 2 parameters, a uint and a long. I assume the uint is the message for the WndProc function. Where does the long go ??  
By the way do I have to define the removeicon function myself ??
If so what do I write in the function ??

Sorry about asking so many questions.
0
 
manxAuthor Commented:
I still can't get it to work. Now the icon appears and remains indefinitely :), however it dissappears whenever I move the mouse near it :(

I think I am following your advice corectly. i.e. I defined a user message OnMyMesssage. Then I called the WndProc from the OnMyMessage functions. Is this correct ?? Also the OnMyMessage has 2 parameters, a uint and a long. I assume the uint is the message for the WndProc function. Where does the long go ??  
By the way do I have to define the removeicon function myself ??
If so what do I write in the function ??

Sorry about asking so many questions.
0
 
jkrCommented:
No, you don't have to call the WndProc from the message handler, it simply has to return 'TRUE' (in order to keep the icon where it is).
It's always good practise to remove the icon programmatically when your application terminates, e.g. using somthing like this:
BOOL TaskBarDeleteIcon  (   HWND    hwnd,
                                UINT    uID
                            )
{
    BOOL            bRC;
    NOTIFYICONDATA  nid;
 
    nid.cbSize  =   sizeof  (   NOTIFYICONDATA);
    nid.hWnd    =   hwnd;
    nid.uID     =   uID;
 
    bRC     =   Shell_NotifyIcon(   NIM_DELETE, &nid);
 
    return( bRC);
}

(PS: You don't have to apologize for asking questions ;-)
0
 
manxAuthor Commented:
Sorry again jkr, still can't get it to work :(

Is what I' am doing correct ??

1)Insert the calling function in place of the ShowWindow()
2)To keep it on the task bar, write a message handle that returns true. Where is this written in the dlg class or the app class ??
3)Where does the WndProc come in ??

Currently the app appeare on the status bar. It can remain there indefinitely. When ever I bring my mouse cursor over it, then it vanishes.

Thanks for your help again.
0
 
jkrCommented:
Well, forget about WndProc, it would only have been relevant if you were using plain C/C++, not MFC.
The message handler must belong to the window specified in 'TaskBarAddIcon()', as it'll receive the messages.
0
 
manxAuthor Commented:
Finally managed to get the thing to work.

Thanks jkr

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now