Click here to Skip to main content
15,867,686 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 542.5K   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

 
QuestionItem1=ShowWindow? Pin
al.kaholik30-Jan-06 4:12
al.kaholik30-Jan-06 4:12 
GeneralIssue With CE Pin
oilos16-Aug-05 1:23
oilos16-Aug-05 1:23 
GeneralProblem Pin
harpreetsbamrah23-Jun-05 20:24
harpreetsbamrah23-Jun-05 20:24 
GeneralRe: Problem Pin
harpreetsbamrah23-Jun-05 20:44
harpreetsbamrah23-Jun-05 20:44 
Generalsorry Pin
sicks18-Jun-05 6:42
sicks18-Jun-05 6:42 
Generalproblem Pin
18-Jun-05 6:29
suss18-Jun-05 6:29 
GeneralRe: problem Pin
Alegg15-Jun-06 5:36
Alegg15-Jun-06 5:36 
GeneralThankYou !! Modification !! Pin
harpreetsbamrah9-Jun-05 22:58
harpreetsbamrah9-Jun-05 22:58 
Thanks Daniel for such an easy to use class and that too bug free.Only thing was that menu was not disappearing ,but somebody posted that too. For my specific requirement i needed the pointer to the CMenu for the icon for that i added the following and my requirement was met .
CMenu* CTrayDialog::TrayGetMenu()
{

return m_mnuTrayMenu.GetSubMenu(0);
}
I am using your class in my application and its working seamlessly .Smile | :)
thanx once more
GeneralMenu will not disapper Pin
_bko22-May-05 23:43
_bko22-May-05 23:43 
JokeRe: Menu will not disapper Pin
Harry Lau6-Apr-06 4:33
Harry Lau6-Apr-06 4:33 
GeneralRe: Menu will not disapper Pin
fherlan2-Dec-06 5:42
fherlan2-Dec-06 5:42 
GeneralThanks Daniel ! Pin
Ganesh_NV18-Apr-05 19:53
Ganesh_NV18-Apr-05 19:53 
GeneralSome bugs Pin
rrrado7-Mar-05 0:43
rrrado7-Mar-05 0:43 
GeneralRe: Some bugs Pin
rrrado7-Mar-05 2:26
rrrado7-Mar-05 2:26 
GeneralGreat job -- small improvement Pin
Hard Hat23-Feb-05 1:21
Hard Hat23-Feb-05 1:21 
GeneralTerminating from TaskManager Pin
Alan Buchanan23-Feb-05 0:02
Alan Buchanan23-Feb-05 0:02 
GeneralRe: Terminating from TaskManager Pin
Anonymous23-Feb-05 14:36
Anonymous23-Feb-05 14:36 
GeneralRe: Terminating from TaskManager Pin
Anonymous18-Apr-05 20:45
Anonymous18-Apr-05 20:45 
GeneralRe: Terminating from TaskManager Pin
TaLF25-Jun-05 13:17
TaLF25-Jun-05 13:17 
GeneralRe: Solution - Terminating from TaskManager Pin
IVarshilov24-May-09 22:53
IVarshilov24-May-09 22:53 
GeneralStart Dialog Minimized to SysTray Pin
codehar13-Feb-05 19:04
codehar13-Feb-05 19:04 
GeneralRe: Start Dialog Minimized to SysTray Pin
mysticwolf9-Mar-05 6:18
mysticwolf9-Mar-05 6:18 
GeneralRe: Start Dialog Minimized to SysTray Pin
bubis14-Sep-05 2:57
bubis14-Sep-05 2:57 
AnswerRe: Start Dialog Minimized to SysTray Pin
CodeWatcher27-Oct-05 13:28
CodeWatcher27-Oct-05 13:28 
GeneralRe: Start Dialog Minimized to SysTray Pin
binyo6618-May-09 16:55
binyo6618-May-09 16:55 

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.