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.

I've added a simple timer to switch the tray icon every 5 second and it doesn't seem to work while minimized. To have the tray icon changed, I have to restore the dialog and re-minimize it back. Any idea on how I can get the tray icon to change while the dialog is minimized?

I just downloaded the sample, followed the instructions in a new sample project, everything ok.

When I added TrayDialog to my current dialog based project in the same way, the only thing what happens is, that the program minimizes to the tray. There's no menu, no reaction on left double click. The only thing shown is the tooltip. I have to kill the application with the task manager...

There's no more CDialog reference in my source. It's nearly 99% the same code in OnInitDialog, message maps etc.

i looked into in but i didn't get reason why SW_HIDE doesn't work OnInit
May be the reason is that window hasn't yet created ..(i don't know the exact reason)
but, i have another solution , you can create your own message handler and postmessage to it, i have written code here just go through it.

hello;
is there a way to creet the systrayicon without creeing a window nor a dialogue? my application does not need any viewing just a tray icon with 2 items: go and stop. it is a great consummer of resources and i don't want to use some resource for nothing

In VC++ 2003, you must use "LRESULT CTrayDialog::OnTrayNotify(WPARAM wParam, LPARAM lParam)" than "void CTrayDialog::OnTrayNotify(WPARAM wParam, LPARAM lParam)", and just return 0 in this function, then your program's still best.
Thanks for your best code.

Nvm I got it, you have to replace every instance of CDialog in the MyTrayDiag.cpp file with CTrayDialog. Just do a replace run through the file.
Ugh that took me near half a day reading online and getting nowhere, hopefully this will save some anyone run into the same problem in the future.

hello, I have a different problem at trayicon, when I'm doing something, I try to R-click my trayicon, it should be pop my write menu,but it's not. it's pop windows menu(sorry, I don't know what is it name), can you help how to avoid the windows menu,thanks, have a nice day.