Click here to Skip to main content
11,489,693 members (69,274 online)
Click here to Skip to main content

System Tray Icons - Minimize Your Application to the SysTray

, 6 Nov 2000 CPOL 418.6K 13K 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 -->

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)

Share

About the Author

Daniel Zilcsak
Software Developer
United States United States
No Biography provided

Comments and Discussions

 
QuestionTrayShow() leads the minimize button becoming invalid Pin
Scarlettlee16-May-13 1:17
memberScarlettlee16-May-13 1:17 
QuestionHow to do If I want it be Tray Mode while begging? Pin
vannes23-Dec-11 2:12
membervannes23-Dec-11 2:12 
AnswerRe: How to do If I want it be Tray Mode while begging? Pin
EsenChang5-Aug-12 18:38
memberEsenChang5-Aug-12 18:38 
Questionprogramatically minimizing does not work on VS 2005 Pin
Erik17-Oct-11 20:01
memberErik17-Oct-11 20:01 
QuestionCWinApp Pin
Member 806538826-Aug-11 0:52
memberMember 806538826-Aug-11 0:52 
AnswerRe: CWinApp Pin
Member 806538826-Aug-11 1:13
memberMember 806538826-Aug-11 1:13 
QuestionLicencing Pin
rainer.hochdorfer27-Apr-10 23:06
memberrainer.hochdorfer27-Apr-10 23:06 
AnswerRe: Licencing Pin
Daniel Zilcsak28-Apr-10 4:29
memberDaniel Zilcsak28-Apr-10 4:29 
AnswerRe: Licencing Pin
rainer.hochdorfer28-Apr-10 7:02
memberrainer.hochdorfer28-Apr-10 7:02 
GeneralThanks just perfect Pin
asierra0111-Jan-10 10:14
memberasierra0111-Jan-10 10:14 
GeneralRight Click Menu Pin
Shup15-May-09 2:20
memberShup15-May-09 2:20 
GeneralThank you! Pin
DevMan7712-May-09 9:20
memberDevMan7712-May-09 9:20 
GeneralChanging tray icon while minimized Pin
eight26-Mar-09 3:21
membereight26-Mar-09 3:21 
GeneralThanks!!! Pin
IAM...15-Feb-09 6:56
memberIAM...15-Feb-09 6:56 
QuestionOnly minimizing to tray in my application? Pin
Member 192119231-Oct-08 6:03
memberMember 192119231-Oct-08 6:03 
QuestionBalloon Tip?? Pin
thanasis_rassp12-Aug-08 4:16
memberthanasis_rassp12-Aug-08 4:16 
AnswerRe: Balloon Tip?? Pin
varandas7924-Sep-08 6:25
membervarandas7924-Sep-08 6:25 
GeneralBug in CTrayDialog::OnSysCommand preventing sizing Pin
EricGen25-Mar-08 0:02
memberEricGen25-Mar-08 0:02 
GeneralSystem get slow... Pin
Member 385006728-Feb-08 22:52
memberMember 385006728-Feb-08 22:52 
Generalsmall but useful demo Pin
lilesh10-Oct-07 23:33
memberlilesh10-Oct-07 23:33 
QuestionHow can I get my app on the tray at the very beginning? Pin
pjvv126-Sep-07 8:31
memberpjvv126-Sep-07 8:31 
AnswerSolution Pin
lilesh11-Oct-07 0:01
memberlilesh11-Oct-07 0:01 
Questionsystray without window Pin
chergui3-Apr-07 2:36
memberchergui3-Apr-07 2:36 
GeneralIn VC++ 2003, there's error Pin
tlbf10-Jan-07 3:46
membertlbf10-Jan-07 3:46 
GeneralRe: In VC++ 2003, there's error Pin
child00728-Jan-08 18:32
memberchild00728-Jan-08 18:32 

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