SystemTray.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /////////////////////////////////////////////////////////////////////////////
  2. // SystemTray.h : header file
  3. //
  4. // Written by Chris Maunder ([email protected])
  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. #ifndef _INCLUDED_SYSTEMTRAY_H_
  23. #define _INCLUDED_SYSTEMTRAY_H_
  24. #define ASSUME_IE5_OR_ABOVE
  25. #ifdef ASSUME_IE5_OR_ABOVE
  26. # ifndef _WIN32_IE
  27. # define _WIN32_IE 0x0500 // enable shell v5 features
  28. # elif _WIN32_IE < 0x0500
  29. # undef _WIN32_IE
  30. # define _WIN32_IE 0x0500 // enable shell v5 features
  31. # endif
  32. # ifdef NOTIFYICONDATA_V1_SIZE // If NOTIFYICONDATA_V1_SIZE, then we can use fun stuff
  33. # define SYSTEMTRAY_USEW2K
  34. # endif
  35. #endif
  36. #ifndef NIIF_NONE
  37. #define NIIF_NONE 0
  38. #endif
  39. // #include <afxwin.h>
  40. #include <afxtempl.h>
  41. #include <afxdisp.h> // COleDateTime
  42. /////////////////////////////////////////////////////////////////////////////
  43. // CSystemTray window
  44. class CSystemTray : public CWnd
  45. {
  46. // Construction/destruction
  47. public:
  48. CSystemTray();
  49. CSystemTray(CWnd* pWnd, UINT uCallbackMessage, LPCTSTR szTip, HICON icon, UINT uID,
  50. BOOL bhidden = FALSE,
  51. LPCTSTR szBalloonTip = NULL, LPCTSTR szBalloonTitle = NULL,
  52. DWORD dwBalloonIcon = NIIF_NONE, UINT uBalloonTimeout = 10);
  53. virtual ~CSystemTray();
  54. DECLARE_DYNAMIC(CSystemTray)
  55. // Operations
  56. public:
  57. BOOL Enabled() { return m_bEnabled; }
  58. BOOL Visible() { return !m_bHidden; }
  59. // Create the tray icon
  60. BOOL Create(CWnd* pParent, UINT uCallbackMessage, LPCTSTR szTip, HICON icon, UINT uID,
  61. BOOL bHidden = FALSE,
  62. LPCTSTR szBalloonTip = NULL, LPCTSTR szBalloonTitle = NULL,
  63. DWORD dwBalloonIcon = NIIF_NONE, UINT uBalloonTimeout = 10);
  64. // Change or retrieve the Tooltip text
  65. BOOL SetTooltipText(LPCTSTR pszTooltipText);
  66. BOOL SetTooltipText(UINT nID);
  67. CString GetTooltipText() const;
  68. // Change or retrieve the icon displayed
  69. BOOL SetIcon(HICON hIcon);
  70. BOOL SetIcon(LPCTSTR lpszIconName);
  71. BOOL SetIcon(UINT nIDResource);
  72. BOOL SetStandardIcon(LPCTSTR lpIconName);
  73. BOOL SetStandardIcon(UINT nIDResource);
  74. HICON GetIcon() const;
  75. void SetFocus();
  76. BOOL HideIcon();
  77. BOOL ShowIcon();
  78. BOOL AddIcon();
  79. BOOL RemoveIcon();
  80. BOOL MoveToRight();
  81. BOOL ShowBalloon(LPCTSTR szText, LPCTSTR szTitle = NULL,
  82. DWORD dwIcon = NIIF_NONE, UINT uTimeout = 10);
  83. // For icon animation
  84. BOOL SetIconList(UINT uFirstIconID, UINT uLastIconID);
  85. BOOL SetIconList(HICON* pHIconList, UINT nNumIcons);
  86. BOOL Animate(UINT nDelayMilliSeconds, int nNumSeconds = -1);
  87. BOOL StepAnimation();
  88. BOOL StopAnimation();
  89. // Change menu default item
  90. void GetMenuDefaultItem(UINT& uItem, BOOL& bByPos);
  91. BOOL SetMenuDefaultItem(UINT uItem, BOOL bByPos);
  92. // Change or retrieve the window to send notification messages to
  93. BOOL SetNotificationWnd(CWnd* pNotifyWnd);
  94. CWnd* GetNotificationWnd() const;
  95. // Change or retrieve the window to send menu commands to
  96. BOOL SetTargetWnd(CWnd* pTargetWnd);
  97. CWnd* GetTargetWnd() const;
  98. // Change or retrieve notification messages sent to the window
  99. BOOL SetCallbackMessage(UINT uCallbackMessage);
  100. UINT GetCallbackMessage() const;
  101. UINT GetTimerID() const { return m_nTimerID; }
  102. // Static functions
  103. public:
  104. static void MinimiseToTray(CWnd* pWnd);
  105. static void MaximiseFromTray(CWnd* pWnd);
  106. public:
  107. // Default handler for tray notification message
  108. virtual LRESULT OnTrayNotification(WPARAM uID, LPARAM lEvent);
  109. // Overrides
  110. // ClassWizard generated virtual function overrides
  111. //{{AFX_VIRTUAL(CSystemTray)
  112. protected:
  113. virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
  114. //}}AFX_VIRTUAL
  115. // Implementation
  116. protected:
  117. void Initialise();
  118. void InstallIconPending();
  119. // Implementation
  120. protected:
  121. NOTIFYICONDATA m_tnd;
  122. BOOL m_bEnabled; // does O/S support tray icon?
  123. BOOL m_bHidden; // Has the icon been hidden?
  124. BOOL m_bRemoved; // Has the icon been removed?
  125. BOOL m_bShowIconPending; // Show the icon once tha taskbar has been created
  126. BOOL m_bWin2K; // Use new W2K features?
  127. CWnd* m_pTargetWnd; // Window that menu commands are sent
  128. CArray<HICON, HICON> m_IconList;
  129. UINT m_uIDTimer;
  130. int m_nCurrentIcon;
  131. COleDateTime m_StartTime;
  132. int m_nAnimationPeriod;
  133. HICON m_hSavedIcon;
  134. UINT m_DefaultMenuItemID;
  135. BOOL m_DefaultMenuItemByPos;
  136. UINT m_uCreationFlags;
  137. // Static data
  138. protected:
  139. static BOOL RemoveTaskbarIcon(CWnd* pWnd);
  140. static const UINT m_nTimerID;
  141. static UINT m_nMaxTooltipLength;
  142. static const UINT m_nTaskbarCreatedMsg;
  143. static CWnd m_wndInvisible;
  144. static BOOL m_bShowWndAnimation;
  145. static BOOL GetW2K();
  146. #ifndef _WIN32_WCE
  147. static void GetTrayWndRect(LPRECT lprect);
  148. static BOOL GetDoWndAnimation();
  149. #endif
  150. // Generated message map functions
  151. protected:
  152. //{{AFX_MSG(CSystemTray)
  153. afx_msg void OnTimer(UINT nIDEvent);
  154. //}}AFX_MSG
  155. #ifndef _WIN32_WCE
  156. afx_msg void OnSettingChange(UINT uFlags, LPCTSTR lpszSection);
  157. #endif
  158. LRESULT OnTaskbarCreated(WPARAM wParam, LPARAM lParam);
  159. DECLARE_MESSAGE_MAP()
  160. };
  161. #endif
  162. /////////////////////////////////////////////////////////////////////////////