Click here to Skip to main content
14,846,774 members
Articles / Programming Languages / C++
Posted 1 Feb 2004


79 bookmarked

Minimize your app to systray in 8 easy steps

Rate me:
Please Sign up or sign in to vote.
4.71/5 (24 votes)
1 Feb 20042 min read
Minimize your app to systray in 8 easy steps

Sample Image

Sample Image


This article explains the use of Shell_NotifyIcon to create and manage System Tray icons with your own application. The article explains the basics of this procedure and provides a walk in to create your own 'Minimize To Tray' applications. The article provides 8 easy steps to follow and make your app work in system tray and handle your tray notifications. The source code provided with this article is designed to work with dialog-based applications.

Tray Icons

To use tray Icons you have to call a shell function :)

  1  BOOL Shell_NotifyIcon( DWORD dwMessage, PNOTIFYICONDATA pnid );

The dwMessage specifys the NIM_ADD,NIM_DELETE and NIM_MODIFY which adds ,delete and modifies an icon in system tray.

PNOTIFYICONDATA structure contains information that the system needs to process taskbar status area messages.

  1  typedef struct _NOTIFYICONDATA { 
  2      DWORD cbSize; 
  3      HWND hWnd; 
  4      UINT uID; 
  5      UINT uFlags; 
  6      UINT uCallbackMessage; 
  7      HICON hIcon; 
  8      #if (_WIN32_IE < 0x0500)
  9          TCHAR szTip[64];
 10      #else
 11          TCHAR szTip[128];
 12      #endif
 13      #if (_WIN32_IE >= 0x0500)
 14          DWORD dwState; 
 15          DWORD dwStateMask; 
 16          TCHAR szInfo[256]; 
 17          union {
 18              UINT  uTimeout; 
 19              UINT  uVersion; 
 20          } DUMMYUNIONNAME;
 21          TCHAR szInfoTitle[64]; 
 22          DWORD dwInfoFlags; 
 23      #endif
 24      #if (_WIN32_IE >= 0x600)
 25          GUID guidItem;
 26      #endif

*Note: For complete information see MSDN.

Creating the Application

Create a new VC++ dialog based project and call it TrayMin.

Step: 1

Define user message in the TrayMinDlg.h header file.

  1  #define WM_TRAY_MESSAGE (WM_USER + 1)

The WM_USER constant is used by applications to help define private messages for use by private window classes, usually of the form WM_USER+X, where X is an integer value.

*See MSDN for more details.

Step: 2

Now add the handler for this user function in TrayMinDlg.h file before the DECLARE_MESSAGE_MAP()

  1  afx_msg void OnTrayNotify(WPARAM wParam, LPARAM lParam);

This is icon's callback message when you add the icon to the taskbar. The callback message identifier is specified in the uCallbackMessage member of the NOTIFYICONDATA structure passed with NIM_ADD(We will see this little later). When an event occurs, the system sends the callback message to the window procedure of the window specified by the hWnd member. The wParam parameter of the message contains the identifier of the taskbar icon in which the event occurred. The lParam parameter holds the mouse or keyboard message associated with the event. For example, when the mouse pointer moves onto a taskbar icon, lParam contains WM_MOUSEMOVE.

Step: 3

Now add the following line in main message map of TrayMinDlg.cpp


Which should now look like this.

  1  BEGIN_MESSAGE_MAP(CTrayMinDlg, CDialog)
  2    //{{AFX_MSG_MAP(CTrayMinDlg)
  4    ON_WM_PAINT()
  7    //}}AFX_MSG_MAP

Step: 4

Now add OnTrayNotify function's definition in the TrayMinDlg.cpp don't forget to add afx_msg before the function.

  1  afx_msg void CTrayMinDlg::OnTrayNotify(WPARAM wParam, LPARAM lParam)
  2  {
  3    UINT uID;
  4    UINT uMsg;
  6    uID = (UINT) wParam;
  7    uMsg = (UINT) lParam;
 10    if (uID != 1)
 11      return;
 13    CPoint pt;
 16    switch (uMsg )
 17    {
 19    case WM_LBUTTONDOWN:
 20      GetCursorPos(&pt);
 21      ClientToScreen(&pt);
 22      OnTrayLButtonDown(pt);
 23      break;
 25    case WM_RBUTTONDOWN:
 26    case WM_CONTEXTMENU:
 27      GetCursorPos(&pt);
 28      OnTrayRButtonDown(pt);
 29      break;
 31    }
 32    return;
 33  }

Step: 5

Now add two member functions in the TrayMinDlg class to handle the mouse events.

To handle Left Button Down/Click add the following function.

  • Function Type: void
  • Function Declaration: OnTrayLButtonDown(CPoint pt)

To handle Right Button Down/Click add the following function.

  • Function Type: void
  • Function Declaration: OnTrayRButtonDown(CPoint pt)

The Declaration of OnTrayLButtonDown(CPoint pt) is as following.

  1  void CTrayMinDlg::OnTrayLButtonDown(CPoint pt)
  2  {
  3    MessageBox("You have clicked Left mouse Button ");
  4  }

The Declaration of OnTrayRButtonDown(CPoint pt) is as following.

  1  void CTrayMinDlg::OnTrayRButtonDown(CPoint pt)
  2  {
  3    //m_menu is the member of CTrayMinDlg as CMenu m_menu;
  4    m_menu.GetSubMenu(0)->TrackPopupMenu(TPM_BOTTOMALIGN|
  5     TPM_LEFTBUTTON|TPM_RIGHTBUTTON,pt.x,pt.y,this);
  6  }

Step: 6

Add two member variable to the CTrayMinDlg.

  • Variable Type: NOTIFYICONDATA
  • Variable Name: m_TrayData;
  • Variable Type: CMenu
  • Variable Name: m_menu;

Now add a menu resource.

Step: 7

Now draw a minimize button on the dialog and write the following in it.

  1  void CShellDlg::OnMinimize()
  2  {
  3    m_TrayData.cbSize = sizeof(NOTIFYICONDATA);
  4    //Size of this structure, in bytes.
  7    m_TrayData.hWnd  = this->m_hWnd;
  8    //Handle to the window that receives notification
  9    //messages associated with an icon in the taskbar
 10    //status area. The Shell uses hWnd and uID to
 11    //identify which icon to operate on when
 12    //Shell_NotifyIcon is invoked.
 14    m_TrayData.uID = 1;
 15    //Application-defined identifier of the taskbar icon.
 16    //The Shell uses hWnd and uID to identify which icon
 17    //to operate on when Shell_NotifyIcon is invoked. You
 18    // can have multiple icons associated with a single
 19    //hWnd by assigning each a different uID.
 21    m_TrayData.uCallbackMessage  = WM_TRAY_MESSAGE;
 22    //Application-defined message identifier. The system
 23    //uses this identifier to send notifications to the
 24    //window identified in hWnd. These notifications are
 25    //sent when a mouse event occurs in the bounding
 26    //rectangle of the icon, or when the icon is selected
 27    //or activated with the keyboard. The wParam parameter
 28    //of the message contains the identifier of the taskbar
 29    //icon in which the event occurred. The lParam parameter
 30    //holds the mouse or keyboard message associated with the
 31    // event. For example, when the pointer moves over a
 32    //taskbar icon, lParam is set to WM_MOUSEMOVE.
 36    m_TrayData.hIcon = this->m_hIcon;
 37    //Handle to the icon to be added, modified, or deleted
 39    strcpy(m_TrayData.szTip,"My Icon");
 40    //Pointer to a null-terminated string with the text
 41    //for a standard ToolTip. It can have a maximum of 64
 42    //characters including the terminating NULL.
 45    m_TrayData.uFlags = NIF_ICON|NIF_MESSAGE;
 46    //Flags that indicate which of the other members contain
 47    valid data.
 50    BOOL bSuccess = FALSE;
 51    BOOL BSus = FALSE;
 53    BSus = m_menu.LoadMenu(IDR_MENU1);
 54    if(!(BSus))
 55      MessageBox("Unabled to Loa menu");
 57    bSuccess = Shell_NotifyIcon(NIM_ADD,&m_TrayData);
 59    if(!(bSuccess))
 60      MessageBox("Unable to Set Tary Icon");
 61    else
 62    {
 63      this->ShowWindow(SW_MINIMIZE);
 64      this->ShowWindow(SW_HIDE);
 66    }
 67  }

Step: 8

In the exit menu write the following:

  1  Shell_NotifyIcon(NIM_DELETE,&m_TrayData);
  2    DestroyWindow();

Now Run the app and Press the minimize button and your app will be minimized to taskbar. Fell free to modify and improve these steps.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Yasar Arslan
United States United States
No Biography provided

Comments and Discussions

GeneralReally bad bug, please fix! Pin
Brian Burns24-Dec-09 11:03
MemberBrian Burns24-Dec-09 11:03 
GeneralSimple and .. Pin
VEMS7-Aug-08 7:12
MemberVEMS7-Aug-08 7:12 
GeneralPreventing automatic pop-ups [modified] Pin
Beau Skinner23-May-06 10:38
MemberBeau Skinner23-May-06 10:38 
QuestionHow to avoid multiple tray ICONs available Pin
Sstar915-Dec-05 19:52
MemberSstar915-Dec-05 19:52 
GeneralTypo in your article Pin
Jaime Olivares15-May-05 6:08
MemberJaime Olivares15-May-05 6:08 
GeneralMore typos Pin
Jaime Olivares15-May-05 6:14
MemberJaime Olivares15-May-05 6:14 
GeneralImprovements and a question Pin
Basirk3-Jun-04 1:55
MemberBasirk3-Jun-04 1:55 
GeneralRe: Improvements and a question Pin
Guido Malato30-Jun-04 10:54
MemberGuido Malato30-Jun-04 10:54 
GeneralRe: Improvements and a question Pin
Basirk30-Jun-04 23:29
MemberBasirk30-Jun-04 23:29 
GeneralRe: Improvements and a question Pin
Defenestration4-Dec-04 3:44
MemberDefenestration4-Dec-04 3:44 
Generalerror c2440 with VC7 Pin
zerdav3-Apr-04 12:43
Memberzerdav3-Apr-04 12:43 
GeneralRe: error c2440 with VC7 Pin
Chernobog15-Apr-04 0:13
MemberChernobog15-Apr-04 0:13 
GeneralRe: error c2440 with VC7 Pin
zerdav5-Apr-04 11:01
Memberzerdav5-Apr-04 11:01 
GeneralRe: error c2440 with VC7 Pin
G. Jason James29-Apr-04 10:49
MemberG. Jason James29-Apr-04 10:49 
GeneralRe: error c2440 with VC7 Pin
sbhnet6-May-05 18:38
Membersbhnet6-May-05 18:38 
GeneralStart in tray Pin
tzd17-Feb-04 12:03
Membertzd17-Feb-04 12:03 
GeneralCode in C# Pin
Member 3350228-Feb-04 21:57
MemberMember 3350228-Feb-04 21:57 
GeneralRe: Code in C# Pin
twisterjosh11-Feb-04 4:54
Membertwisterjosh11-Feb-04 4:54 
GeneralRe: Code in C# Pin
Jaclyn15-Feb-04 23:20
MemberJaclyn15-Feb-04 23:20 
QuestionCould I minimize application with Modal Dialog? Pin
Oleg32104-Feb-04 2:58
MemberOleg32104-Feb-04 2:58 
GeneralContext menu bug Pin
Anonymous2-Feb-04 4:27
MemberAnonymous2-Feb-04 4:27 
Generalthat's alot of code Pin
Anonymous1-Feb-04 21:51
MemberAnonymous1-Feb-04 21:51 

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.