Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Getting an application to run without appearing on the task bar

Posted on 1998-11-04
11
294 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Include multiple hostnames in this command? 3 55
substring method in java 1 129
post4 challenge 28 104
Way to decrease size of apk file 9 86
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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 video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

808 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