Click here to Skip to main content
15,887,746 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 546.6K   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

 
AnswerRe: Great Class but how begin a dialogbox in hide mode?? Pin
()z26-Oct-02 3:15
suss()z26-Oct-02 3:15 
GeneralRe: Great Class but how begin a dialogbox in hide mode?? Pin
Anonymous31-Oct-02 3:53
Anonymous31-Oct-02 3:53 
QuestionHow to make Systray Menu dissappear when click outside menu Pin
16-Jul-01 6:07
suss16-Jul-01 6:07 
AnswerRe: How to make Systray Menu dissappear when click outside menu Pin
Otyug13-Jul-02 13:25
Otyug13-Jul-02 13:25 
GeneralSystem Tray Icons Pin
NT4-Jul-01 21:15
NT4-Jul-01 21:15 
GeneralMaking a derivable dialog to handle this. Pin
20-Apr-01 4:22
suss20-Apr-01 4:22 
GeneralGood Work!! Pin
8-Apr-01 3:27
suss8-Apr-01 3:27 
GeneralKeeping icon in tray when dialog is restored Pin
28-Feb-01 0:14
suss28-Feb-01 0:14 
Hallo there

Good tray class. It's been ages since I was trying to figure out how Windows implements the system tray functionality - your class helped me understand better some of the Win32 functions involved in the process.

However, I find that keeping the icon displayed in the tray all the time prevents the double-click on the app's icon in the tray from percolating on any other remaining icons. Since the system tray icons shift to the right when one goes missing, I have found that (sometimes) the second click of a double-click on a tray dialog icon (that uses this CTrayDialog class) is transmitted on the icon of the application that replaces the hidden icon. Nothing wrong with the implementation of the class itself of course, but I have modified it a bit to leave the icon displayed all the time, whether the dialog app is minimized or not. The changes are simple:

In CTrayDialog::OnSysCommand(...)
=================================

Comment the call to TrayShow() because icon is being shown always, therefore no need to re-show it when dlg is minimized.

if(m_bMinimizeToTray)
{
if ((nID & 0xFFF0) == SC_MINIMIZE)
{
//if( TrayShow())
this->ShowWindow(SW_HIDE);
}
else
CDialog::OnSysCommand(nID, lParam);
}
else
CDialog::OnSysCommand(nID, lParam);
}

In CTrayDialog::OnTrayLButtonDblClk(CPoint pt)
==============================================

Comment the call to TrayHide because icon is being shown always, therefore no need to hide it:

{
if(m_bMinimizeToTray)
// if(TrayHide())
this->ShowWindow(SW_SHOW);
}

In CMyTrayDlg::OnInitDialog(...)
================================

Uncomment the (commented) TrayShow() call towards the end of the function to show the icon as soon as the dialog app starts.


Regards
Christopher Spiteri

GeneralMinimizing Pin
23-Feb-01 4:46
suss23-Feb-01 4:46 
QuestionIs this different? Pin
7-Nov-00 3:18
suss7-Nov-00 3:18 

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.