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   
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? PinmemberRobert 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.

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

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