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

System Tray Icons - Minimize Your Application to the SysTray

, 6 Nov 2000 CPOL
Rate this:
Please Sign up or sign in to vote.
Minimize your application to system tray instead of the taskbar
<!-- Link to source file download --> <!-- Article image -->

Sample Image - TrayIcons.jpg

<!-- Add the rest of your HTML here -->


This article illustrates the use of Shell_NotifyIcon to create and manage System Tray icons. The article explains the basics of this operation and assists you in creating your own 'Minimize To Tray' applications. The source code provided with this article is designed to work with dialog-based applications, but it can easily be modified to work with a CFrameWnd or CWnd based application.

Creating and Using Tray Icons

To create a Tray Icon, you need to call the following shell function: -

BOOL Shell_NotifyIcon( DWORD dwMessage, PNOTIFYICONDATA pnid );

The dwMessage parameter specifies the action to be taken - NIM_ADD, NIM_DELETE, NIM_MODIFY adds, deletes and modifies tray icons respectively.

The pnid parameter is used to customize, create, delete and obtain data from the Tray Icon. (See the MSDN Library for more details about this structure.)

Creating The Application

1. Create a new VC++ dialog based project. For this example, I will call this project MyTray which will contain the CMyTrayApp and CMyTrayDlg classes.

2. Download and extract the DialogTray source code to the root of the project folder

3. From the Project->Add To Project menu, select Files and then select TrayDialog.h and TrayDialog.cpp. This will add a new class to your project named CTrayDialog.

4. Replace the CMyTrayDlg base class with CTrayDialog.

class CMyTrayDlg : public CDialog


#include "TrayDialog.h"

class CMyTrayDlg : public CTrayDialog

5. Replace the other occurrences of CDialog in the MyTrayDlg.cpp file as follows :-

CMyTrayDlg::CMyTrayDlg(CWnd* pParent /*=NULL*/)
  : CDialog(CMyTrayDlg::IDD, pParent)


CMyTrayDlg::CMyTrayDlg(CWnd* pParent /*=NULL*/)	
    : CTrayDialog(CMyTrayDlg::IDD, pParent)

6. Create a menu resource and name it IDR_MENU1

7. In the InitDialog member function, enter the following:

    TraySetToolTip("ToolTip for tray icon");

8. Modify the IDD_MYTRAY _DIALOG resource to have a minimize box.

9. Build and run the application

NB : To add tray menu item handlers use the class wizard.

Displaying the tray icon all the time

Simply add a TrayShow() statement to InitDialog() in CMyTrayDlg.cpp, and call TraySetMinimizeToTray(FALSE) to disable minimizing to the tray.

The events that occur in the tray are captured through the following functions:

    virtual void OnTrayLButtonDown(CPoint pt);
    virtual void OnTrayLButtonDblClk(CPoint pt);

	virtual void OnTrayRButtonDown(CPoint pt);
	virtual void OnTrayRButtonDblClk(CPoint pt);
	virtual void OnTrayMouseMove(CPoint pt);

Feel free to add more events or to improve on these ones.


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


About the Author

Daniel Zilcsak
Software Developer
United States United States
No Biography provided

Comments and Discussions

QuestionTrayShow() leads the minimize button becoming invalid PinmemberScarlettlee16-May-13 1:17 
QuestionHow to do If I want it be Tray Mode while begging? Pinmembervannes23-Dec-11 2:12 
AnswerRe: How to do If I want it be Tray Mode while begging? PinmemberEsenChang5-Aug-12 18:38 
Questionprogramatically minimizing does not work on VS 2005 PinmemberErik17-Oct-11 20:01 
QuestionCWinApp PinmemberMember 806538826-Aug-11 0:52 
AnswerRe: CWinApp PinmemberMember 806538826-Aug-11 1:13 
QuestionLicencing Pinmemberrainer.hochdorfer27-Apr-10 23:06 
AnswerRe: Licencing PinmemberDaniel Zilcsak28-Apr-10 4:29 
AnswerRe: Licencing Pinmemberrainer.hochdorfer28-Apr-10 7:02 
GeneralThanks just perfect Pinmemberasierra0111-Jan-10 10:14 
GeneralRight Click Menu PinmemberShup15-May-09 2:20 
GeneralThank you! PinmemberDevMan7712-May-09 9:20 
GeneralChanging tray icon while minimized Pinmembereight26-Mar-09 3:21 
GeneralThanks!!! PinmemberIAM...15-Feb-09 6:56 
QuestionOnly minimizing to tray in my application? PinmemberMember 192119231-Oct-08 6:03 
QuestionBalloon Tip?? Pinmemberthanasis_rassp12-Aug-08 4:16 
AnswerRe: Balloon Tip?? Pinmembervarandas7924-Sep-08 6:25 
Great class.

replace .cpp:

void CTrayDialog::TraySetToolTip(CString mToolTip, DWORD mIcon)
strcpy(m_nidIconData.szInfo, mToolTip);

CString tmp;
strcpy(m_nidIconData.szInfoTitle, tmp);

m_nidIconData.uFlags |= NIF_MESSAGE | NIF_ICON | NIF_TIP | NIF_INFO;
// Flag Description:
// - NIF_ICON The hIcon member is valid.
// - NIF_MESSAGE The uCallbackMessage member is valid.
// - NIF_TIP The szTip member is valid.
// - NIF_STATE The dwState and dwStateMask members are valid.
// - NIF_INFO Use a balloon ToolTip instead of a standard ToolTip. The szInfo, uTimeout, szInfoTitle, and dwInfoFlags members are valid.
// - NIF_GUID Reserved.

m_nidIconData.dwInfoFlags = mIcon; // add an icon to a balloon ToolTip
// Flag Description
// - NIIF_ERROR An error icon.
// - NIIF_INFO An information icon.
// - NIIF_NONE No icon.
// - NIIF_WARNING A warning icon.
// - NIIF_ICON_MASK Version 6.0. Reserved.
// - NIIF_NOSOUND Version 6.0. Do not play the associated sound. Applies only to balloon ToolTips

replace .h

void TraySetToolTip(CString mToolTip, DWORD mIcon=NIIF_INFO);
GeneralBug in CTrayDialog::OnSysCommand preventing sizing PinmemberEricGen25-Mar-08 0:02 
GeneralSystem get slow... PinmemberMember 385006728-Feb-08 22:52 
Generalsmall but useful demo Pinmemberlilesh10-Oct-07 23:33 
QuestionHow can I get my app on the tray at the very beginning? Pinmemberpjvv126-Sep-07 8:31 
AnswerSolution Pinmemberlilesh11-Oct-07 0:01 
Questionsystray without window Pinmemberchergui3-Apr-07 2:36 
GeneralIn VC++ 2003, there's error Pinmembertlbf10-Jan-07 3:46 
GeneralRe: In VC++ 2003, there's error Pinmemberchild00728-Jan-08 18:32 
GeneralRe: In VC++ 2003, there's error PinmemberJerry Peterson14-May-09 4:15 
Generaltray disappear after mouse over PinmembergeoPsyc4-Dec-06 14:05 
GeneralRe: tray disappear after mouse over PinmembergeoPsyc4-Dec-06 14:24 
GeneralRe: tray disappear after mouse over Pinmemberlilesh11-Oct-07 0:16 
QuestionRe: tray disappear after mouse over PinmemberAmbiguities23-Oct-07 18:53 
GeneralRe: tray disappear after mouse over PinmemberDau Le Trung4-Apr-12 18:29 
Questioncan you help me? about tray icon PinmemberDavid121519-Jun-06 18:08 
GeneralCompiling on VIsual C++ 7.0 PinmemberSychad19-Apr-06 8:34 
GeneralRe: Compiling on VIsual C++ 7.0 Pinmemberchild00728-Jan-08 18:31 
GeneralDisplaying the menu in the icon tray problem Pinmemberchris17514-Mar-06 9:01 
GeneralRe: Displaying the menu in the icon tray problem PinmemberDean Michaud24-Mar-06 5:30 
QuestionItem1=ShowWindow? PinmemberAm0k30-Jan-06 5:12 
GeneralIssue With CE Pinmemberoilos16-Aug-05 2:23 
GeneralProblem Pinmemberharpreetsbamrah23-Jun-05 21:24 
GeneralRe: Problem Pinmemberharpreetsbamrah23-Jun-05 21:44 
Generalsorry Pinmembersicks18-Jun-05 7:42 
Generalproblem Pinmembersicks18-Jun-05 7:29 
GeneralRe: problem PinmemberAleJ15-Jun-06 6:36 
GeneralThankYou !! Modification !! Pinmemberharpreetsbamrah9-Jun-05 23:58 
GeneralMenu will not disapper Pinmember_bko23-May-05 0:43 
JokeRe: Menu will not disapper PinmemberHarry Lau6-Apr-06 5:33 
GeneralRe: Menu will not disapper Pinmemberfherlan2-Dec-06 6:42 
GeneralThanks Daniel ! Pinmemberganesh_nv@yahoo.com18-Apr-05 20:53 
GeneralSome bugs Pinmemberrrrado7-Mar-05 1:43 
GeneralRe: Some bugs Pinmemberrrrado7-Mar-05 3:26 

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
Web04 | 2.8.150414.1 | Last Updated 7 Nov 2000
Article Copyright 2000 by Daniel Zilcsak
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid