Click here to Skip to main content
13,803,919 members
Click here to Skip to main content
Add your own
alternative version


81 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

GeneralAnother WTL code snippet to easily minimize into a tray icon Pin
ryltsov21-Jan-09 0:58
memberryltsov21-Jan-09 0:58 
GeneralRe: Another WTL code snippet to easily minimize into a tray icon Pin
Peter Mares25-Sep-10 1:06
memberPeter Mares25-Sep-10 1:06 
QuestionCan you change the icon Pin
Angus Comber17-Feb-07 7:08
memberAngus Comber17-Feb-07 7:08 
AnswerRe: Can you change the icon Pin
MaxZ23-Apr-07 6:42
memberMaxZ23-Apr-07 6:42 
Questionexit can't remove tray icon Pin
Alan Black8-Feb-07 21:05
memberAlan Black8-Feb-07 21:05 
QuestionNeeds a better right button down handler? Pin
Brandon Jen17-Oct-06 13:48
memberBrandon Jen17-Oct-06 13:48 
AnswerRe: Needs a better right button down handler? Pin
Rob Caldecott17-Oct-06 14:28
memberRob Caldecott17-Oct-06 14:28 
GeneralReally nice! Pin
Jörgen Sigvardsson24-Aug-06 14:42
memberJörgen Sigvardsson24-Aug-06 14:42 
GeneralRe: Really nice! Pin
Rob Caldecott25-Aug-06 0:16
memberRob Caldecott25-Aug-06 0:16 
GeneralWTL versus MFC Pin
BruteBertus19-Feb-06 1:31
memberBruteBertus19-Feb-06 1:31 
QuestionHow do I get rid of the Taskbar button? Pin
Peeyush_kota1-Apr-05 19:10
memberPeeyush_kota1-Apr-05 19:10 
AnswerRe: How do I get rid of the Taskbar button? Pin
no_way18-Mar-07 11:11
memberno_way18-Mar-07 11:11 
Generalerror Pin
GameServer19-Dec-04 16:41
memberGameServer19-Dec-04 16:41 
GeneralRe: error Pin
Rob Caldecott19-Dec-04 22:35
memberRob Caldecott19-Dec-04 22:35 
GeneralRe: error Pin
WiNrOOt[FCG]5-Nov-06 16:59
memberWiNrOOt[FCG]5-Nov-06 16:59 
GeneralRe: error Pin
Yogesh Dhakad27-Apr-07 10:32
memberYogesh Dhakad27-Apr-07 10:32 
GeneralSome corner cases Pin
troff23-Sep-04 4:59
membertroff23-Sep-04 4:59 
GeneralRe: Some corner cases Pin
Rob Caldecott23-Sep-04 5:18
memberRob Caldecott23-Sep-04 5:18 
GeneralRe: Some corner cases Pin
troff28-Sep-04 5:56
membertroff28-Sep-04 5:56 
QuestionHow did you make the XP button style? Pin
DrGary27-Nov-02 11:38
memberDrGary27-Nov-02 11:38 
AnswerRe: How did you make the XP button style? Pin
Rob Caldecott27-Nov-02 13:04
memberRob Caldecott27-Nov-02 13:04 
GeneralOops. Multiple About boxes Pin
DrGary23-Nov-02 15:53
memberDrGary23-Nov-02 15:53 
GeneralRe: Oops. Multiple About boxes Pin
Rob Caldecott27-Nov-02 13:06
memberRob Caldecott27-Nov-02 13:06 
Generalwell done! Pin
ChauJohnthan10-Nov-02 19:11
memberChauJohnthan10-Nov-02 19:11 
GeneralRe: well done! Pin
Duncan Colvin24-Feb-06 8:56
memberDuncan Colvin24-Feb-06 8:56 

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 | Cookies | Terms of Use | Mobile
Web02 | 2.8.181218.1 | Last Updated 5 Nov 2002
Article Copyright 2002 by Rob Caldecott
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid