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 :-

So that the right click menu would be dismissed if the mouse is clicked somewhere outside of there menu. But after the addition of this code, the strange thing that started to happen is that sometimes (I couldn't determine exactly when) the Windows Task Bar right click menu popes up over the application right click menu when right clicking on the application tray icon.