Solved

Getting an application to run without appearing on the task bar

Posted on 1998-11-04
11
263 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
 

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now