Click here to Skip to main content
11,790,021 members (49,187 online)
Click here to Skip to main content

Minimize your app to systray in 8 easy steps

, 1 Feb 2004 130.8K 4.4K 77
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.


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 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;

  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

You may also be interested in...

Comments and Discussions

GeneralReally bad bug, please fix! Pin
Brian Burns24-Dec-09 11:03
memberBrian Burns24-Dec-09 11:03 
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
gmalato30-Jun-04 10:54
membergmalato30-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
gjjames29-Apr-04 10:49
membergjjames29-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
Legend Never Dies8-Feb-04 21:57
memberLegend Never Dies8-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
sussAnonymous2-Feb-04 4:27 
Generalthat's alot of code Pin
Anonymous1-Feb-04 21:51
sussAnonymous1-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 | Terms of Use | Mobile
Web03 | 2.8.1509028.1 | Last Updated 2 Feb 2004
Article Copyright 2004 by Yasar Arslan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid