CTrayNotifyIcon v1.77
Welcome to CTrayNotifyIcon, A freeware MFC/WTL class to encapsulate the tray
notification API on Windows.
Features
- Simple and clean C++ interface.
- All code compiles cleanly at warning level 4 and is /analyze and
/permissive- clean.
- Supports all of the tray functionality right up to Windows 7 which was
the last version of Windows where new features were added to the Tray
Notification API.
- Hides the complexity of having to deal with the messy NOTIFYICONDATA structure
and all its various quirks, which you probably will be familiar with if you
have coded directly to the "Shell_NotifyIcon" API.
Usage
- To use the class in your code simple include NTray.cpp in your project and
#include NTray.h in which ever of your modules needs to make calls to the class.
- As of v1.51, the code now uses ATL's CWindowImpl class in preference to
the author's CHookWnd class. This does mean that for MFC only client projects,
you will need to add ATL support to your project. Please see the sample app
included in the download for how to include ATL in an MFC project.
- As of v1.56, you can use the class without the need for pulling in MFC.
This is achieved by using WTL which is an open source library extension for
ATL to provide UI support along the lines of MFC. You can download WTL from
http://sourceforge.net/projects/wtl/.
- As of v1.68, the classes are now designed for VC 2017 or later. They will
not compile on earlier releases of VC.
- To see the class in action, have a look at the code in the module "mainfrm.cpp".
Copyright
- You are allowed to include the source code in any product (commercial, shareware,
freeware or otherwise) when your product is released in binary form.
- You are allowed to modify the source code in any way you want except you
cannot modify the copyright details at the top of each module.
- If you want to distribute source code with your application, then you are
only allowed to distribute versions released by the author. This is to maintain
a single distribution point for the source code.
History
v1.77 (28 October 2023)
- Fixed an issue where the CTrayNotifyIcon::OnTrayNotification callback
method would not work correctly if the m_NotifyIconData.uTimeout member
variable gets updated during runtime of client applications. This can occur
when you call CTrayNotifyIcon::SetBalloonDetails. Thanks to Maisala Tuomo
for reporting this bug.
v1.76 (18 June 2023)
- Updated copyright details.
- Added additional SAL annotations to the code.
v1.75 (21 May 2022)
- Updated copyright details.
- Updated the code to use C++ uniform initialization for all variable
declarations.
v1.74 (18 December 2021)
- Updated copyright details.
- Fixed more Clang-Tidy static code analysis warnings in the code.
v1.73 (12 May 2020)
- Fixed more Clang-Tidy static code analysis warnings in the code.
v1.72 (3 April 2020)
- Updated copyright details.
- Fixed more Clang-Tidy static code analysis warnings in the code.
- Removed internal declaration of various SDK structs. You now need to
define NTDDI_VERSION >= NTDDI_VISTA to use the code.
v1.71 (24 December 2019)
- Fixed various Clang-Tidy static code analysis warnings in the code.
v1.70 (15 September 2019)
- Replaced enum with enum class throughout the code
v1.69 (22 April 2019)
- Updated copyright details
- Updated the code to clean compile on VC 2019
v1.68 (7 October 2018)
- Updated copyright details.
- Fixed a number of C++ core guidelines compiler warnings. These changes
mean that the code will now only compile on VC 2017 or later.
v1.67 (26 November 2017)
- Updated copyright details.
- Fixed a number of compilation errors when compiled with VC 2017
- Updated the code to compile cleanly when _ATL_NO_AUTOMATIC_NAMESPACE is
defined.
- Replaced NULL throughout the codebase with nullptr. This means that the
minimum requirement for the framework is now VC 2010.
- Replaced BOOL throughout the codebase with bool.
- Removed all functionality for pre Vista versions of Windows.
v1.66 (27 April 2016)
- Updated copyright details.
- Updated the code to clean compile on VC 2012 - VC 2015.
- Removed support for CTRAYNOTIFYICON_NOWIN2K preprocessor macro
- Removed various redefines of ShellApi.h constants from the code
- Added SAL annotations to all the code.
- Reworked the definition of the string class to now use a typedef
internal to the CTrayNotifyIcon class.
- Updated CTrayNotifyIcon::OnTrayNotification to handle
NOTIFYICON_VERSION_4 style notifications.
- Reworked the internal storage of the animation icons to use
ATL::CHeapPtr
v1.65 (26 November 2010)
- Minor update to use DBG_UNREFERENCED_LOCAL_VARIABLE macro. Thanks to
Jukka Ojanen for prompting this update.
v1.64 (6 November 2010)
- Minor update to code in SetTooltipText to code which handles
unreferenced variable compiler warning
- Implemented a GetTooltipMaxSize method which reports the maximum
size which the tooltip can be for a tray icon. Thanks to Geert van Horrik
for this nice addition
- All places which copy text to the underlying NOTIFYICONDATA now
use the _TRUNCATE parameter in their call to the Safe CRT runtime. This
change in behaviour means that client apps will no longer crash if they
supply data larger than this Windows structure can accommodate. Thanks to
Geert van Horrik for prompting this update.
- All calls to sizeof(struct)/sizeof(first element) have been
replaced with _countof
- Fixed a linker error when compiling the WTL sample app in release
mode.
v1.63 (10 July 2010)
- Updated the sample app to compile cleanly on VS 2010.
- Fixed a bug in CTrayNotifyIcon::Delete where the code would ASSERT if
the tray notify icon was never actually created. Thanks to "trophim" for
reporting this bug.
v1.62 (10 April 2010)
- Updated copyright details.
- Updated the project settings to more modern default values.
- Updated the WTL version of LoadIcon to use the more modern ModuleHelper
class from WTL to get the resource instance. Thanks to "Yarp" for reporting
this issue.
- The class now has support for the Windows 7 "NIIF_RESPECT_QUIET_TIME" flag.
This value can be set via the new "bQuietTime" parameter to the Create method.
- Updated the code which does version detection of the Shell version
v1.61 (22 June 2008)
- Code now compiles cleanly using Code Analysis (/analyze)
- Updated code to compile correctly using _ATL_CSTRING_EXPLICIT_CONSTRUCTORS
define
- Removed VC 6 style AppWizard comments from the code.
- The code now only supports VC 2005 or later.
v1.60 (12 March 2008)
- Updated copyright details
- Fixed a bug in SetBalloonDetails where the code did not set the flag NIF_ICON
if a user defined icon was being set. Thanks to "arni" for reporting this bug.
- Updated the sample app to clean compile on VC 2008
v1.59 (13 October 2007)
- Subclassing of the top level window is now not done internally by the CTrayNotifyIcon
class using the CTrayIconHooker class. Instead now a hidden top level window
is created for each tray icon you create and these look after handling the tray
resurrection and animated icon timer messages. This refactoring of the internals
of the class now also fixes a bug where an application which creates multiples
tray icons would only get one icon recreated when the tray resurrection message
was received. Thanks to Steven Dwyer for prompting this update.
- Updated the MFC sample app to correctly initialize ATL for VC 6
v1.58 (15 June 2007)
- Updated copyright messages.
- If the code detects that MFC is not included in the project, the code uses
the standard preprocessor define "_CSTRING_NS" to declare the string class to
use rather than explicitly using WTL::CString. Thanks to Krzysztof Suszka for
reporting this issue.
- Updated sample app to compile cleanly on VC 2005.
- Addition of a "BOOL bShow" to all the Create methods. This allows you to
create an icon without actually showing it. This avoids the flicker which previously
occurred if you created the icon and then immediately hid the icon. Thanks to
Krzysztof Suszka for providing this suggestion.
- Demo app now initially creates the first icon as hidden for demonstration
purposes.
- Added support for NIIF_LARGE_ICON. This Vista only feature allows you to
create a large balloon icon.
- Added support for NIF_REALTIME. This Vista only flag allows you to specify
not to bother showing the balloon if it is delayed due to the presence of an
existing balloon.
- Added support for NOTIFYICONDATA::hBalloonIcon. This Vista only feature
allows you to create a user specified balloon icon which is different to the
actual tray icon.
- LoadIconResource method now includes support for loading large icons and
has been renamed to simply LoadIcon. Also two overridden versions of this method
have been provided which allow the hInstance resource ID to be specified to
load the icon from.
- Reworked the internal code to CTrayNotifyIcon which detects the shell version.
- Updated the tray icon text in the demo app to better demonstrate the features
of the class.
- Updated the WTL sample to be consistent with the MFC sample code
- Updated comments in documentation about usage of the Platform SDK.
v1.57 (15 September 2006)
- Fixed a bug where WM_DESTROY messages were not been handled correctly for
the top level window which the CTrayIconHooker class subclasses in order to
handle the tray resurrection message, the animation timers and auto destroying
of the icons when the top level window is destroyed. Thanks to Edward Livingston
for reporting this bug.
- Fixed a bug where the tray icons were not being recreated correctly when
we receive the "TaskbarCreated" message when Explorer is restarted. Thanks to
Nuno Esculcas for reporting this bug.
- Split the functionality of hiding versus deleting and showing versus creating
of the tray icon into 4 separate functions, namely Delete(), Create(), Hide()
and Show(). Note that Hide and Show functionality is only available on Shell
v5 or later.
- Fixed an issue with recreation of tray icons which use a dynamic icon created
from a bitmap (through the use of BitmapToIcon).
- CTrayNotifyIcon::LoadIconResource now loads up an icon as a shared icon
resource using LoadImage. This should avoid resource leaks using this function.
v1.56 (19 August 2006)
- Updated the code to operate independent of MFC if so desired. This requires
WTL which is an open source library extension for ATL to provide UI support
along the lines of MFC. Thanks to zhiguo zhao for providing this very nice addition.
v1.55 (19 July 2006)
- The default menu item can now be customized via SetDefaultMenuItem and GetDefaultMenuItem.
Thanks to Mikhail Bykanov for suggesting this nice update.
- Optimized CTrayNotifyIcon constructor code
v1.54 (6 July 2006)
- Reverted the change made for v1.53 where the screen coordinates used to
show the context menu use the current message's screen coordinates. Instead
the pre v1.53 mechanism which uses the current cursor's screen coordinates is
now used. Thanks to Itamar Syn-Hershko for reporting this issue.
v1.53 (5 July 2006)
- Fixed a bug where the menu may pop up a second time after a menu item is
chosen on Windows 2000. The problem was tracked down to the code in CTrayNotifyIcon::OnTrayNotification.
During testing of this bug, I was unable to get a workable solution using the
new shell messages of WM_CONTEXTMENU, NIN_KEYSELECT & NIN_SELECT on Windows
2000 and Windows XP. This means that the code in CTrayNotifyIcon::OnTrayNotification
uses the old way of handling notifications (WM_RBUTTDOWN*). This does mean that
by default, client apps which use the CTrayNotifyIcon class will not support
the new keyboard and mouse semantics for tray icons (IMHO this is no big loss!).
Client code is of course free to handle their own notifications. If you go down
this route then I would advise you to thoroughly test your application on Windows
2000 and Windows XP as my testing has shown that there is significant differences
in how tray icons handle their messaging on these 2 operating systems. Thanks
to Edwin Geng for reporting this issue.
- Class now displays the menu based on the current message's screen coordinates,
rather than the current cursor screen coordinates.
- Fixed bug in sample app where if the about dialog is already up and it is
reactivated from the tray menu, it did not bring itself into the foreground
v1.52 (3 July 2006)
- Fixed a bug where the HideIcon functionality did not work on Windows 2000.
This was related to how the cbSize member of the NOTIFYICONDATA structure was
initialized. The code now dynamically determines the correct size to set at
runtime according to the instructions provided by the MSDN documentation for
this structure. As a result of this, all "bWin2k" parameters which were previously
exposed via CTrayNotifyIcon have now been removed as there is no need for them.
Thanks to Edwin Geng for reporting this important bug. Client code will still
need to intelligently make decisions on what is supported by the OS. For example
balloon tray icons are only supported on Shell v5 (nominally Windows 2000 or
later). CTrayNotifyIcon will ASSERT if for example calls are made to it to create
a balloon tray icon on operating systems with < Shell v5.
v1.51 (27 June 2006)
- Code now uses new C++ style casts rather than old style C casts where necessary.
- The class framework now requires the Platform SDK if compiled using VC 6.
- Updated the logic of the ASSERTs which validate the various string lengths.
- Fixed a bug in CTrayNotifyIcon::SetFocus() where the cbSize value was not
being set correctly.
- CTrayIconHooker class now uses ATL's CWindowImpl class in preference to
the author's CHookWnd class. This does mean that for MFC only client projects,
you will need to add ATL support to your project.
- Optimized CTrayIconHooker constructor code.
- Updated code to compile cleanly using VC 2005. Thanks to "Itamar" for prompting
this update.
- Addition of a CTRAYNOTIFYICON_EXT_CLASS and CTRAYNOTIFYICON_EXT_API macros
which makes the class easier to use in an extension dll.
- Made CTrayNotifyIcon destructor virtual.
v1.50 (3 March 2006)
- Updated copyright details.
- Updated the documentation to use the same style as the web site.
- Did a spell check of the documentation.
- Fixed some issues when the code is compiled using /Wp64. Please note that
to support this the code now requires a recentish Platform SDK to be installed
if the code is compiled with Visual C++ 6.
- Replaced all calls to ZeroMemory with memset.
- Fixed an issue where SetBalloonDetails was not setting the cbSize parameter.
Thanks to Enrique Granda for reporting this issue.
- Added support for NIIF_USER and NIIF_NONE flags.
- Now includes support for NIM_NIMSETVERSION via SetVersion. In addition this
is now automatically set in the Create() calls if the Win2k boolean parameter
is set.
- Removed derivation from CObject as it was not really needed.
- Now includes support for NIM_SETFOCUS
- Added support for NIS_HIDDEN via the ShowIcon and HideIcon methods.
- Added support for NIIF_NOSOUND
v1.49 (23 July 2004)
- Minor update to remove unnecessary include of "resource.h"
v1.48 (2 April 2003)
- Now uses v1.06 of my Hookwnd class
- Fixed a bug in the sample app for this class where the hooks should have
been created as global instances rather than as member variables of the mainframe
window. This ensures that the hooks remain valid even after calling DefWindowProc
on the mainframe.
v1.47 (31 March 2003)
- Now uses v1.05 of my Hookwnd class
v1.46 (21 March 2003)
- Fixed icon resource leaks in SetIcon(LPCTSTR lpIconName) and SetIcon(UINT
nIDResource). Thanks to Egor Pervouninski for reporting this.
- Fixed unhooking of the tray icon when the notification window is being closed.
25 November 2002
- Added a comment to the NTray.h header file about where to get the HookWnd.h
file if the code compile fails.
v1.45 (28 August 2001)
- Added support for direct access to the System Tray's HDC. This allows you
to generate an icon for the tray on the fly using GDI calls. The idea came from
the article by Jeff Heaton in the April issue of WDJ. Also added are overridden
Create methods to allow you to easily construct a dynamic tray icon given a
BITMAP instead of an ICON.
v1.44 (26 August 2001)
- Fixed memory leak in RemoveIcon.
- Fixed GPF in RemoveIcon by removing call to Unhook
v1.43 (13 June 2001)
- Now removes windows hook upon call to RemoveIcon.
v1.42 (10 February 2001)
- Now fully supports creation of 2 tray icons at the same time.
v1.41 (1 January 2001)
- Now includes copyright message in the source code and documentation.
- Fixed problem where the window does not get focus after double clicking
on the tray icon.
- Now fully supports the Windows 2000 balloon style tooltips.
- Fixed a off by one problem in some of the ASSERT's.
- Fixed problems with Unicode build configurations.
- Provided Win2k specific build configurations.
21 May 2000
- Minor update to the documentation.
v1.4 (16 May 1999)
- Modified the sample program to ensure that 16*16 icons are used when tray
icon animation is not being used.
- Now uses the author's hookwnd class. This prevents the need to create the
two hidden windows namely CTrayRessurectionWnd and CTrayTimerWnd to handle icon
animation and tray creation messages.
- Sample project now compiles at warning level 4.
- General code tidy up and rearrangement.
- Added numerous ASSERT's to improve the code's robustness.
- Added functions to allow context menu to be customized.
v1.33 (9 May 1999)
- Fixed a problem as documented in KB article "PRB: Menus for Notification
Icons Do Not Work Correctly", Article ID: Q135788.
v1.32 (28 January 1999)
- Removed a number of level 4 warnings which were occurring.
v1.31 (27 January 1999)
- Code now correctly loads up 16*16 icons, previously it allowed Windows to
rescale the icon from 32*32 resulting in slightly blurry images. This has been
accomplished via the new LoadIconResource method.
v1.3 (22 July 1998)
- Code now compiles cleanly at warning level 4
- Code is now UNICODE enabled + build configurations are provided
- The documentation for the class has been completely revamped
v1.2 (23 June 1998)
- Class now supports the new Taskbar Creation Notification message which comes
with IE 4. This allows the tray icon to be recreated whenever the explorer restarts
(Crashes!!)
v1.1 (25 November 1997)
- Addition of HideIcon(), ShowIcon() & MoveToExtremeRight()
- Support for animated tray icons
v1.0 (14 May 1997)
API Reference
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.
CTrayNotifyIcon::Create
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
CTrayNotifyIcon::SetIcon
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.
CTrayNotifyIcon::GetIcon
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
CTrayNotifyIcon::Hide
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
CTrayNotifyIcon::Show
void CTrayNotifyIcon::Show();
Remarks
The corollary function to Hide
CTrayNotifyIcon::Delete
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
CTrayNotifyIcon::SetMenu
void CTrayNotifyIcon::SetMenu(HMENU hMenu);
Parameters
hMenu The menu resource to use as the context menu for the tray icon.
CTrayNotifyIcon::GetMenu
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.
CTrayNotifyIcon::IsShowing
bool CTrayNotifyIcon::IsShowing() const;
Return Value
true if the tray icon is currently been shown else false
CTrayNotifyIcon::IsHidden
bool CTrayNotifyIcon::IsHidden();
Return Value
true if the tray icon is currently hidden else false
CTrayNotifyIcon::LoadIcon
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
CTrayNotifyIcon::BitmapToIcon
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.
Contacting the Author
PJ Naughter
Email: pjna@naughter.com
Web: http://www.naughter.com
28 October 2023