Click here to Skip to main content
13,800,882 members
Click here to Skip to main content
Add your own
alternative version


135 bookmarked
Posted 7 Dec 2002
Licenced CPOL

System Tray Icons

, 1 Apr 2012
Rate this:
Please Sign up or sign in to vote.
An article on creating, manipulating and showing popup balloons on system tray icons.

Sample Image - SysTrayBalloon.jpg

System tray icons with a tooltip


Note :- This code will work only under Windows 2000 and above, when compiled with VC++ .NET or VC++ 6.0 (with the latest platform SDK).

The system tray is a familiar place for every reasonably experienced Windows OS user. Many applications use this area to place small icons there, which give information about their status. They give the user additional flexibility to directly interact with the application. Almost all applications provide a context menu over the system tray icons for interaction with the application. With the advent of Windows 2000/XP, popup balloons can also be shown over the system tray icons, to notify the user of any relevant event. This article explains the process of creating and showing icons in the system tray for any application. This also caters for multiple icons to be shown for the same application. You can also show popup balloons over the specified icons.

Using the code

  • Include the files SYSTRAYICON.H and SYSTRAYICON.CPP in your project.
  • Create an object of class CSysTrayIcon. In case you wish to process mouse messages over the icons derive a class from CSysTrayIcon and override any or all of the mouse messages mentioned in section "Overridable functions"
  • Create the object of class CSysTrayIcon or a class derived from CSysTrayIcon (preferably in the MainFrame class).
  • Create and manipulate icons in the system tray using the functions given in section "Functions for creating and manipulating icons in the system tray".
  • Mouse messages are trapped and overridable functions are provided for custom processing. See section "Overridable functions" for the list of overridable functions which are triggered by mouse messages.

Functions for creating and manipulating icons in the system tray

  • BOOL CreateIcon(HICON hIcon, UINT nIconID, PCSTR szTip)

    Input parameters

    • HICON hIcon -> Handle to an icon to be shown in the system tray.
    • UINT nIconID -> A user defined icon ID. It can be any unsigned integer.
    • PCSTR szTip -> A tooltip that is shown when the user moves the mouse over the icon.

    Return value

    TRUE if a new tray icon with the ID nIconID could be created, else FALSE

  • BOOL ShowIcon(UINT nIconID)

    Input parameters

    • UINT nIconID ->The icon with this ID to be shown.

    Return value

    TRUE if the icon with this ID exists and could be shown, else FALSE

  • BOOL HideIcon(UINT nIconID) - Hides the icon. It does not delete the icon from memory.

    Input parameters

    • UINT nIconID -> The icon with this ID to be hidden. It is not deleted from memory.

    Return value

    TRUE if the icon with this ID exists and could be hidden, else FALSE

  • BOOL DeleteIcon(UINT nIconID) - Hides and deletes the icon from the memory.

    Input parameters

    • UINT nIconID -> The icon with this ID to be hidden and deleted from memory.

    Return value

    TRUE if the icon with the ID exists and could be removed and deleted from the system tray and memory, else FALSE

  • BOOL ChangeIconTip(UINT nIconID, CString strTip) - This changes the tool tip associated with the system tray icon.

    Input parameters

    • UINT nIconID -> The icon with this ID whose tool tip has to be changed.
    • CString strTip -> The new tool tip to be associated with this icon.

    Return value

    TRUE if the icon with this ID exists and its tool tip cold be changed, else FALSE

  • BOOL ShowBalloon(UINT nIconID, PTSTR szBalloonTitle, PTSTR szBalloonMsg, DWORD dwIcon = NIIF_NONE, UINT nTimeOut = 10) - Shows a popup balloon over the icon with the ID, nIconID.

    Input parameters

    • UINT nIconID -> The icon ID over which the balloon will be shown.
    • PTSTR szBalloonTitle -> A string for the heading of the balloon message
    • PTSTR szBalloonMsg -> A string for the balloon message.
    • DWORD dwIcon -> An additional symbol in the left corner of the balloon that will emphasize the meaning of the balloon. There are four possibilities, NIIF_NONE, NIIF_ERROR, NIIF_INFO and NIIF_WARNING. The default is NIIF_NONE. These are standard constants defined in Windows Platform SDK in shellapi.h
    • UINT nTimeOut -> Time in seconds for the balloon to be visible. In Windows 2000/XP, the minimum is 10 seconds. Even if you specify a time less than 10 seconds, it will be visible for a minimum of 10 seconds. This will probably change with the future versions of Windows.

    Return value

    TRUE if the balloon with given ID exists and the requested balloon could be shown, else FALSE.

System tray balloon details

Overridable functions

There are ten overridable virtual functions, all of which deal with mouse messages.

  • virtual void OnLButtonDown(UINT nIconID, CPoint ptMouse)

    Gets called when the left mouse button has been pressed

  • virtual void OnRButtonDown(UINT nIconID, CPoint ptMouse)
  • Gets called when the right mouse button has been pressed

  • virtual void OnMButtonDown(UINT nIconID, CPoint ptMouse)

    Gets called when the middle mouse button has been pressed

  • virtual void OnLButtonUp(UINT nIconID, CPoint ptMouse)

    Gets called when the left mouse button is released

  • virtual void OnRButtonUp(UINT nIconID, CPoint ptMouse)

    Gets called when the right mouse button is released

  • virtual void OnMButtonUp(UINT nIconID, CPoint ptMouse)

    Gets called when the middle mouse button is released

  • virtual void OnLButtonDblClk(UINT nIconID, CPoint ptMouse)

    Gets called when the left mouse button is double clicked

  • virtual void OnRButtonDblClk(UINT nIconID, CPoint ptMouse)

    Gets called when the right mouse button is double clicked

  • virtual void OnMButtonDblClk(UINT nIconID, CPoint ptMouse)

    Gets called when the middle mouse button is double clicked

  • virtual void OnMouseMove(UINT nIconID, CPoint ptMouse)

    Gets called when the mouse moves over the icon

Input parameters for all overridables:

  • UINT nIconID -> The icon ID over which the mouse message has been generated
  • CPoint ptMouse -> The mouse position in screen co-ordinates

Helper function for handling a context menu

  • BOOL OnContextMenu(CMenu* pContextMenu, CPoint ptMouse, CWnd* pWndMessageHandler)

    This function should be called by the programmer, as it will help him to do the requisite processing to show a context menu. This sets the window mentioned in the third parameter in the foreground, so that commands can be routed to it for processing.

    This function should generally be called in response to a right button down message, typically in the overridden method OnRButtonDown of the class derived from CSysTrayIcon.

    Input parameters:

    • CMenu* pContextMenu -> Pointer to the menu to be shown, it is mostly on a right button press.
    • CPoint ptMouse -> Mouse position in screen co-ordinates.
    • CWnd* pWndMessageHandler -> A pointer to the window that will process the command handlers of the menu pContextMenu. This function sets this window in the foreground so that it is capable of processing the command handlers. This function was written specifically for bringing the menu command handler receiver window in the foreground. In case you don’t bring the window that will receive the menu commands in the foreground, the commands won’t be routed properly. This window is generally the MainFrame window in a typical Windows application.

The implementation of the class CSysTrayIcon

The CSysTrayIcon class is a wrapper around the Windows API, Shell_NotifyIcon and the Windows Platform SDK structure NOTIFYICONDATA, which is declared in the header file shellapi.h. Details about the above mentioned API and structure are well documented in the MSDN documentation.

The class CSysTrayIcon contains a linked list of NOTIFYICONDATA structures (CSystrayIcon::NOTIFYICONDATAList), which maintains information about each icon in the system tray related to that application. Every application can have more than one icon in the system tray. There is no restriction on an application to have only one system tray icon. It is only restricted by the maximum value of an unsigned integer!

Since this class CSysTrayIcon can’t directly receive mouse messages, there has to be a way to route the messages to this class. This is done by creating a hidden window as a private member variable m_wndTrayMsgReceiver (of type CSysTrayWnd) in the class CSysTrayIcon, which is registered to receive the WM_SYSTRAYMSG. When a message WM_SYSTRAYMSG is received by the window object CSysTrayIcon::m_wndTrayMsgReceiver, it is mapped to CSysTrayWnd::OnSysTrayMsg(WPARAM wParam, LPARAM lParam). The lParam gives the type of mouse message and the wParam gives the icon ID on which the mouse message was generated. If you look at the implementation of CSysTrayIcon::CreateIcon(), the NOTIFYICONDATA structure members hWnd and uCallBackMessage are initialized as shown below. This is where the operating system comes to know which window is to be sent the WM_SYSTRAYMSG.

pnidIconInfo->hWnd  = m_wndTrayMsgReceiver.GetSafeHwnd();
pnidIconInfo->uCallbackMessage = WM_SYSTRAYMSG;

Message Routing Mechanism

An object of CSysTrayWnd window is created in the constructor of CSysTrayIcon itself. The CSystTrayWnd also has a pointer to the CSysTrayIcon class which helps in calling the virtual overridable functions as mentioned in "Overridable functions".

There are few other functions which are used internally in the class to get the icons from the linked list, if given the icon ID as the parameter. These are summarized below.

  • GetNotifyIconDataFromID() -> Retrieves the NOTIFYCOINDATA structure from the linked list CSysTrayIcon::NOTIFYICONDATAList for the given icon ID.
  • CheckIfIconIDExists() -> This is called to check if the icon with the given ID already exists, when creating a new system tray icon.
  • DeleteNotifyIconDataFromID() -> This is called from the DeleteIcon() function and the destructor of the CSystTrayIcon class for deleting the icon and proper cleanup. This function removes the icon by removing and deleting it from the linked list CSysTrayIcon::NOTIFYICONDATAList.

Demo project

A class is derived from CSysTrayIcon with the name CMyAppTrayIcon. This class overrides only two virtual functions, OnLButtonDownDBlClk() to show a message box and OnRButtonDown to show a context menu. These two functions get triggered for the Windows messages WM_LBUTTONDBLCLK and WM_RBUTTONDOWN respectively, by the routing through a CSysTrayWnd object.

An object of CMyAppTrayIcon is kept as a member variable of CMainFrame class. Four icons are created in the CMainFrame::OnCreate(). The CMainFrame class handles all the command handlers for the context menu for the system tray icons.

For the context menu processing, the overridden OnRButtonDown method passes the main window's pointer to the CSysTrayIcon::OnContextMenu as the third parameter. The main window has the command handlers for the required processing.

 void CMyAppTrayIcon::OnRButtonDown(UINT nIconID, CPoint ptMouse)
    CMenu t_Menu;
    OnContextMenu(t_Menu.GetSubMenu(0), ptMouse, ::AfxGetMainWnd());

Use the icon menu to manipulate the system tray icons as shown in the top figure.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Prateek Kaul
Web Developer
India India
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionDouble Shell_NotifyIcon Pin
Gian Luca Tanda16-Mar-17 14:19
memberGian Luca Tanda16-Mar-17 14:19 
AnswerRe: Double Shell_NotifyIcon Pin
Prateek Kaul22-Mar-17 2:05
memberPrateek Kaul22-Mar-17 2:05 
BugWARNING: The demo project executable has a TROJAN. BEWARE Pin
Adi Shavit27-Jan-16 5:25
memberAdi Shavit27-Jan-16 5:25 
GeneralMy vote of 5 Pin
Michael Haephrati4-Dec-12 3:30
mvpMichael Haephrati4-Dec-12 3:30 
QuestionDecent Pin
BillW3311-Sep-12 3:57
professionalBillW3311-Sep-12 3:57 
Questioncan i use this code with Windows SDI project ? Pin
infant_coder9-May-12 20:46
memberinfant_coder9-May-12 20:46 
GeneralPblm with event handling from tray icon....... Pin
ajith_m11-Feb-09 20:18
memberajith_m11-Feb-09 20:18 
GeneralI'm using VC++ 6 Enterprise Edition Pin
senthurpandian7-Nov-08 0:21
membersenthurpandian7-Nov-08 0:21 
GeneralRe: I'm using VC++ 6 Enterprise Edition Pin
Prateek Kaul16-Nov-08 21:15
memberPrateek Kaul16-Nov-08 21:15 
Generalhandling context menu Pin
laleh_sg24-May-06 4:14
memberlaleh_sg24-May-06 4:14 
Generalproblem with event handling [modifed] Pin
laleh_sg20-May-06 3:23
memberlaleh_sg20-May-06 3:23 
GeneralRe: problem with event handling [modifed] Pin
Prateek Kaul21-May-06 7:48
memberPrateek Kaul21-May-06 7:48 
GeneralBugs when using WM_TIMER Pin
Allad20-Mar-06 14:27
memberAllad20-Mar-06 14:27 
GeneralContent length... Pin
ioannis-e15-Jun-05 2:24
memberioannis-e15-Jun-05 2:24 
Generalabout VC++ 6.0 compile..... Pin
Seungkwon Kim26-May-05 17:36
memberSeungkwon Kim26-May-05 17:36 
GeneralRe: about VC++ 6.0 compile..... Pin
Christian Graus26-May-05 17:57
protectorChristian Graus26-May-05 17:57 
GeneralRe: about VC++ 6.0 compile..... Pin
Seungkwon Kim26-May-05 21:08
memberSeungkwon Kim26-May-05 21:08 
GeneralCan't complie with VC++6 Pin
Mark Chuang29-Mar-05 21:53
memberMark Chuang29-Mar-05 21:53 
GeneralRe: Can't complie with VC++6 [modified] Pin
Prateek Kaul30-Mar-05 0:04
memberPrateek Kaul30-Mar-05 0:04 
QuestionAny idea, using this, for NTService application? Pin
Anonymous13-Apr-04 0:44
memberAnonymous13-Apr-04 0:44 
GeneralWinXP autoHide icon Pin
darwinw14-Nov-03 22:12
memberdarwinw14-Nov-03 22:12 
GeneralRe: WinXP autoHide icon Pin
The_Mega_ZZTer1-Nov-05 10:52
memberThe_Mega_ZZTer1-Nov-05 10:52 
Generalresponding to user clicking the balloon Pin
Jase Jennings25-Jul-03 14:40
memberJase Jennings25-Jul-03 14:40 
GeneralRe: responding to user clicking the balloon Pin
ChetnaBABY20-Aug-04 1:50
memberChetnaBABY20-Aug-04 1:50 
GeneralDisplaying balloon for hidden icons Pin
SLiDeR17-Dec-02 4:50
memberSLiDeR17-Dec-02 4:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.181215.1 | Last Updated 1 Apr 2012
Article Copyright 2002 by Prateek Kaul
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid