Click here to Skip to main content
12,751,904 members (29,565 online)
Click here to Skip to main content
Add your own
alternative version


80 bookmarked
Posted 4 Nov 2002

WTL Tray Icon Template

, 4 Nov 2002
Rate this:
Please Sign up or sign in to vote.
A small template allowing you to easily add system tray icon support to your WTL application
<!-- Download Links --> <!-- Add the rest of your HTML here -->


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 SetDefaultItem.

Using CTrayIconImpl

To use the CTrayIconImpl template, do the following:

Firts, include the header file:

#include "trayiconimpl"

Next. derive your main window class (usually CMainFrame for SDI/MDI apps, or CMainDlg for dialog-based apps) from CTrayIconImpl:

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 InstallIcon from 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 SetDefaultItem:

// Double-clicking the tray icon will display the "About" box

Change the tooltip text by calling SetTooltipText.


Override the 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)
    CMenuHandle menu(hMenu);
    menu.EnableMenuItem(ID_TEST_DISABLED, MF_GRAYED);
    menu.CheckMenuItem(ID_TEST_CHECKED, MF_CHECKED);


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

You may also be interested in...

Comments and Discussions

GeneralOops. Multiple About boxes Pin
DrGary23-Nov-02 15:53
sussDrGary23-Nov-02 15:53 
GeneralRe: Oops. Multiple About boxes Pin
Robert Edward Caldecott27-Nov-02 13:06
memberRobert Edward Caldecott27-Nov-02 13:06 
Generalwell done! Pin
johnthan10-Nov-02 19:11
memberjohnthan10-Nov-02 19:11 
GeneralRe: well done! Pin
Duncan Colvin24-Feb-06 8:56
memberDuncan Colvin24-Feb-06 8:56 
GeneralNicely Executed Pin
Ed Gadziemski6-Nov-02 3:45
memberEd Gadziemski6-Nov-02 3:45 
GeneralRe: Nicely Executed Pin
Robert Edward Caldecott6-Nov-02 3:54
memberRobert Edward Caldecott6-Nov-02 3:54 
GeneralRe: Nicely Executed Pin
Barry Lapthorn13-Nov-02 7:22
memberBarry Lapthorn13-Nov-02 7:22 
GeneralRe: Nicely Executed Pin
Robert Edward Caldecott13-Nov-02 7:32
memberRobert Edward Caldecott13-Nov-02 7:32 
I prefer it to MFC for many reasons. It creates much smaller EXEs with fewer dependencies, and will run on any platform (try running an MFC7 app on Win95 - you need some additional DLLs ... OLEACC.DLL I believe).

I also love template programming - using templates to extend existing classes (like this example) seems like the way forward. Coupling WTL with ATL, allowing easy COM support, is also great.

WTL itself is light ... MFC is monolithic. I have used MFC since 1994ish, and it has allowed me to write some great apps, but I think it's time has passed. My aim is to be able to do anything in WTL that I could in MFC, and so far so good.

I have written a form-editor type app with WTL that is a port of existing MFC code, yet is a THIRD of the size! I'll mail you a screen shot if you like...

All new projects I embark on will be in WTL - I have no need for MFC anymore. Part of this has also been the need to keep learning new skills, so adopting a new framework has also given me a warm fuzzy feeling inside... Wink | ;)

When I am king, you will be first against the wall.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170217.1 | Last Updated 5 Nov 2002
Article Copyright 2002 by Rob Caldecott
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid