Click here to Skip to main content
Click here to Skip to main content

WTL Tray Icon Template

By , 4 Nov 2002
 

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
Architect
United Kingdom United Kingdom
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralAnother WTL code snippet to easily minimize into a tray iconmemberryltsov20 Jan '09 - 23:58 
A related post on alax.info - The easiest yet user-friendly WTL way to Minimize Application to System Tray Icon[^]. No thrills tray icon which also reuses standard window menu as a tray icon popup menu.
 
modified on Wednesday, January 21, 2009 6:06 AM

GeneralRe: Another WTL code snippet to easily minimize into a tray iconmemberPeter Mares25 Sep '10 - 0:06 
Thanks for the share! Unfortunately, it does not actually provide an encapsulation for the tray icon so its not very 're-usable' outside of copy/paste.
Anyway, atleast its nice to see the 'guts' of code sometimes Smile | :)


All good things were meant to be improved

QuestionCan you change the iconmemberAngus Comber17 Feb '07 - 6:08 
Actually I am thinking of having a red icon and a gree icon. when the userl ogs in I will change icon colour from red to green. Would that be possible? how?
 

AnswerRe: Can you change the iconmemberMaxZ23 Apr '07 - 5:42 
just add:
 
bool ChangeIcon(HICON hIcon)
{
ATLASSERT(hIcon);
//Call the Shell_NotifyIcon function
m_nid.uFlags = NIF_ICON;
m_nid.hIcon = hIcon;
return Shell_NotifyIcon(NIM_MODIFY,&m_nid);
}
bool ChangeIcon(UINT nIDResource)
{
HICON hIcon = (HICON)::LoadImage(_Module.GetResourceInstance(),
MAKEINTRESOURCE(nIDResource),
IMAGE_ICON,
::GetSystemMetrics(SM_CXSMICON),
::GetSystemMetrics(SM_CYSMICON),
LR_DEFAULTCOLOR);

bool bSuccess = ChangeIcon(hIcon);
DestroyIcon(hIcon);
return bSuccess;
}
Questionexit can't remove tray iconmemberAlan Black8 Feb '07 - 20:05 
When I add a message box on the exit routine, the program can't exit properly, the tray icon won't disappear.
 
void CMainDlg::CloseDialog(int nVal)
{
// Uncomment these two lines the program can't exit properly.
// if (IDNO == MessageBox(_T("exit?"), _T(""), MB_YESNO | MB_ICONWARNING))
// return ;
 
DestroyWindow();
::PostQuitMessage(nVal);
}
 
.net
mfc

QuestionNeeds a better right button down handler?memberBrandon Jen17 Oct '06 - 12:48 
If you right-click down on the icon and release the right button somewhere else (say... over the taskbar) both menus appear (one for the icon and the other for the taskbar). Any ideas on how to solve this problem?
AnswerRe: Needs a better right button down handler?memberRob Caldecott17 Oct '06 - 13:28 
No ideas - I also see this with other taskbar icons (even MS ones like "Safely remove hardware", etc.) so it looks like a general issue. I'll chew it over when I get a moment. Smile | :)
 

Kicking squealing Gucci little piggy.

GeneralReally nice!memberJörgen Sigvardsson24 Aug '06 - 13:42 
I used your code in an article of mine: http://www.codeproject.com/useritems/kdemouse.asp[^]. It was ridiculously easy to use, and is despite its easiness quite powerful! (The two concepts are sadly often mutually exclusive)
 
Thanks a bunch! Smile | :)
 
--
A Stern Warning of Things to Come
GeneralRe: Really nice!memberRobert Edward Caldecott24 Aug '06 - 23:16 
Thanks Jörgen! Smile | :)
GeneralWTL versus MFCmemberBruteBertus19 Feb '06 - 0:31 
i think you should remove the virtual before the declaration of PrepareMenu in trayiconimpl.h
since unlike MFC, WTL uses templates which specify the derived class.. .
->because you use pT->PrepareMenu with pT of the right class-type (T*), no virtual is needed..
 
except this i like your code/article, keep up the good work.Big Grin | :-D
 
bert.
 

QuestionHow do I get rid of the Taskbar button?memberPeeyush_kota1 Apr '05 - 18:10 
Thanks for the great sample.
I want to hide the taskbar button also when the dialog is sent to system tray, how do I do that?
AnswerRe: How do I get rid of the Taskbar button?memberno_way18 Mar '07 - 10:11 
You have to catch the message that minimizes the app, and modify its behaviour to do ShowWindow(SW_HIDE) instead of the default ShowWindow(SW_MINIMIZE)
 
MESSAGE_HANDLER(WM_SYSCOMMAND, OnSysCommand)
 
LRESULT CMainDlg::OnSysCommand(UINT uMsg, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled) {
bHandled = FALSE;
if(wParam == SC_MINIMIZE) {
ShowWindow(SW_HIDE);
bHandled = TRUE;
}
return TRUE;
}

Generalerrormemberlxwq33719 Dec '04 - 15:41 
Linking...
CVTRES : fatal error CVT1100: duplicate resource -- type:24, name:1, language:0x0409
.\Debug\WTLTrayIconWindow.res : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt
Error executing link.exe.
 
WTLTrayIconWindow.exe - 2 error(s), 0 warning(s)
 
I'm a Progream Boy,I Love WTL!!!
GeneralRe: errormemberRobert Edward Caldecott19 Dec '04 - 21:35 
Are you using VS7? Looks like there are two manifest resources - the old VS6 version and the proper VS7 one. One needs to be removed - you could do this using the resource editor.
 
I will update the project and upload it again ASAP.

 

The Rob Blog
GeneralRe: errormemberWiNrOOt[FCG]5 Nov '06 - 15:59 
http://www.developer.com/img/2006/10/Nick4.gif[^]
GeneralRe: errormemberYogesh P. Dhakad27 Apr '07 - 9:32 
The solution described below seems to be an appropriate solution;
http://www.codeproject.com/wtl/wtlwindowfont.asp?df=100&forumid=13440&exp=0&select=824475&mpp=50#xx824475xx[^]
 
Regards,
Yogesh Dhakad
GeneralSome corner casesmembertroff23 Sep '04 - 3:59 
Maybe I'm mistaken, but it seems to me that you will run into trouble if you run more than one application using the trayicon code since you are registering the same window message.
 
I also think you should handle the TaskbarCreated message.
GeneralRe: Some corner casesmemberRobert Edward Caldecott23 Sep '04 - 4:18 
Sharing the same message value isn't a problem - the tray icon code sends this message direct to each window, so it doesn't matter what it is as long is it is unique to the system (hence I use RegisterWindowMessage).
 
TaskbarCreated - yes, I should do something with this...
 

The Rob Blog
GeneralRe: Some corner casesmembertroff28 Sep '04 - 4:56 
Ah, yes, you're right of course. I guess multiple tray icons per window is a bit exotic, which is what would be needed to get you into trouble.
QuestionHow did you make the XP button style?sussDrGary27 Nov '02 - 10:38 
The author has done something unusual in the demo code. The buttons on the main dialog and about box have a rounded, XP style. I didn't see any code to cause this change to the default dialog code created by the VC+++ WTL dialog app wizard. Nor could I find any property or resource to set this style.
 
How was it done?
 

AnswerRe: How did you make the XP button style?memberRobert Edward Caldecott27 Nov '02 - 12:04 
Check the .rc file - there is a manifest resource which will give you the funky new style when run on XP. The standard WTL VC6 wizard assumes that you will ship the manifest as a separate file, whereas I prefer to embed it in the resource:
 
1. Add a new "Custom" resource, type "24".
2. Give the resource an ID of 1.
3. Paste in the manifest file.
4. You can have the entire manifest XML embedded in the .RC or have it saved as a file.
 
Open the .rc file in a text editor to see this:
 
1 24 DISCARDABLE "res\\WTLTrayIconDialog.exe.manifest"
 
VS7 supports the manifest properly (replacing 24 with RT_MANIFEST).
 

When I am king, you will be first against the wall.
GeneralOops. Multiple About boxessussDrGary23 Nov '02 - 14:53 
In the example dialog application, if you hit the 'About...' button, the About dialog appears modally--you can't click any buttons in the main dialog until you close the About dialog. However, while the modal About dialog is shown, you can still access the tray icon popup menu. In fact, you can select 'About...' from the menu repeatedly and create multiple modal About dialogs.
 
As the original article above points out, you can disable items on the popup menu, so the problem can be fixed easily by disabling the 'About...' item when the About dialog is displayed.
 
But the popup menu still appears with other items during the modal About dialog. What's the proper interface guideline here? Should the Icon Tray menu be unavailable during a modal dialog? Should it appear, but with all of its items disabled?
 
Thanks for a nice piece of code, BTW.
 

 

GeneralRe: Oops. Multiple About boxesmemberRobert Edward Caldecott27 Nov '02 - 12:06 
Looking at other apps that use a tray icon, all menu options will remain enabled, but if the "About" dialog is displayed, then the existing dialog will be activated instead of a new instance being created. I'll leave this as an exercise for the user (not hard!).

 

When I am king, you will be first against the wall.
Generalwell done!memberjohnthan10 Nov '02 - 18:11 
I can use it in 30secs Smile | :)
GeneralRe: well done!memberDuncan Colvin24 Feb '06 - 7:56 
26 seconds Poke tongue | ;-P
GeneralNicely ExecutedmemberEd Gadziemski6 Nov '02 - 2:45 
While I personally despise tray icons, I feel you have done a good job on the execution of this one for WTL. The code works as advertised and the article is clearly written.
GeneralRe: Nicely ExecutedmemberRobert Edward Caldecott6 Nov '02 - 2:54 
Thanks Ed! Much appreciated. I have a lot more articles planned - mainly small classes/templates like this that ease the pain when migrating from MFC to WTL. Wink | ;)
 

Kicking, squealing Gucci little piggy.
GeneralRe: Nicely ExecutedmemberBarry Lapthorn13 Nov '02 - 6:22 
Since you seem to be a WTL man - how does it fair with MFC?
What's the most complicated app you've written with WTL?

 
Regards
 
Barry

GeneralRe: Nicely ExecutedmemberRobert Edward Caldecott13 Nov '02 - 6: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    Rant Rant    Admin Admin   

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