Welcome to CTrayNotifyIcon, A freeware MFC/WTL class to encapsulate the tray notification API on Windows.
Features |
Usage |
Copyright |
History |
API Reference |
Contacting the Author |
v1.77 (28 October 2023)
v1.76 (18 June 2023)
v1.75 (21 May 2022)
v1.74 (18 December 2021)
v1.73 (12 May 2020)
v1.72 (3 April 2020)
v1.71 (24 December 2019)
v1.70 (15 September 2019)
v1.69 (22 April 2019)
v1.68 (7 October 2018)
v1.67 (26 November 2017)
v1.66 (27 April 2016)
v1.65 (26 November 2010)
v1.64 (6 November 2010)
v1.63 (10 July 2010)
v1.62 (10 April 2010)
v1.61 (22 June 2008)
v1.60 (12 March 2008)
v1.59 (13 October 2007)
v1.58 (15 June 2007)
v1.57 (15 September 2006)
v1.56 (19 August 2006)
v1.55 (19 July 2006)
v1.54 (6 July 2006)
v1.53 (5 July 2006)
v1.52 (3 July 2006)
v1.51 (27 June 2006)
v1.50 (3 March 2006)
v1.49 (23 July 2004)
v1.48 (2 April 2003)
v1.47 (31 March 2003)
v1.46 (21 March 2003)
25 November 2002
v1.45 (28 August 2001)
v1.44 (26 August 2001)
v1.43 (13 June 2001)
v1.42 (10 February 2001)
v1.41 (1 January 2001)
21 May 2000
v1.4 (16 May 1999)
v1.33 (9 May 1999)
v1.32 (28 January 1999)
v1.31 (27 January 1999)
v1.3 (22 July 1998)
v1.2 (23 June 1998)
v1.1 (25 November 1997)
v1.0 (14 May 1997)
Instantiate a CTrayNotifyIcon where it will be around for the lifetime of your icon. The sample program does this in the mainframe as the icon will be available for the lifetime of the application. In the sample program this is done in the mainframe class. The API consists of the following public functions of the class CTrayNotifyIcon
CTrayNotifyIcon::CTrayNotifyIcon
CTrayNotifyIcon::~CTrayNotifyIcon
CTrayNotifyIcon::Create
CTrayNotifyIcon::SetTooltipText
CTrayNotifyIcon::GetTooltipText
CTrayNotifyIcon::SetIcon
CTrayNotifyIcon::SetStandardIcon
CTrayNotifyIcon::GetIcon
CTrayNotifyIcon::UsingAnimatedIcon
CTrayNotifyIcon::SetNotificationWnd
CTrayNotifyIcon::GetNotificationWnd
CTrayNotifyIcon::Hide
CTrayNotifyIcon::Show
CTrayNotifyIcon::Delete
CTrayNotifyIcon::SetMenu
CTrayNotifyIcon::GetMenu
CTrayNotifyIcon::OnTrayNotification
CTrayNotifyIcon::IsShowing
CTrayNotifyIcon::IsHidden
CTrayNotifyIcon::LoadIconResource
CTrayNotifyIcon::SetBalloonDetails
CTrayNotifyIcon::GetBalloonText
CTrayNotifyIcon::GetBalloonCaption
CTrayNotifyIcon::GetBalloonStyle
CTrayNotifyIcon::GetBalloonTimeout
CTrayNotifiyIcon::BitmapToIcon
CTrayNotifiyIcon::GetDynamicDCAndBitmap
CTrayNotifyIcon::CTrayNotifyIcon
CTrayNotifyIcon::CTrayNotifyIcon();
Remarks
Standard constructor, just initialises all member variables to safe values.
CTrayNotifyIcon::~CTrayNotifyIcon
CTrayNotifyIcon::~CTrayNotifyIcon();
Remarks
Standard destructor. Internally this will call RemoveIcon to remove the icon from the UI.
bool CTrayNotifyIcon::Create(CWnd* pNotifyWnd, UINT nID, LPCTSTR pszTooltipText, HICON hIcon, UINT nNotifyMessage, UINT uMenuID = 0, bool bShow = true);
bool CTrayNotifyIcon::Create(CWnd* pNotifyWnd, UINT uID, LPCTSTR pszTooltipText, HICON* phIcons, int nNumIcons, DWORD dwDelay, UINT nNotifyMessage, UINT uMenuID = 0, bool bShow = true);
bool CTrayNotifyIcon::Create(CWnd* pNotifyWnd, UINT uID, LPCTSTR pszTooltipText, const CBitmap* pBitmap, UINT nNotifyMessage, UINT uMenuID = 0, bool bShow = true);
bool CTrayNotifyIcon::Create(CWnd* pNotifyWnd, UINT uID, LPCTSTR pszTooltipText, LPCTSTR pszBalloonText, LPCTSTR pszBalloonCaption, UINT nTimeout, BalloonStyle style, HICON hIcon, UINT nNotifyMessage, UINT uMenuID = 0, bool bNoSound = false, bool bLargeIcon = false, bool bRealtime = false, HICON hBalloonIcon = nullptr, bool bQuiet = false, bool bShow = true);
bool CTrayNotifyIcon::Create(CWnd* pNotifyWnd, UINT uID, LPCTSTR pszTooltipText, LPCTSTR pszBalloonText, LPCTSTR pszBalloonCaption, UINT nTimeout, BalloonStyle style, HICON* phIcons, int nNumIcons, DWORD dwDelay, UINT nNotifyMessage, UINT uMenuID = 0, bool bNoSound = false, bool bLargeIcon = false, bool bRealtime = false, HICON hBalloonIcon = nullptr, bool bQuiet = false, bool bShow = true);
bool CTrayNotifyIcon::Create(CWnd* pNotifyWnd, UINT uID, LPCTSTR pszTooltipText, LPCTSTR pszBalloonText, LPCTSTR pszBalloonCaption, UINT nTimeout, BalloonStyle style, CBitmap* pBitmap, UINT nNotifyMessage, UINT uMenuID = 0, bool bNoSound = false, bool bLargeIcon = false, bool bRealtime = false, HICON hBalloonIcon = nullptr, bool bQuiet = false, bool bShow = true);
Return Value
true if the tray icon was successfully created otherwise false.
Parameters
pNotifyWnd: The window to send notification messages to. This can be changed later by using the SetNotificationWnd method.
uID: An application defined value to associate with each tray icon. This allows you to display multiple icons from one app. CTrayNotifyIcon also overrides its meaning such that it is used as the menu resource id for the context menu.
pszTooltipText: Initial tooltip text to display. This can be changed later using the SetTooltipText method.
hIcon: The icon to display. This can be changed later using the SetIcon method.
nNotifyMessage: This is the notification message to use to inform the window about mouse messages. This should be defined as some WM_USER message and handled in the notification windows message map using the ON_MESSAGE macro. To see how to hook this up have a look at the mainfrm.cpp
phIcons: pointer to an array of icon handles to use to do the icon animation.
nNumIcons: The number of icons phIcons points to.
dwDelay: interval in milliseconds between icon animation frames.
pszBalloonText: The text to appear in the balloon style tooltip
pszBalloonCaption: The caption for the balloon style tooltip
nTimeout: The timeout associated with the balloon style tooltip. After this time (in milliseconds) the balloon tooltip will disappear.
style: The icon to appear in the balloon tooltip. It can be one of the enum values CTrayNotifyIcon::Info, CTrayNotifyIcon::Error, CTrayNotifyIcon::Warning, CTrayNotifyIcon::None or CTrayNotifyIcon::User. Please note that CTrayNotifyIcon::User is only available on XP SP2 or later. Also note that Balloon style tooltips themselves are only available on version 5 of the shell or later (Windows 2000 or later).
uMenuID Set this value to a non-zero value to use a different menu resource instead of the default one as specified by uID
pBitmap The bitmap to display as the icon. Internally the code converts the bitmap to an icon prior to displaying it.
bNoSound Setting this value to true (the default is false) will cause the shell not to play the popup sound when the balloon style tooltip is shown. Please note that this functionality is only available on XP SP2 or later.
bShow Should the icon be shown initially, if set to false, then the icon is created hidden. Please note that this functionality is only available on Windows 2000 or later.
bLargeIcon Corresponds to the NIIF_LARGE_ICON flag which creates the balloon with a large icon. Please note that this functionality is only available on Windows Vista or later.
bRealtime Corresponds to the NIF_REALTIME flag. This allows you to specify not to bother showing the balloon if it is delayed due to the presence of an existing balloon. Please note that this functionality is only available on Windows Vista or later.
hBalloonIcon Corresponds to the NOTIFYICONDATA::hBalloonIcon variable. This allows you to create a user specified balloon icon which is different to the actual tray icon. Please note that this functionality is only available on Windows Vista or later.
bQuite Corresponds to the NIIF_RESPECT_QUIET_TIME flag which tells the API to respect the "Quiet" mode setting of the OS as silently not show the icon if necessary. This is used by Windows 7 to reduce the clutter of tray notification icons during the initial Out of Box experience. Please note that this functionality is only available on Windows 7 or later.
Remarks
Creates a tray notification icon. The 2 overridden versions which specify balloon tooltips are only available on Shell v5 (nominally Windows 2000 and later).
CTrayNotifyIcon::SetTooltipText
bool CTrayNotifyIcon::SetTooltipText(LPCTSTR pszTooltipText);
bool CTrayNotifyIcon::SetTooltipText(UINT nID);
Return Value
true if the tooltip text was changed successfully otherwise false
Parameters
pszTooltipText The text to set
nID A windows string resource ID of the text to set
Remarks
Sets the tooltip text associated with the tray icon
CTrayNotifyIcon::GetTooltipText
CString CTrayNotifyIcon::GetTooltipText() const
Return Value
The current tooltip text
bool SetIcon(HICON hIcon);
bool SetIcon(CBitmap* pBitmap);
bool SetIcon(LPCTSTR lpIconName);
bool SetIcon(UINT nIDResource);
bool SetIcon(HICON* phIcons, int nNumIcons, DWORD dwDelay);
Return Value
true if the icon was changed successfully otherwise false
Parameters
hIcon handle to the icon to set
lpIconName a Windows Icon resource string of the icon to use
nIDResource a Windows Icon resource ID of the icon to use
phIcons: pointer to an array of icon handles to use to do the icon animation.
nNumIcons: The number of icons phIcons points to.
dwDelay: interval in milliseconds between icon animation frames.
pBitmap The bitmap to use as the icon
Remarks
Sets the icon associated with the tray icon. Normally you should use LoadIconResource to load up your icons.
CTrayNotifyIcon::SetStandardIcon
bool CTrayNotifyIcon::SetStandardIcon(LPCTSTR lpIconName);
bool CTrayNotifyIcon::SetStandardIcon(UINT nIDResource);
Parameters
lpIconName a Windows Icon resource string of the icon to use
nIDResource a Windows Icon resource ID of the icon to use
Remarks
Sets the icon associated with the tray icon using one of the standard system icon identifiers.
HICON CTrayNotifyIcon::GetIcon() const;
Return Value
Icon handle of the icon currently being displayed
CTrayNotifyIcon::UsingAnimatedIcon
bool UsingAnimatedIcon() const;
Return Value
true if an animated icon is being show, false if an ordinary static icon is being used.
CTrayNotifyIcon::SetNotificationWnd
bool CTrayNotifyIcon::SetNotificationWnd(CWnd* pNotifyWnd);
Return Value
true if the notification window was successfully set otherwise false
Parameters
pNotifyIcon The window which is to be the notification window associated with this tray icon
CTrayNotifyIcon::GetNotificationWnd
CWnd* GetNotificationWnd() const;
Return Value
The window which is the notification window associated with this tray icon
void CTrayNotifyIcon::Hide();
Remarks
Temporarily hides the tray notify icon. Note this function is only supported on Shell v5 or later (nominally Windows 2000 or later). Internally this method will use the NIS_HIDDEN functionality to hide the icon
void CTrayNotifyIcon::Show();
Remarks
The corollary function to Hide
void CTrayNotifyIcon::Delete(bool bCloseHelperWindow = true);
Parameters
bCloseHelperWindow if set, then the internal window which the CTrayNotifyIcon class creates to handle animation timers and the tray resurrection messages is destroyed. You would normally leave this as true so that you can create a fresh tray icon via the Create methods.
Remarks
Removes the tray icon entirely and resets the class member variables. This is the corollary function of CTrayNotifyIcon::Create
void CTrayNotifyIcon::SetMenu(HMENU hMenu);
Parameters
hMenu The menu resource to use as the context menu for the tray icon.
CMenu& CTrayNotifyIcon::GetMenu();
Return Value
menu resource currently associated with the tray icon.
CTrayNotifyIcon::OnTrayNotification
virtual LRESULT CTrayNotifyIcon::OnTrayNotification(WPARAM wParam, LPARAM lParam);
Return Value
Standard result to return from the message handler
Parameters
wParam: the WPARAM details for the notification.
lParam: the LPARAM details for the notification.
Remarks
When your handler gets control, you are free to do whatever you like when you get the notification. The provided sample app does nothing and delegates all the work to the default implementation in CTrayNotifyIcon This virtual (so you can override it) function implements the double click and standard context menu support.
bool CTrayNotifyIcon::IsShowing() const;
Return Value
true if the tray icon is currently been shown else false
bool CTrayNotifyIcon::IsHidden();
Return Value
true if the tray icon is currently hidden else false
static HICON LoadIcon(LPCTSTR lpIconName, bool bLargeIcon = false);
static HICON LoadIcon(UINT nIDResource, bool bLargeIcon = false);
static HICON LoadIcon(HINSTANCE hInstance, LPCTSTR lpIconName, bool bLargeIcon = false);
static HICON LoadIcon(HINSTANCE hInstance, UINT nIDResource, bool bLargeIcon = false);
Parameters
lpIconName a Windows Icon resource string of the icon to load
nIDResource a Windows Icon resource ID of the icon to load
hInstance The instance handle to load the resource from.
bLargeIcon If you are loading an icon to be used as a large icon for the balloon window on Vista, then set this to true to load the large version of the icon (normally 32*32)
Return Value
An icon handle if the icon could be loaded otherwise nullptr.
Remarks
You should call this function in preference to the MFC CWinApp::LoadIcon or SDK LoadIcon call to load up your icons for use by CTrayNotifyIcon. This function will first attempt to load a 16*16 size (or 32*32 if you specify bLargeIcon = true) icon.
CTrayNotifyIcon::SetBalloonDetails
bool CTrayNotifyIcon::SetBalloonDetails(LPCTSTR pszBalloonText, LPCTSTR pszBalloonCaption, BalloonStyle style, UINT nTimeout, HICON hUserIcon=nullptr, bool bNoSound=false, bool bLargeIcon = false, bool bRealtime = false, HICON hBalloonIcon = nullptr);
Return Value
true if the details were updated successfully otherwise false
Parameters
pszBalloonText The text to appear in the balloon style tooltip.
pszBalloonCaption The caption for the balloon style tooltip.
nTimeout The timeout associated with the balloon style tooltip. After this time (in milliseconds) the balloon tooltip will disappear.
style The icon to appear in the balloon tooltip. Please see CTrayNotifyIcon::Create for more details on the possible values.
hUserIcon If you set "style" to User, then this parameter is where you specify the user icon to display in the balloon style tooltip.
bNoSound Setting this value to true (the default is false) will cause the shell not to play the popup sound when the balloon style tooltip is shown. Please note that this functionality is only available on XP SP2 or later.
bLargeIcon Corresponds to the NIIF_LARGE_ICON flag which creates the balloon with a large icon. Please note that this functionality is only available on Windows Vista or later.
bRealtime Corresponds to the NIF_REALTIME flag. This allows you to specify not to bother showing the balloon if it is delayed due to the presence of an existing balloon. Please note that this functionality is only available on Windows Vista or later.
hBalloonIcon Corresponds to the NOTIFYICONDATA::hBalloonIcon variable. This allows you to create a user specified balloon icon which is different to the actual tray icon. Please note that this functionality is only available on Windows Vista or later.
Remarks
Sets the balloon tooltip details associated with the tray icon
CTrayNotifyIcon::GetBalloonText
CString CTrayNotifyIcon::GetBalloonText() const
Return Value
The current balloon text
CTrayNotifyIcon::GetBalloonCaption
CString CTrayNotifyIcon::GetBalloonCaption() const
Return Value
The current balloon caption
CTrayNotifyIcon::GetBalloonStyle
CTrayNotifiyIcon::BalloonStyle CTrayNotifyIcon::GetBalloonStyle() const
Return Value
The current style balloon tooltip being used
CTrayNotifyIcon::GetBalloonTimeout
UINT CTrayNotifyIcon::GetBalloonTimeout() const
Return Value
The current timeout (in milliseconds) associated with the balloon tooltip being used
HICON CTrayNotifyIcon::BitmapToIcon(CBitmap* pBitmap)
Return Value
An ICON representation of the specified bitmap
Parameters
pBitmap: The bitmap to convert to an icon
Remarks
Converts a windows bitmap to a icon. This method is used by the Create method when called with a CBitmap instead of a HICON.
CTrayNotifyIcon::GetDynamicDCAndBitmap
static bool CTrayNotifyIcon::GetDynamicDCAndBitmap(CDC* pDC, CBitmap* pBitmap)
Return Value
true if the DC and bitmap were obtained ok otherwise false
Parameters
pDC: The address of a DC class to be returned with a DC compatible with the tray.
pBitmap: The address of a bitmap which will contain the tray icon representation.
Remarks
This function should be called in preparation for using dynamic tray icons (i.e. where you construct the ICON at runtime by drawing into a memory device context.). Upon a successful return you should hang on to these two parameters until you need them. For example in the sample app, the code periodically updates the DC by drawing a new number into the DC and then updating the tray icon with this. For more info on using dynamic tray icons, please consult the supplied sample application.
PJ Naughter
Email: pjna@naughter.com
Web: http://www.naughter.com
28 October 2023