fTRAYICON


Quick Data

Header file:trayicon.h
Object name:fTRAYICON
Object index:1 of 1 Object

General Description

The fTRAYICON is a class to insert and maintain a system tray icon. This could come in very handy for many different applications - so enjoy indeed!

I'm sure you know, but just in case: the system tray is the area on the taskbar with the clock, that also has status icons that can act as shortcuts for controlling programs. Mine looks like this (at time of writing): . It's got a lot of stuff in it - only recently, with my computer upgrade, have I been able to put that many icons there (not enough memory before).

Two more notes: One: this class only handles one icon at a time. If you wish to add more icons, then you can use multiple instances of this class.

Note two: if you change anything after you have added the icon, you don't need to remove and add the icon again - the changes are automatically applied for you by the class.


Methods


Prototype Description

void SetIcon(HICON Icon); This function sets the icon to use. If the icon is already trayed, the icon is changed immediately. You can use this to constantly update the icon, or create animations (I've seen that before... 4 tray icons, side by side, with animations. Completely pointless, wastes memory, but looks pretty cool.)
void SetTip(char* Tip); This function sets what the tip is. If you rest your mouse cursor over the icon, it will display a tip - and this can be set with this function. Please keep in mind that the text can not be more than 63 characters - any more will be truncated.
void AddIcon(HWND Handle, int ID); This function adds an icon to the tray. You will need to set the properties of it before adding it to the tray. Handle is the window to act as the parent and will receive messages relating to the tray icon. ID is an ID to give the icon. Please note that you must have a Handle to a window to be able to create an icon.
void RemoveIcon(void); This function removes the icon from the tray. You will need to ensure that the icon is removed before the application closes - Windows will not destroy it! The icon is removed in the deconstructor of this class, should you forget.

Events

Please note that the events listed here are done in pairs; one as a property, and the other as a function. The property is assigned a function name, ie. Object.OnClick = &FunctionName, whilst the function is called to verify that an event has occured. Please see the chapter on event handling for information on how to use these correctly.


Prototype Description Sent under

bool CheckLeftClick(lParam);
void (*OnClick)(fTRAYICON* Sender);
This event occurs when the icon is single left clicked. WM_TRAYICON
bool CheckLeftDblClick(lParam);
void (*OnDoubleClick)(fTRAYICON* Sender);
This event occurs when the icon was double left clicked. WM_TRAYICON
bool CheckRightClick(lParam);
void (*OnRightClick)(fTRAYICON* Sender);
This event occurs when the icon was single right clicked. This is a good event to trap so as to know when to show a menu. WM_TRAYICON
bool CheckRightDblClick(lParam);
void (*OnRightDoubleClick)(fTRAYICON* Sender);
This event occurs when the user double right clicks the tray icon. WM_TRAYICON

Sample Code

The following snippet gives a basic idea of how to use this component.


//Create an instance, setup...
fTRAYICON TrayIcon;

//Load the main icon (I'm assuming a resource called
//"MAIN_ICON" exists).
HICON MainIcon;
MainIcon = LoadIcon(GetModuleHandle(NULL), "MAIN_ICON");

//Set the icon.
TrayIcon.SetIcon(MainIcon);

//Set the tip.
TrayIcon.SetTip("This is a test icon.");

//Add the icon to the tray...
TrayIcon.AddIcon(Window.GetHandle(), TRAYICON_ID);

//You can change the icon and tip, and the tray icon
//will automatically be updated.
TrayIcon.SetTip("Hello, this is a different tip!");

//Remove it if you wish...
TrayIcon.RemoveIcon();

//Don't forget to clean up the icon memory!
//Do this as you quit...
DeleteObject(MainIcon);

Back to indexThe FreeFoote Foundation Classes Documentation