|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Introduction Introduction
This class is a light wrapper around the windows system tray stuff. It adds an icon to the system tray with the specified ToolTip text and callback notification value, which is sent back to the Parent window. The Old way: The basic steps to using a tray icon via the windows API are:
Changing the values of the fields in The Better way The simpler way to add an icon to the system tray is to create an object of type CSystemTray m_TrayIcon; // Member variable of some class ... // in some member function maybe... m_TrayIcon.Create(pParentWnd, WM_MY_NOTIFY, "Click here", hIcon, nTrayIconID); This will insert an icon in the system tray. See the following section for details. To MFC or not to MFC...There are two forms of the class: MFC and Non-MFC. They are in the SystemTray.* and SystemTraySDK.* files respectively. The MFC version has been written to use MFC classes ( Both classes have essentially the same functionality, excepting that the non-MFC version only supports a single tray icon per application. Because the non-MFC version is not derived from
Eg. For a non-MFC tray icon, do the following: CSystemTray m_TrayIcon; // Member variable of some class ... // in some member function maybe... m_TrayIcon.Create(hInstance, NULL, WM_MY_NOTIFY, "Click here", hIcon, nID); // Send all menu messages to hMyMainWindow m_TrayIcon.SetTargetWnd(hMyMainWindow); ConstructionCSystemTray();
CSystemTray(CWnd* pWnd, UINT uCallbackMessage, LPCTSTR szToolTip,
HICON icon, UINT uID, BOOL bHidden = FALSE,
LPCTSTR szBalloonTip = NULL, LPCTSTR szBalloonTitle = NULL,
DWORD dwBalloonIcon = NIIF_NONE, UINT uBalloonTimeout = 10);
BOOL Create(CWnd* pWnd, UINT uCallbackMessage, LPCTSTR szToolTip,
HICON icon, UINT uID, BOOL bHidden = FALSE,
LPCTSTR szBalloonTip = NULL, LPCTSTR szBalloonTitle = NULL,
DWORD dwBalloonIcon = NIIF_NONE, UINT uBalloonTimeout = 10);
The non-MFC version includes an additional parameter (parameter 1) that represents the applications instance handle. Note that the destructor automatically removes the icon from the tray.
If the pWnd parameter is OperationsLRESULT OnTrayNotification(WPARAM wID, LPARAM lEvent) // Discussed below void MoveToRight() // Moves the icon to the far right of the tray, // so it is immediately to the left of the clock void HideIcon() // Hides but does not totally remove the icon // from the tray. void RemoveIcon() // Removes the icon from the tray (icon can no // longer be manipulated) void ShowIcon() // Redisplays a previously hidden icon void AddIcon() // Adds the icon to the tray void SetFocus( // Sets the focus to the icon (Win2000 only) BOOL ShowBalloon(LPCTSTR szText, // Shows the balloon tip (Win2000 only) LPCTSTR szTitle = NULL, DWORD dwIcon = NIIF_NONE, UINT uTimeout = 10); BOOL SetTooltipText(LPCTSTR pszTip) // Set Tooltip text BOOL SetTooltipText(UINT nID) // Set tooltip from text resource ID CString GetTooltipText() const // Retrieve tool tip text BOOL SetNotificationWnd(CWnd* pWnd) // Self explanatory CWnd* GetNotificationWnd() const BOOL SetTargetWnd(CWnd* pTargetWnd);// Change or retrieve the window to CWnd* GetTargetWnd() const; // send menu commands to BOOL SetCallbackMessage(UINT uCallbackMessage) // Self explanatory UINT GetCallbackMessage() const; HICON GetIcon() const // Get current tray icon BOOL SetIcon(HICON hIcon) // Change tray icon. Returns FALSE // if unsuccessful BOOL SetIcon(LPCTSTR lpszIconName) // Same, using name of the icon resource BOOL SetIcon(UINT nIDResource) // Same, using icon resource ID BOOL SetStandardIcon(LPCTSTR lpIconName) // Load icon from the BOOL SetStandardIcon(UINT nIDResource) // current application // Set list of icons for animation BOOL SetIconList(UINT uFirstIconID, UINT uLastIconID); BOOL SetIconList(HICON* pHIconList, UINT nNumIcons); // Start animation BOOL Animate(UINT nDelayMilliSeconds, int nNumSeconds = -1); BOOL StepAnimation(); // Step to next icon BOOL StopAnimation(); // Stop animation BOOL SetMenuDefaultItem(UINT uItem, BOOL bByPos); // Set default menu item void GetMenuDefaultItem(UINT& uItem, BOOL& bByPos); // Get default menu item static void MinimiseToTray(CWnd* pWnd); static void MaximiseFromTray(CWnd* pWnd, CRect rectTo); static void MaximiseFromTray(CWnd* pWnd, LPCREATESTRUCT lpCreateStruct); virtual void CustomizeMenu(CMenu*) // Customise the context menu before it's // displayed
The default The default menu item code was contributed by Enrico Lelina. Minimising an application to the system trayTwo functions have been provided to allow you to easily "minimise" an application to the system tray: static void MinimiseToTray(CWnd* pWnd); static void MaximiseFromTray(CWnd* pWnd); where pWnd is the window to minimise or maximise (usually your main application window). "Minimising to the system tray" means that the applications main window is minimised using the If an application has been minimised to the tray, then it can be maximised again by calling void CMyDialog::OnDestroy() { CDialog::OnDestroy(); CSystemTray::MinimiseToTray(this); } void CMyDialog::OnShowWindow(BOOL bShow, UINT nStatus) { CDialog::OnShowWindow(bShow, nStatus); if (bShow) CSystemTray::MaximiseFromTray(this); } Icon animationIcon animation can be achieved by specifying a list of icons using Default message handlingThe parent window, on receiving a notification message, can redirect this message back to the tray icon for handling by calling Using the new Windows 2000 / IE5 Balloon tipsThe new balloon tips rely on Shell32.dll to be version 5.0 or later, and for the the
I currently use the lazy method. There is a define Example of useA good place to declare the tray icon is in your #define WM_ICON_NOTIFY WM_APP+10
CSystemTray m_TrayIcon
Add a message map entry for the tray icon notification: BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
...
ON_MESSAGE(WM_ICON_NOTIFY, OnTrayNotification)
END_MESSAGE_MAP()
Create the icon (maybe in your OnCreate): if (!m_TrayIcon.Create(this, WM_ICON_NOTIFY, strToolTip, hIcon, IDR_POPUP_MENU)) return -1; where LRESULT CMainFrame::OnTrayNotification(WPARAM wParam, LPARAM lParam)
{
// Delegate all the work back to the default
// implementation in CSystemTray.
return m_TrayIcon.OnTrayNotification(wParam, lParam);
}
The menu used ( IDR_POPUP_MENU MENU PRELOAD DISCARDABLE
BEGIN
POPUP "POPUP_MENU"
BEGIN
MENUITEM "&About...", ID_APP_ABOUT
MENUITEM SEPARATOR
MENUITEM "&Options...", ID_APP_OPTIONS
MENUITEM SEPARATOR
MENUITEM "E&xit", ID_APP_EXIT
END
END
A single right click on the tray icon will bring up this menu, while a double click will send a Alternatively, you can NOTE on TrackPopupMenuMany people have had troubles using Anyway - to workaround this "feature", one must set the current window as the foreground window before calling So - what should have been a simple: TrackPopupMenu(hSubMenu, TPM_RIGHTBUTTON, pt.x,pt.y, 0, hDlg, NULL);
becomes SetForegroundWindow(hDlg); TrackPopupMenu(hSubMenu, TPM_RIGHTBUTTON, pt.x,pt.y, 0, hDlg, NULL); PostMessage(hDlg, WM_NULL, 0, 0); Refer to KB article "PRB: Menus for Notification Icons Don't Work Correctly" for more info. HistoryI updated the class so it would work in CE. A CE demo project is now included. Thomas Mooney helped make changes that allows the class to be used in an NT service. The problem occured when using Michael Dun added Windows 2000 support - namely those way cool balloon tips. The class now supports the new Windows2000 features, as well as CE (including popup menus from the tray). Two new functions for minimising an application to the system tray have also been added. 21 Sep 2000 - Matthew Ellis has improved the minimise-to-tray functionality by providing an improved version of the There is also a non-MFC version. Håkan Trygg has updated the class with the following: Instead of always sending the menu messages to the Main window ( The new functions are BOOL SetTargetWnd(CWnd* pTargetWnd);
CWnd* GetTargetWnd() const;
Also: the creation flags of the tray icon are saved so that if the icon needs to be recreated (settings change, taskbar recreated etc) then the icon will be created properly. 16 Jun 2002 Added the 3 Aug 2003 - added a bunch of small fixes as well as the
Håkan Trygg has also methods added to hold and change the menu. BOOL SetMenuText(UINT uiCmd, LPCTSTR szText);
BOOL SetMenuText(UINT uiCmd, UINT uiID);
BOOL CheckMenuItem(UINT uiCmd, BOOL bCheck);
BOOL EnableMenuItem(UINT uiCmd, BOOL bEnable);
BOOL DeleteMenu(UINT uiCmd);
The updated class files can be downloaded here. They have not been merged into the main class yet simply becuase I haven't had time to test - but I felt them important enough that I didn't want to delay making them available. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||