I took up a project to create a simple Stopwatch. The aim of the project was to keep a track of the time I spend on various projects. I wanted a very simple application which was able to Start Pause and Stop a Stopwatch.

The best approach that I thought up was through a System Tray application. This would give me easy access to the controls and display, without interfering with my open windows. As I am trying to have a go at C++ and win32, I decided to implement it using Visual C++ 2008 express edition and win32 API.

So without any further delay, here are the basic steps needed to create an icon in the system tray.

  1. Include the “shellapi.h” header file, and link to “shell32.lib” library (Visual C++ links to it by default.
  2. Define a custom message identifier for our icon. Whenever there is a mouse or keyboard event on our icon, windows will use this identifer to send messages to our message procedure.
  3. Create and initialize the NOTIFYICONDATA structure according to our needs.
  4. Invoke Shell_NotifyIcon() API, with a pointer to NOTIFYICONDATA, to create, modify or delete our icon.

To demonstrate the steps let us consider this example. For our application, we would like to have an icon in the system tray. It should display a message box whenever it is double clicked and ‘Tray Icon’ whenever the mouse hovers on top of it.

To begin with we need to include ‘shellapi.h’ in our application.

#include <shellapi.h>

The next step would be to define a custom message identifier. Whenever a mouse event occurs on our icon, Windows will send this custom message to our message procedure. The lParam variable will have the actual mouse or keyboard event and wParam will contain the iconid.

#define WM_MYMESSAGE (WM_USER + 1)

Now we should create a variable of type NOTIFYICONDATA and populate it as per our icon’s need. I will first show the example, before explaining the structure further.

NOTIFYICONDATA nid;

nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = hWnd;
nid.uID = 100;
nid.uVersion = NOTIFYICON_VERSION;
nid.uCallbackMessage = WM_MYMESSAGE;
nid.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcscpy_s(nid.szTip, L"Tray Icon");
nid.uFlags = NIF_MESSAGE NIF_ICON NIF_TIP;

Shell_NotifyIcon(NIM_ADD, &nid);

cbSize : It indicates the size of our structure.

hWnd : It takes the handle to our window which will process the messages from our icon.

uID : This is a unique Id for our icon. Shell uses hWnd + uID to identify which icon to operate on when Shell_NotifyIcon() is invoked.

uVersion : It defines the behavior of our icon, i.e. whether it should behave like a windows 98 icon, a windows xp icon or windows Vista icon. Based on the value of this variable, certain other variables of the NOTIFYICONDATA become available to us. (refer to MSDN for more info).

uCallBackMessage : This indicates the message to be sent to the message procedure whenever a mouse event occurs on our icon.

hIcon : This stores the handle to the Icon to be used for display in the system tray. Any icon that is available to our application can be used, here I have used the system defined icon.

szTip : Here we can store a null terminated string which will be displayed near the icon, whenever the mouse is on it. The type of variable depends on your language settings, I am using the unicode version in the example.

uFlags : This variable defines whether the variables are uCallbackMessage, hIcon and szTip are valid or not. This is achieved by bit masks NIF_MESSAGE, NIF_ICON, NIF_TIP.

Once we have defined our NOTIFYICONDATA structure, we should invoke Shell_NotifyIcon() as follows.

Shell_NotifyIcon(NIM_ADD, &nid);

This will create and display our icon in system tray. As we can see Shell_NotifyIcon takes two parameters.

  1. dwMessage : This could be NIM_ADD, NIM_MODIFY or NIM_DELETE.
  2. lpdata : A pointer to our NOTIFYICONDATA variable, which defines our icon.

The last thing that we need to do is capture and process the WM_MYMESSAGE message in our message procedure. Consider a typical message handler procedure as follows.

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
    case WM_CREATE:
         break;
    ....
    ....
    case WM_MYMESSAGE:
         switch(lParam)
         {
         case WM_LBUTTONDBLCLK:
                 MessageBox(NULL, L"Tray icon double clicked!", L"clicked", MB_OK);
                 break;
         default:
                return DefWindowProc(hWnd, msg, wParam, lParam);
         };
         break;
     .....
     .....
     default:
           return DefWindowProc(hWnd, msg, wParam, lParam);
     };
     return 0;
 }

Here you can see that we are trapping our WM_MYMESSAGE and then checking lParam to see what mouse event occured. As described in our example, we wanted to show a messagebox whenever our icon is double clicked. Thus we are processing the WM_LBUTTONDBLCLK message from the lParam variable. We can process other messages also, as per the application’s requirements. For example, we can process WM_LBUTTONUP to do something whenever a user clicks on it etc.

In this way we can create, display and interact with our System Tray Icon.

To delete the icon whenever we are done with it, for example when our program is exiting we should define the NOTIFYDATAICON to point to our icon and call Shell_NotifyIcon with NIM_DELETE message. For example, to delete the icon created above we need to define.

NOTIFYICONDATA nid;
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = hWnd;
nid.uID = 100;
Shell_NotifyIcon(NIM_DELETE, &nid);

We don’t have to define any other member of the NOTIFYICONDATA structure.

Similarly to modify the icon we should define the NOTIFYICONDATA and call Shell_NotifyIcon with NIM_MODIFY. For example to modify the icon tip.

NOTIFYICONDATA nid;
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = hWnd;
nid.uID = 100;
wcscpy_s(nid.szTip, L"Modified Tip");
nid.uFlags = NIF_TIP;
Shell_NotifyIcon(NIM_MODIFY, &nid);

We can combine NIF_TIP and NIF_ICON to modify the tip and icon together.

Well thats about it regarding the system tray icons and how to use them.

A note regarding WM_MYMESSAGE :
As you can see I defined WM_MYMESSAGE as WM_USER + 1, this is because, all the message numbers below 1024(0x0400) are reserved by Windows. If you checkout WinUser.h file, you will see that WM_USER has been defined as 0x0400 (1024), thus we can base our custom messages starting with this value.

Advertisements