Click here to Skip to main content
Click here to Skip to main content

Minimize your app to systray in 8 easy steps

, 1 Feb 2004
Rate this:
Please Sign up or sign in to vote.
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 Smile | :)

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.

typedef struct _NOTIFYICONDATA { 
    DWORD cbSize; 
    HWND hWnd; 
    UINT uID; 
    UINT uFlags; 
    UINT uCallbackMessage; 
    HICON hIcon; 
    #if (_WIN32_IE < 0x0500)
        TCHAR szTip[64];
        TCHAR szTip[128];
    #if (_WIN32_IE >= 0x0500)
        DWORD dwState; 
        DWORD dwStateMask; 
        TCHAR szInfo[256]; 
        union {
            UINT  uTimeout; 
            UINT  uVersion; 
        TCHAR szInfoTitle[64]; 
        DWORD dwInfoFlags; 
    #if (_WIN32_IE >= 0x600)
        GUID guidItem;

*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.

  #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()

  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.

    BEGIN_MESSAGE_MAP(CTrayMinDlg, CDialog)


Step: 4

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

  afx_msg void CTrayMinDlg::OnTrayNotify(WPARAM wParam, LPARAM lParam)
    UINT uID; 
    UINT uMsg; 
    uID = (UINT) wParam;
    uMsg = (UINT) lParam; 
    if (uID != 1)
    CPoint pt;  
    switch (uMsg ) 



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.

    void CTrayMinDlg::OnTrayLButtonDown(CPoint pt)
      MessageBox("You have clicked Left mouse Button ");

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

    void CTrayMinDlg::OnTrayRButtonDown(CPoint pt)
      //m_menu is the member of CTrayMinDlg as CMenu m_menu;

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.

  void CShellDlg::OnMinimize() 
    m_TrayData.cbSize = sizeof(NOTIFYICONDATA);
    //Size of this structure, in bytes. 
    m_TrayData.hWnd  = this->m_hWnd;
    //Handle to the window that receives notification 
    //messages associated with an icon in the taskbar 
    //status area. The Shell uses hWnd and uID to 
    //identify which icon to operate on when 
    //Shell_NotifyIcon is invoked. 
    m_TrayData.uID = 1;
    //Application-defined identifier of the taskbar icon.
    //The Shell uses hWnd and uID to identify which icon 
    //to operate on when Shell_NotifyIcon is invoked. You
    // can have multiple icons associated with a single 
    //hWnd by assigning each a different uID. 

    m_TrayData.uCallbackMessage  = WM_TRAY_MESSAGE;
    //Application-defined message identifier. The system 
    //uses this identifier to send notifications to the 
    //window identified in hWnd. These notifications are 
    //sent when a mouse event occurs in the bounding 
    //rectangle of the icon, or when the icon is selected 
    //or activated with the keyboard. 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 pointer moves over a 
    //taskbar icon, lParam is set to WM_MOUSEMOVE. 

    m_TrayData.hIcon = this->m_hIcon;
    //Handle to the icon to be added, modified, or deleted
    strcpy(m_TrayData.szTip,"My Icon");
    //Pointer to a null-terminated string with the text 
    //for a standard ToolTip. It can have a maximum of 64 
    //characters including the terminating NULL. 
    m_TrayData.uFlags = NIF_ICON|NIF_MESSAGE;
    //Flags that indicate which of the other members contain 
    valid data.  

    BOOL bSuccess = FALSE;
    BOOL BSus = FALSE;

    BSus = m_menu.LoadMenu(IDR_MENU1);
      MessageBox("Unabled to Loa menu");

    bSuccess = Shell_NotifyIcon(NIM_ADD,&m_TrayData);

      MessageBox("Unable to Set Tary Icon");


Step: 8

In the exit menu write the following:


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! PinmemberBrian Burns24-Dec-09 11:03 
GeneralPreventing automatic pop-ups [modified] PinmemberBeau Skinner23-May-06 10:38 
QuestionHow to avoid multiple tray ICONs available PinmemberSstar915-Dec-05 19:52 
GeneralTypo in your article PinmemberJaime Olivares15-May-05 6:08 
GeneralMore typos PinmemberJaime Olivares15-May-05 6:14 
GeneralImprovements and a question PinmemberBasirk3-Jun-04 1:55 
GeneralRe: Improvements and a question Pinmembergmalato30-Jun-04 10:54 
GeneralRe: Improvements and a question PinmemberBasirk30-Jun-04 23:29 
GeneralRe: Improvements and a question Pinmemberdefenestration4-Dec-04 3:44 
Generalerror c2440 with VC7 Pinmemberzerdav3-Apr-04 12:43 
GeneralRe: error c2440 with VC7 PinmemberChernobog15-Apr-04 0:13 
GeneralRe: error c2440 with VC7 Pinmemberzerdav5-Apr-04 11:01 
GeneralRe: error c2440 with VC7 Pinmembergjjames29-Apr-04 10:49 
GeneralRe: error c2440 with VC7 Pinmembersbhnet6-May-05 18:38 
GeneralStart in tray Pinmembertzd17-Feb-04 12:03 
GeneralCode in C# PinmemberLegend Never Dies8-Feb-04 21:57 
GeneralRe: Code in C# Pinmembertwisterjosh11-Feb-04 4:54 
GeneralRe: Code in C# PinmemberJaclyn15-Feb-04 23:20 
QuestionCould I minimize application with Modal Dialog? PinmemberOleg32104-Feb-04 2:58 
GeneralContext menu bug PinsussAnonymous2-Feb-04 4:27 
Generalthat's alot of code PinsussAnonymous1-Feb-04 21:51 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 2 Feb 2004
Article Copyright 2004 by Yasar Arslan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid