| 1 | ///////////////////////////////////////////////////////////////////////////// |
|---|
| 2 | // SystemTray.h : header file |
|---|
| 3 | // |
|---|
| 4 | // Written by Chris Maunder (cmaunder@mail.com) |
|---|
| 5 | // Copyright (c) 1998. |
|---|
| 6 | // |
|---|
| 7 | // This code may be used in compiled form in any way you desire. This |
|---|
| 8 | // file may be redistributed unmodified by any means PROVIDING it is |
|---|
| 9 | // not sold for profit without the authors written consent, and |
|---|
| 10 | // providing that this notice and the authors name is included. If |
|---|
| 11 | // the source code in this file is used in any commercial application |
|---|
| 12 | // then acknowledgement must be made to the author of this file |
|---|
| 13 | // (in whatever form you wish). |
|---|
| 14 | // |
|---|
| 15 | // This file is provided "as is" with no expressed or implied warranty. |
|---|
| 16 | // |
|---|
| 17 | // Expect bugs. |
|---|
| 18 | // |
|---|
| 19 | // Please use and enjoy. Please let me know of any bugs/mods/improvements |
|---|
| 20 | // that you have found/implemented and I will fix/incorporate them into this |
|---|
| 21 | // file. |
|---|
| 22 | |
|---|
| 23 | #ifndef _INCLUDED_SYSTEMTRAY_H_ |
|---|
| 24 | #define _INCLUDED_SYSTEMTRAY_H_ |
|---|
| 25 | |
|---|
| 26 | #ifdef NOTIFYICONDATA_V1_SIZE // If NOTIFYICONDATA_V1_SIZE, then we can use fun stuff |
|---|
| 27 | #define SYSTEMTRAY_USEW2K |
|---|
| 28 | #else |
|---|
| 29 | #define NIIF_NONE 0 |
|---|
| 30 | #endif |
|---|
| 31 | |
|---|
| 32 | // #include <afxwin.h> |
|---|
| 33 | #include <afxtempl.h> |
|---|
| 34 | #include <afxdisp.h> // COleDateTime |
|---|
| 35 | #define WM_TRAY_NOTIFY (WM_APP + 11) |
|---|
| 36 | #define WM_ICON_NOTIFY (WM_APP + 10) |
|---|
| 37 | ///////////////////////////////////////////////////////////////////////////// |
|---|
| 38 | // CSystemTray window |
|---|
| 39 | |
|---|
| 40 | class CSystemTray : public CWnd |
|---|
| 41 | { |
|---|
| 42 | // Construction/destruction |
|---|
| 43 | public: |
|---|
| 44 | CSystemTray(); |
|---|
| 45 | CSystemTray(CWnd* pWnd, UINT uCallbackMessage, LPCTSTR szTip, HICON icon, UINT uID, |
|---|
| 46 | BOOL bhidden = FALSE, |
|---|
| 47 | LPCTSTR szBalloonTip = NULL, LPCTSTR szBalloonTitle = NULL, |
|---|
| 48 | DWORD dwBalloonIcon = NIIF_NONE, UINT uBalloonTimeout = 10); |
|---|
| 49 | virtual ~CSystemTray(); |
|---|
| 50 | |
|---|
| 51 | DECLARE_DYNAMIC(CSystemTray) |
|---|
| 52 | |
|---|
| 53 | // Operations |
|---|
| 54 | public: |
|---|
| 55 | BOOL Enabled() { return m_bEnabled; } |
|---|
| 56 | BOOL Visible() { return !m_bHidden; } |
|---|
| 57 | |
|---|
| 58 | // Create the tray icon |
|---|
| 59 | BOOL Create(CWnd* pParent, UINT uCallbackMessage, LPCTSTR szTip, HICON icon, UINT uID, |
|---|
| 60 | BOOL bHidden = FALSE, |
|---|
| 61 | LPCTSTR szBalloonTip = NULL, LPCTSTR szBalloonTitle = NULL, |
|---|
| 62 | DWORD dwBalloonIcon = NIIF_NONE, UINT uBalloonTimeout = 10); |
|---|
| 63 | |
|---|
| 64 | // Change or retrieve the Tooltip text |
|---|
| 65 | BOOL SetTooltipText(LPCTSTR pszTooltipText); |
|---|
| 66 | BOOL SetTooltipText(UINT nID); |
|---|
| 67 | CString GetTooltipText() const; |
|---|
| 68 | |
|---|
| 69 | // Change or retrieve the icon displayed |
|---|
| 70 | BOOL SetIcon(HICON hIcon); |
|---|
| 71 | BOOL SetIcon(LPCTSTR lpszIconName); |
|---|
| 72 | BOOL SetIcon(UINT nIDResource); |
|---|
| 73 | BOOL SetStandardIcon(LPCTSTR lpIconName); |
|---|
| 74 | BOOL SetStandardIcon(UINT nIDResource); |
|---|
| 75 | HICON GetIcon() const; |
|---|
| 76 | |
|---|
| 77 | void SetFocus(); |
|---|
| 78 | BOOL HideIcon(); |
|---|
| 79 | BOOL ShowIcon(); |
|---|
| 80 | BOOL AddIcon(); |
|---|
| 81 | BOOL RemoveIcon(); |
|---|
| 82 | BOOL MoveToRight(); |
|---|
| 83 | |
|---|
| 84 | BOOL ShowBalloon(LPCTSTR szText, LPCTSTR szTitle = NULL, |
|---|
| 85 | DWORD dwIcon = NIIF_NONE, UINT uTimeout = 10); |
|---|
| 86 | |
|---|
| 87 | // For icon animation |
|---|
| 88 | BOOL SetIconList(UINT uFirstIconID, UINT uLastIconID); |
|---|
| 89 | BOOL SetIconList(HICON* pHIconList, UINT nNumIcons); |
|---|
| 90 | BOOL Animate(UINT nDelayMilliSeconds, int nNumSeconds = -1); |
|---|
| 91 | BOOL StepAnimation(); |
|---|
| 92 | BOOL StopAnimation(); |
|---|
| 93 | |
|---|
| 94 | // Change menu default item |
|---|
| 95 | void GetMenuDefaultItem(UINT& uItem, BOOL& bByPos); |
|---|
| 96 | BOOL SetMenuDefaultItem(UINT uItem, BOOL bByPos); |
|---|
| 97 | |
|---|
| 98 | // Change or retrieve the window to send notification messages to |
|---|
| 99 | BOOL SetNotificationWnd(CWnd* pNotifyWnd); |
|---|
| 100 | CWnd* GetNotificationWnd() const; |
|---|
| 101 | |
|---|
| 102 | // Change or retrieve the window to send menu commands to |
|---|
| 103 | BOOL SetTargetWnd(CWnd* pTargetWnd); |
|---|
| 104 | CWnd* GetTargetWnd() const; |
|---|
| 105 | |
|---|
| 106 | // Change or retrieve notification messages sent to the window |
|---|
| 107 | BOOL SetCallbackMessage(UINT uCallbackMessage); |
|---|
| 108 | UINT GetCallbackMessage() const; |
|---|
| 109 | |
|---|
| 110 | UINT_PTR GetTimerID() const { return m_nTimerID; } |
|---|
| 111 | |
|---|
| 112 | // Static functions |
|---|
| 113 | public: |
|---|
| 114 | static void MinimiseToTray(CWnd* pWnd, BOOL bForceAnimation = FALSE); |
|---|
| 115 | static void MaximiseFromTray(CWnd* pWnd, BOOL bForceAnimation = FALSE); |
|---|
| 116 | |
|---|
| 117 | public: |
|---|
| 118 | // Default handler for tray notification message |
|---|
| 119 | virtual LRESULT OnTrayNotification(WPARAM uID, LPARAM lEvent); |
|---|
| 120 | |
|---|
| 121 | // Overrides |
|---|
| 122 | // ClassWizard generated virtual function overrides |
|---|
| 123 | //{{AFX_VIRTUAL(CSystemTray) |
|---|
| 124 | protected: |
|---|
| 125 | virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam); |
|---|
| 126 | //}}AFX_VIRTUAL |
|---|
| 127 | |
|---|
| 128 | // Implementation |
|---|
| 129 | protected: |
|---|
| 130 | void Initialise(); |
|---|
| 131 | void InstallIconPending(); |
|---|
| 132 | |
|---|
| 133 | virtual void CustomizeMenu(CMenu*) {} // Used for customizing the menu |
|---|
| 134 | |
|---|
| 135 | // Implementation |
|---|
| 136 | protected: |
|---|
| 137 | NOTIFYICONDATA m_tnd; |
|---|
| 138 | BOOL m_bEnabled; // does O/S support tray icon? |
|---|
| 139 | BOOL m_bHidden; // Has the icon been hidden? |
|---|
| 140 | BOOL m_bRemoved; // Has the icon been removed? |
|---|
| 141 | BOOL m_bShowIconPending; // Show the icon once tha taskbar has been created |
|---|
| 142 | BOOL m_bWin2K; // Use new W2K features? |
|---|
| 143 | CWnd* m_pTargetWnd; // Window that menu commands are sent |
|---|
| 144 | |
|---|
| 145 | CArray<HICON, HICON> m_IconList; |
|---|
| 146 | UINT_PTR m_uIDTimer; |
|---|
| 147 | INT_PTR m_nCurrentIcon; |
|---|
| 148 | COleDateTime m_StartTime; |
|---|
| 149 | UINT m_nAnimationPeriod; |
|---|
| 150 | HICON m_hSavedIcon; |
|---|
| 151 | UINT m_DefaultMenuItemID; |
|---|
| 152 | BOOL m_DefaultMenuItemByPos; |
|---|
| 153 | UINT m_uCreationFlags; |
|---|
| 154 | |
|---|
| 155 | // Static data |
|---|
| 156 | protected: |
|---|
| 157 | static BOOL RemoveTaskbarIcon(CWnd* pWnd); |
|---|
| 158 | |
|---|
| 159 | static const UINT m_nTimerID; |
|---|
| 160 | static UINT m_nMaxTooltipLength; |
|---|
| 161 | static const UINT m_nTaskbarCreatedMsg; |
|---|
| 162 | static CWnd m_wndInvisible; |
|---|
| 163 | |
|---|
| 164 | static BOOL GetW2K(); |
|---|
| 165 | #ifndef _WIN32_WCE |
|---|
| 166 | static void GetTrayWndRect(LPRECT lprect); |
|---|
| 167 | static BOOL GetDoWndAnimation(); |
|---|
| 168 | #endif |
|---|
| 169 | |
|---|
| 170 | // Generated message map functions |
|---|
| 171 | protected: |
|---|
| 172 | //{{AFX_MSG(CSystemTray) |
|---|
| 173 | afx_msg void OnTimer(UINT_PTR nIDEvent); |
|---|
| 174 | //}}AFX_MSG |
|---|
| 175 | #ifndef _WIN32_WCE |
|---|
| 176 | afx_msg void OnSettingChange(UINT uFlags, LPCTSTR lpszSection); |
|---|
| 177 | #endif |
|---|
| 178 | LRESULT OnTaskbarCreated(WPARAM wParam, LPARAM lParam); |
|---|
| 179 | DECLARE_MESSAGE_MAP() |
|---|
| 180 | }; |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | #endif |
|---|
| 184 | |
|---|
| 185 | ///////////////////////////////////////////////////////////////////////////// |
|---|