This is a small template that you can use to add system tray icon support to your WTL based application. A big nod in the direction of Chris Maunder is due, as back in my MFC days I used his excellent
CSystemTray class, which was the inspiration for this WTL version (though his adds much more functionality).
This template can be used to add "default" tray icon behaviour to your application. A menu is displayed when you right-click the icon, and double-clicking the icon will execute the default menu item. Note that the first menu item will be used as the default, though you can change this by calling
To use the
CTrayIconImpl template, do the following:
Firts, include the header file:
Next. derive your main window class (usually
CMainFrame for SDI/MDI apps, or
CMainDlg for dialog-based apps) from
class CMainDlg :
Next (and this is important) add a
CHAIN_MSG_MAP entry to your windows message map (to ensure that the WM_TRAYICON message is processed correctly):
To install an icon in the system tray, call
OnCreate (SDI/MDI apps) or
OnInitDialog (dialog apps). Note that you supply three parameters to this call - the tooltip text, the icon handle and the resource ID of the popup menu to display when the tray icon is right-clicked.
// Load a small icon
HICON hIconSmall = (HICON)::LoadImage(_Module.GetResourceInstance(),
// Install tray icon
InstallIcon(_T("Tooltip text"), hIconSmall, IDR_POPUP);
Finally, add the necessary
COMMAND_ID_HANDLERs for your popup menu commands. That's it!
Change the default menu item by calling
Change the tooltip text by calling
void PrepareMenu(HMENU hMenu) function in order to initialize the popup menu before it is displayed. For example, you may want to disable items, check items, etc. (see the WTLTrayIconWindow example for a demonstration).
void PrepareMenu(HMENU hMenu)