Click here to Skip to main content
6,585,487 members and growing! (26,387 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » WTL » Shell Programming     Intermediate

WTL Tray Icon Template

By Rob Caldecott

A small template allowing you to easily add system tray icon support to your WTL application
VC6, VC7Win2K, WinXP, WTL, Dev
Posted:4 Nov 2002
Views:90,318
Bookmarked:58 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
31 votes for this article.
Popularity: 7.09 Rating: 4.75 out of 5
1 vote, 4.3%
1

2
2 votes, 8.7%
3
1 vote, 4.3%
4
19 votes, 82.6%
5

Introduction

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 :
    ...
    CTrayIconImpl<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):

BEGIN_MSG_MAP(CMainDlg)
    ...
    CHAIN_MSG_MAP(CTrayIconImpl<CMainDlg>)
END_MSG_MAP()

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(), 
                                      MAKEINTRESOURCE(IDR_MAINFRAME),
                                      IMAGE_ICON, 
                                      ::GetSystemMetrics(SM_CXSMICON), 
                                      ::GetSystemMetrics(SM_CYSMICON), 
                                      LR_DEFAULTCOLOR);
...
// 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!

Notes

Change the default menu item by calling SetDefaultItem:

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

SetDefaultItem(ID_APP_ABOUT);

Change the tooltip text by calling SetTooltipText.

SetTooltipText(_T("Yeeha!"));

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);
}

License

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

Rob Caldecott


Member

Occupation: Architect
Location: United Kingdom United Kingdom

Other popular WTL articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 28 (Total in Forum: 28) (Refresh)FirstPrevNext
GeneralAnother WTL code snippet to easily minimize into a tray icon Pinmemberryltsov0:58 21 Jan '09  
GeneralCan you change the icon PinmemberAngus Comber7:08 17 Feb '07  
GeneralRe: Can you change the icon PinmemberMaxZ6:42 23 Apr '07  
Questionexit can't remove tray icon PinmemberAlan Black21:05 8 Feb '07  
GeneralNeeds a better right button down handler? PinmemberBrandon Jen13:48 17 Oct '06  
GeneralRe: Needs a better right button down handler? PinmemberRob Caldecott14:28 17 Oct '06  
GeneralReally nice! PinmemberJörgen Sigvardsson14:42 24 Aug '06  
GeneralRe: Really nice! PinmemberRobert Edward Caldecott0:16 25 Aug '06  
GeneralWTL versus MFC PinmemberBruteBertus1:31 19 Feb '06  
GeneralHow do I get rid of the Taskbar button? PinmemberPeeyush_kota19:10 1 Apr '05  
GeneralRe: How do I get rid of the Taskbar button? Pinmemberno_way11:11 18 Mar '07  
Generalerror Pinmemberlxwq33716:41 19 Dec '04  
GeneralRe: error PinmemberRobert Edward Caldecott22:35 19 Dec '04  
GeneralRe: error PinmemberWiNrOOt[FCG]16:59 5 Nov '06  
GeneralRe: error PinmemberYogesh P. Dhakad10:32 27 Apr '07  
GeneralSome corner cases Pinmembertroff4:59 23 Sep '04  
GeneralRe: Some corner cases PinmemberRobert Edward Caldecott5:18 23 Sep '04  
GeneralRe: Some corner cases Pinmembertroff5:56 28 Sep '04  
GeneralHow did you make the XP button style? PinsussDrGary11:38 27 Nov '02  
GeneralRe: How did you make the XP button style? PinmemberRobert Edward Caldecott13:04 27 Nov '02  
GeneralOops. Multiple About boxes PinsussDrGary15:53 23 Nov '02  
GeneralRe: Oops. Multiple About boxes PinmemberRobert Edward Caldecott13:06 27 Nov '02  
Generalwell done! Pinmemberjohnthan19:11 10 Nov '02  
GeneralRe: well done! PinmemberDuncan Colvin8:56 24 Feb '06  
GeneralNicely Executed PinmemberEd Gadziemski3:45 6 Nov '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 4 Nov 2002
Editor: Chris Maunder
Copyright 2002 by Rob Caldecott
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project