Click here to Skip to main content
11,789,710 members (60,741 online)
Click here to Skip to main content

System Tray Icons - Minimize Your Application to the SysTray

, 6 Nov 2000 CPOL 425.6K 13.3K 142
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

You may also be interested in...

Comments and Discussions

GeneralThank you! Pin
DevMan7712-May-09 8:20
memberDevMan7712-May-09 8:20 
GeneralChanging tray icon while minimized Pin
eight26-Mar-09 2:21
membereight26-Mar-09 2:21 
GeneralThanks!!! Pin
IAM...15-Feb-09 5:56
memberIAM...15-Feb-09 5:56 
QuestionOnly minimizing to tray in my application? Pin
Member 192119231-Oct-08 5:03
memberMember 192119231-Oct-08 5:03 

I just downloaded the sample, followed the instructions in a new sample project, everything ok.

When I added TrayDialog to my current dialog based project in the same way, the only thing what happens is, that the program minimizes to the tray. There's no menu, no reaction on left double click. The only thing shown is the tooltip. I have to kill the application with the task manager...

There's no more CDialog reference in my source. It's nearly 99% the same code in OnInitDialog, message maps etc.

Any idea what to look for?

QuestionBalloon Tip?? Pin
thanasis_rassp12-Aug-08 3:16
memberthanasis_rassp12-Aug-08 3:16 
AnswerRe: Balloon Tip?? Pin
varandas7924-Sep-08 5:25
membervarandas7924-Sep-08 5:25 
GeneralBug in CTrayDialog::OnSysCommand preventing sizing Pin
EricGen24-Mar-08 23:02
memberEricGen24-Mar-08 23:02 
GeneralSystem get slow... Pin
Member 385006728-Feb-08 21:52
memberMember 385006728-Feb-08 21:52 
Generalsmall but useful demo Pin
lilesh10-Oct-07 22:33
memberlilesh10-Oct-07 22:33 
QuestionHow can I get my app on the tray at the very beginning? Pin
pjvv126-Sep-07 7:31
memberpjvv126-Sep-07 7:31 
AnswerSolution Pin
lilesh10-Oct-07 23:01
memberlilesh10-Oct-07 23:01 
Questionsystray without window Pin
chergui3-Apr-07 1:36
memberchergui3-Apr-07 1:36 
GeneralIn VC++ 2003, there's error Pin
tlbf10-Jan-07 2:46
membertlbf10-Jan-07 2:46 
GeneralRe: In VC++ 2003, there's error Pin
child00728-Jan-08 17:32
memberchild00728-Jan-08 17:32 
GeneralRe: In VC++ 2003, there's error Pin
Jerry Peterson14-May-09 3:15
memberJerry Peterson14-May-09 3:15 
Generaltray disappear after mouse over Pin
geoPsyc4-Dec-06 13:05
membergeoPsyc4-Dec-06 13:05 
GeneralRe: tray disappear after mouse over Pin
geoPsyc4-Dec-06 13:24
membergeoPsyc4-Dec-06 13:24 
GeneralRe: tray disappear after mouse over Pin
lilesh10-Oct-07 23:16
memberlilesh10-Oct-07 23:16 
QuestionRe: tray disappear after mouse over Pin
Ambiguities23-Oct-07 17:53
memberAmbiguities23-Oct-07 17:53 
GeneralRe: tray disappear after mouse over Pin
Dau Le Trung4-Apr-12 17:29
memberDau Le Trung4-Apr-12 17:29 
Questioncan you help me? about tray icon Pin
David121519-Jun-06 17:08
memberDavid121519-Jun-06 17:08 
GeneralCompiling on VIsual C++ 7.0 Pin
Sychad19-Apr-06 7:34
memberSychad19-Apr-06 7:34 
GeneralRe: Compiling on VIsual C++ 7.0 Pin
child00728-Jan-08 17:31
memberchild00728-Jan-08 17:31 
GeneralDisplaying the menu in the icon tray problem Pin
chris17514-Mar-06 8:01
memberchris17514-Mar-06 8:01 
GeneralRe: Displaying the menu in the icon tray problem Pin
Dean Michaud24-Mar-06 4:30
memberDean Michaud24-Mar-06 4:30 
QuestionItem1=ShowWindow? Pin
Am0k30-Jan-06 4:12
memberAm0k30-Jan-06 4:12 
GeneralIssue With CE Pin
oilos16-Aug-05 1:23
memberoilos16-Aug-05 1:23 
GeneralProblem Pin
harpreetsbamrah23-Jun-05 20:24
memberharpreetsbamrah23-Jun-05 20:24 
GeneralRe: Problem Pin
harpreetsbamrah23-Jun-05 20:44
memberharpreetsbamrah23-Jun-05 20:44 
Generalsorry Pin
sicks18-Jun-05 6:42
membersicks18-Jun-05 6:42 
Generalproblem Pin
sicks18-Jun-05 6:29
membersicks18-Jun-05 6:29 
GeneralRe: problem Pin
AleJ15-Jun-06 5:36
memberAleJ15-Jun-06 5:36 
GeneralThankYou !! Modification !! Pin
harpreetsbamrah9-Jun-05 22:58
memberharpreetsbamrah9-Jun-05 22:58 
GeneralMenu will not disapper Pin
_bko22-May-05 23:43
member_bko22-May-05 23:43 
JokeRe: Menu will not disapper Pin
Harry Lau6-Apr-06 4:33
memberHarry Lau6-Apr-06 4:33 
GeneralRe: Menu will not disapper Pin
fherlan2-Dec-06 5:42
memberfherlan2-Dec-06 5:42 
GeneralThanks Daniel ! Pin
ganesh_nv@yahoo.com18-Apr-05 19:53
memberganesh_nv@yahoo.com18-Apr-05 19:53 
GeneralSome bugs Pin
rrrado7-Mar-05 0:43
memberrrrado7-Mar-05 0:43 
GeneralRe: Some bugs Pin
rrrado7-Mar-05 2:26
memberrrrado7-Mar-05 2:26 
GeneralGreat job -- small improvement Pin
Hard Hat23-Feb-05 1:21
memberHard Hat23-Feb-05 1:21 
GeneralTerminating from TaskManager Pin
Alan Buchanan23-Feb-05 0:02
memberAlan Buchanan23-Feb-05 0:02 
GeneralRe: Terminating from TaskManager Pin
Anonymous23-Feb-05 14:36
sussAnonymous23-Feb-05 14:36 
GeneralRe: Terminating from TaskManager Pin
Anonymous18-Apr-05 20:45
sussAnonymous18-Apr-05 20:45 
GeneralRe: Terminating from TaskManager Pin
TaLF25-Jun-05 13:17
memberTaLF25-Jun-05 13:17 
GeneralRe: Solution - Terminating from TaskManager Pin
IVarshilov24-May-09 22:53
memberIVarshilov24-May-09 22:53 
GeneralStart Dialog Minimized to SysTray Pin
codehar13-Feb-05 19:04
membercodehar13-Feb-05 19:04 
GeneralRe: Start Dialog Minimized to SysTray Pin
mysticwolf9-Mar-05 6:18
membermysticwolf9-Mar-05 6:18 
GeneralRe: Start Dialog Minimized to SysTray Pin
bubis14-Sep-05 2:57
memberbubis14-Sep-05 2:57 
AnswerRe: Start Dialog Minimized to SysTray Pin
CodeWatcher27-Oct-05 13:28
memberCodeWatcher27-Oct-05 13:28 
GeneralRe: Start Dialog Minimized to SysTray Pin
binyo6618-May-09 16:55
memberbinyo6618-May-09 16:55 

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