Solved

Getting an application to run without appearing on the task bar

Posted on 1998-11-04
11
288 Views
Last Modified: 2013-11-20
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
Comment
Question by:manx
  • 6
  • 5
11 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 1324228
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
 

Author Comment

by:manx
ID: 1324229
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
 
LVL 86

Expert Comment

by:jkr
ID: 1324230
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:manx
ID: 1324231
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
 
LVL 86

Expert Comment

by:jkr
ID: 1324232
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
 

Author Comment

by:manx
ID: 1324233
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
 

Author Comment

by:manx
ID: 1324234
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
 
LVL 86

Expert Comment

by:jkr
ID: 1324235
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
 

Author Comment

by:manx
ID: 1324236
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
 
LVL 86

Expert Comment

by:jkr
ID: 1324237
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
 

Author Comment

by:manx
ID: 1324238
Finally managed to get the thing to work.

Thanks jkr

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
difference between String.subString() and String.subSequence() 6 200
centeredAverage challenge 8 167
sum13 challenge 24 107
ShiftLeft challenge 21 82
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question