Objective Toolkit : Chapter 15 User Interface Extensions : Tray Icon Class
Tray Icon Class
The SECTrayIcon class helps you create applications that are invoked via the Windows tray interface. Animated icons are supported. A System Tray Icon is a user interface metaphor that was introduced by Windows 95. If you look at the rightmost edge of the Windows 95 taskbar, there is a small "tray" of application icons and a system clock.
SECTrayIcon provides your application with an easy-to-use mechanism for adding your own custom icons to the system tray and providing user interface feedback such as tooltip text, context menu support, and animated icons.
Figure 127 – Objective Toolkit Tray Icon Class Hierarchy
To incorporate the Tray Icon Class into your application
1. In your main window class (main frame or dialog), add an SECTrayIcon data member for each tray icon you want to display. For example:
 
SECTrayIcon m_TrayApp;
2. In the initialization code for your main window class, create the tray icon by calling the Create() method.
 
m_TrayApp.Create(this);
3. In the resource editor, add an icon resource and then set the icon with the SetIcon() method.
 
m_TrayApp.SetIcon(IDI_STINGRAY);
4. Optionally, set tooltip text with the SetTip() method.
 
m_TrayApp.SetTip(_T("Stingray Tray Icon Demo"));
5. Display the tray icon with the Show() method.
 
m_TrayApp.Show(TRUE);
The SECTrayIcon destructor automatically cleans up the icon; however, you can still issue a Destroy() call directly.
To add notification handlers for mouse events
The following steps set up a notification handler to display a context menu in response to a right-click.
1. Complete the steps in “To incorporate the Tray Icon Class into your application”.
2. For each tray icon, create a unique notification ID or use the GetNextNotifyID() method to generate one on the fly.
 
m_TrayNotifyId=SECTrayIcon::GetNextNotifyID();
 
Add the notification ID as the second parameter to the Create() method call.
 
m_TrayApp.Create(this, m_TrayNotifyId);
3. Add the WM_SEC_TRAYICON_NOTIFY message using the ON_MESSAGE macro in the message map. The tray icon sends notifications via this user message. You can also specify your own message ID as an optional parameter to the SECTrayIcon::Create() method call.
ON_MESSAGE(WM_SEC_TRAYICON_NOTIFY,
OnTrayIconNotify)
Add a message handler for this message. The wParam contains the notification ID passed to the Create() method. The lParam contains the mouse message. For example:
 
LRESULT CTrayIconDlg::OnTrayIconNotify(WPARAM wParam, LPARAM lParam)
{
// Use the wParam to identify which tray icon
if(wParam==m_TrayNotifyId) {
 
// lParam identifies the mouse message
switch(lParam) {
case WM_MOUSEMOVE:
// mousemove is a good place to
// change tooltip text – for example
// if you were displaying time of day
 
case WM_RBUTTONUP:
// the SECTrayIcon::ShowContextMenu
// static member makes it a snap to
// display popup menus—-all the
// positioning and message routing
// is done automatically for you.
SECTrayIcon::ShowContextMenu(this,
IDR_MENU_NOTIFY);
break;
 
case WM_LBUTTONDBLCLK:
AfxMessageBox(
_T("You just double clicked me!"));
break;
}
}
return 0L;
}
To animate a tray icon
1. Complete the steps in “To incorporate the Tray Icon Class into your application”.
2. In the resource editor, add icons for each frame of the animation.
3. After the SECTrayIcon::Create() method call, call the AddState() method and specify a unique ID for each state, the icon to display, the tooltip to display, and a frame delay time. The default time delay is 255 milliseconds. If you need to specify a different time delay, multiply the delay parameter by 17 to determine the time in milliseconds.
 
m_tray.AddState(IDI_SCROLL_ON0, IDI_SCROLL_ON0,
strOn, nFrameDelay);
m_tray.AddState(IDI_SCROLL_ON1, IDI_SCROLL_ON1,
strOn, nFrameDelay);
m_tray.AddState(IDI_SCROLL_ON2, IDI_SCROLL_ON2,
strOn, nFrameDelay);
m_tray.AddState(IDI_SCROLL_ON3, IDI_SCROLL_ON3,
strOn, nFrameDelay);
m_tray.AddState(IDI_SCROLL_ON4, IDI_SCROLL_ON4,
strOn, nFrameDelay);
m_tray.AddState(IDI_SCROLL_ON5, IDI_SCROLL_ON5,
strOn, nFrameDelay);
4. After the call to the Show() method, call the Play() method to start the animation and the Stop() to halt the animation.
 
m_TrayAnimated.Play(IDI_SCROLL_ON0,6);
Tray Icon Sample
Refer to the trayicon sample (\Samples\Toolkit\MFC\UIExt\TrayIcon) for more information. Animations are also demonstrated in this sample. This sample does not ship with the product. For information on how to obtain this sample, see “Location of Sample Code” in the Getting Started part.