Click here to Skip to main content
15,861,125 members
Articles / Programming Languages / C++
Article

System Tray Icons - Minimize Your Application to the SysTray

Rate me:
Please Sign up or sign in to vote.
4.79/5 (79 votes)
6 Nov 2000CPOL 542K   16.5K   150   140
Minimize your application to system tray instead of the taskbar

Sample Image - TrayIcons.jpg

Introduction

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

becomes

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

becomes

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:

TraySetIcon(IDR_MAINFRAME);
TraySetToolTip("ToolTip for tray icon");
TraySetMenu(IDR_MENU1);

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.

License

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


Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalit still works Pin
Southmountain17-Sep-20 7:58
Southmountain17-Sep-20 7:58 
SuggestionSome Modifications Pin
smart_dummies28-Sep-16 21:16
smart_dummies28-Sep-16 21:16 
QuestionRe: Some Modifications Pin
Member 1296230922-Jan-17 7:11
Member 1296230922-Jan-17 7:11 
QuestionTrayShow() leads the minimize button becoming invalid Pin
Scarlettlee16-May-13 0:17
professionalScarlettlee16-May-13 0:17 
QuestionHow to do If I want it be Tray Mode while begging? Pin
vannes23-Dec-11 1:12
vannes23-Dec-11 1:12 
AnswerRe: How to do If I want it be Tray Mode while begging? Pin
EsenChang5-Aug-12 17:38
EsenChang5-Aug-12 17:38 
Questionprogramatically minimizing does not work on VS 2005 Pin
Erik17-Oct-11 19:01
Erik17-Oct-11 19:01 
QuestionCWinApp Pin
lukegorman25-Aug-11 23:52
lukegorman25-Aug-11 23:52 
AnswerRe: CWinApp Pin
lukegorman26-Aug-11 0:13
lukegorman26-Aug-11 0:13 
QuestionLicencing Pin
rainer.hochdorfer27-Apr-10 22:06
rainer.hochdorfer27-Apr-10 22:06 
AnswerRe: Licencing Pin
Daniel 200028-Apr-10 3:29
Daniel 200028-Apr-10 3:29 
AnswerRe: Licencing Pin
rainer.hochdorfer28-Apr-10 6:02
rainer.hochdorfer28-Apr-10 6:02 
GeneralThanks just perfect Pin
asierra0111-Jan-10 9:14
asierra0111-Jan-10 9:14 
GeneralRight Click Menu Pin
Shup15-May-09 1:20
Shup15-May-09 1:20 
GeneralThank you! Pin
DevMan7712-May-09 8:20
DevMan7712-May-09 8:20 
GeneralChanging tray icon while minimized Pin
eight26-Mar-09 2:21
eight26-Mar-09 2:21 
GeneralThanks!!! Pin
IAM...15-Feb-09 5:56
IAM...15-Feb-09 5:56 
QuestionOnly minimizing to tray in my application? Pin
Alexander Peter31-Oct-08 5:03
Alexander Peter31-Oct-08 5:03 
QuestionBalloon Tip?? Pin
thanasis_rassp12-Aug-08 3:16
thanasis_rassp12-Aug-08 3:16 
AnswerRe: Balloon Tip?? Pin
varandas7924-Sep-08 5:25
varandas7924-Sep-08 5:25 
GeneralBug in CTrayDialog::OnSysCommand preventing sizing Pin
EricGen24-Mar-08 23:02
EricGen24-Mar-08 23:02 
GeneralSystem get slow... Pin
Member 385006728-Feb-08 21:52
Member 385006728-Feb-08 21:52 
Generalsmall but useful demo Pin
lilesh10-Oct-07 22:33
lilesh10-Oct-07 22:33 
QuestionHow can I get my app on the tray at the very beginning? Pin
pjvv126-Sep-07 7:31
pjvv126-Sep-07 7:31 
AnswerSolution Pin
lilesh10-Oct-07 23:01
lilesh10-Oct-07 23:01 

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.