NTray.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. Module : NTray.h
  3. Purpose: Interface for a MFC class to encapsulate Shell_NotifyIcon
  4. Created: PJN / 14-05-1997
  5. Copyright (c) 1997 - 2016 by PJ Naughter (Web: www.naughter.com, Email: [email protected])
  6. All rights reserved.
  7. Copyright / Usage Details:
  8. You are allowed to include the source code in any product (commercial, shareware, freeware or otherwise)
  9. when your product is released in binary form. You are allowed to modify the source code in any way you want
  10. except you cannot modify the copyright details at the top of each module. If you want to distribute source
  11. code with your application, then you are only allowed to distribute versions released by the author. This is
  12. to maintain a single distribution point for the source code.
  13. */
  14. /////////////////////////// Macros / Defines ///////////////////////////
  15. #pragma once
  16. #ifndef _NTRAY_H__
  17. #define _NTRAY_H__
  18. #ifndef CTRAYNOTIFYICON_EXT_CLASS
  19. #define CTRAYNOTIFYICON_EXT_CLASS
  20. #endif //#ifndef CTRAYNOTIFYICON_EXT_CLASS
  21. #ifndef _In_
  22. #define _In_
  23. #endif //#ifndef _In_
  24. #ifndef _In_opt_
  25. #define _In_opt_
  26. #endif //#ifndef _In_opt_
  27. #ifndef _Out_
  28. #define _Out_
  29. #endif //#ifndef _Out_
  30. #ifndef _Inout_
  31. #define _Inout_
  32. #endif //#ifndef _Inout_
  33. #ifndef __ATLWIN_H__
  34. #pragma message("CTrayNotifyIcon as of v1.51 requires ATL support to implement its functionality. If your project is MFC only, then you need to update it to include ATL support")
  35. #endif //#ifndef __ATLWIN_H__
  36. #ifndef _AFX
  37. #ifndef __ATLMISC_H__
  38. #pragma message("To avoid this message, please put atlmisc.h (part of WTL) in your pre compiled header (normally stdafx.h)")
  39. #include <atlmisc.h> //If you do want to use CTrayNotifyIcon independent of MFC, then you need to download and install WTL from http://sourceforge.net/projects/wtl
  40. #endif //#ifndef __ATLMISC_H__
  41. #endif //#ifndef _AFX
  42. #include "TrayWnd.h"
  43. /////////////////////////// Classes ///////////////////////////////////////////
  44. //the actual tray notification class wrapper
  45. class CTRAYNOTIFYICON_EXT_CLASS CTrayNotifyIcon : public CWindowImpl<CTrayNotifyIcon>
  46. {
  47. public:
  48. //Enums / Typedefs
  49. #ifndef _AFX
  50. typedef _CSTRING_NS::CString String;
  51. #else
  52. typedef CString String;
  53. #endif //#ifndef _AFX
  54. enum BalloonStyle
  55. {
  56. Warning,
  57. Error,
  58. Info,
  59. None,
  60. User
  61. };
  62. //We use our own definitions of the NOTIFYICONDATA structs so that
  63. //we can use all the functionality without requiring client code to
  64. //define _WIN32_IE >= 0x500
  65. typedef struct _NOTIFYICONDATA_1 //The version of the structure supported by Shell v4
  66. {
  67. DWORD cbSize;
  68. HWND hWnd;
  69. UINT uID;
  70. UINT uFlags;
  71. UINT uCallbackMessage;
  72. HICON hIcon;
  73. TCHAR szTip[64];
  74. } NOTIFYICONDATA_1;
  75. typedef struct _NOTIFYICONDATA_2 //The version of the structure supported by Shell v5
  76. {
  77. DWORD cbSize;
  78. HWND hWnd;
  79. UINT uID;
  80. UINT uFlags;
  81. UINT uCallbackMessage;
  82. HICON hIcon;
  83. TCHAR szTip[128];
  84. DWORD dwState;
  85. DWORD dwStateMask;
  86. TCHAR szInfo[256];
  87. union
  88. {
  89. UINT uTimeout;
  90. UINT uVersion;
  91. } DUMMYUNIONNAME;
  92. TCHAR szInfoTitle[64];
  93. DWORD dwInfoFlags;
  94. } NOTIFYICONDATA_2;
  95. typedef struct _NOTIFYICONDATA_3 //The version of the structure supported by Shell v6
  96. {
  97. DWORD cbSize;
  98. HWND hWnd;
  99. UINT uID;
  100. UINT uFlags;
  101. UINT uCallbackMessage;
  102. HICON hIcon;
  103. TCHAR szTip[128];
  104. DWORD dwState;
  105. DWORD dwStateMask;
  106. TCHAR szInfo[256];
  107. union
  108. {
  109. UINT uTimeout;
  110. UINT uVersion;
  111. } DUMMYUNIONNAME;
  112. TCHAR szInfoTitle[64];
  113. DWORD dwInfoFlags;
  114. GUID guidItem;
  115. } NOTIFYICONDATA_3;
  116. typedef struct _NOTIFYICONDATA_4 //The version of the structure supported by Shell v7
  117. {
  118. DWORD cbSize;
  119. HWND hWnd;
  120. UINT uID;
  121. UINT uFlags;
  122. UINT uCallbackMessage;
  123. HICON hIcon;
  124. TCHAR szTip[128];
  125. DWORD dwState;
  126. DWORD dwStateMask;
  127. TCHAR szInfo[256];
  128. union
  129. {
  130. UINT uTimeout;
  131. UINT uVersion;
  132. } DUMMYUNIONNAME;
  133. TCHAR szInfoTitle[64];
  134. DWORD dwInfoFlags;
  135. GUID guidItem;
  136. HICON hBalloonIcon;
  137. } NOTIFYICONDATA_4;
  138. DECLARE_WND_CLASS(_T("TrayNotifyIconClass"))
  139. //Constructors / Destructors
  140. CTrayNotifyIcon();
  141. virtual ~CTrayNotifyIcon();
  142. //Create the tray icon
  143. #ifdef _AFX
  144. BOOL Create(_In_ CWnd* pNotifyWnd, _In_ UINT uID, _In_ LPCTSTR pszTooltipText, _In_ HICON hIcon, _In_ UINT nNotifyMessage, _In_ UINT uMenuID = 0, _In_ BOOL bShow = TRUE);
  145. BOOL Create(_In_ CWnd* pNotifyWnd, _In_ UINT uID, _In_ LPCTSTR pszTooltipText, _In_ CBitmap* pBitmap, _In_ UINT nNotifyMessage, _In_ UINT uMenuID = 0, _In_ BOOL bShow = TRUE);
  146. BOOL Create(_In_ CWnd* pNotifyWnd, _In_ UINT uID, _In_ LPCTSTR pszTooltipText, _In_ HICON* phIcons, _In_ int nNumIcons, _In_ DWORD dwDelay, _In_ UINT nNotifyMessage, _In_ UINT uMenuID = 0, _In_ BOOL bShow = TRUE);
  147. BOOL Create(_In_ CWnd* pNotifyWnd, _In_ UINT uID, _In_ LPCTSTR pszTooltipText, _In_ LPCTSTR pszBalloonText, _In_ LPCTSTR pszBalloonCaption, _In_ UINT nTimeout, _In_ BalloonStyle style, _In_ HICON hIcon, _In_ UINT nNotifyMessage, _In_ UINT uMenuID = 0, _In_ BOOL bNoSound = FALSE, _In_ BOOL bLargeIcon = FALSE, _In_ BOOL bRealtime = FALSE, _In_opt_ HICON hBalloonIcon = NULL, _In_ BOOL bQuietTime = FALSE, _In_ BOOL bShow = TRUE);
  148. BOOL Create(_In_ CWnd* pNotifyWnd, _In_ UINT uID, _In_ LPCTSTR pszTooltipText, _In_ LPCTSTR pszBalloonText, _In_ LPCTSTR pszBalloonCaption, _In_ UINT nTimeout, _In_ BalloonStyle style, _In_ CBitmap* pBitmap, _In_ UINT nNotifyMessage, _In_ UINT uMenuID = 0, _In_ BOOL bNoSound = FALSE, _In_ BOOL bLargeIcon = FALSE, _In_ BOOL bRealtime = FALSE, _In_opt_ HICON hBalloonIcon = NULL, _In_ BOOL bQuietTime = FALSE, _In_ BOOL bShow = TRUE);
  149. BOOL Create(_In_ CWnd* pNotifyWnd, _In_ UINT uID, _In_ LPCTSTR pszTooltipText, _In_ LPCTSTR pszBalloonText, _In_ LPCTSTR pszBalloonCaption, _In_ UINT nTimeout, _In_ BalloonStyle style, _In_ HICON* phIcons, _In_ int nNumIcons, _In_ DWORD dwDelay, _In_ UINT nNotifyMessage, _In_ UINT uMenuID = 0, _In_ BOOL bNoSound = FALSE, _In_ BOOL bLargeIcon = FALSE, _In_ BOOL bRealtime = FALSE, _In_opt_ HICON hBalloonIcon = NULL, _In_ BOOL bQuietTime = FALSE, _In_ BOOL bShow = TRUE);
  150. #else
  151. BOOL Create(_In_ CWindow* pNotifyWnd, _In_ UINT uID, _In_ LPCTSTR pszTooltipText, _In_ HICON hIcon, _In_ UINT nNotifyMessage, _In_ UINT uMenuID = 0, _In_ BOOL bShow = TRUE);
  152. BOOL Create(_In_ CWindow* pNotifyWnd, _In_ UINT uID, _In_ LPCTSTR pszTooltipText, _In_ CBitmap* pBitmap, _In_ UINT nNotifyMessage, _In_ UINT uMenuID = 0, _In_ BOOL bShow = TRUE);
  153. BOOL Create(_In_ CWindow* pNotifyWnd, _In_ UINT uID, _In_ LPCTSTR pszTooltipText, _In_ HICON* phIcons, _In_ int nNumIcons, _In_ DWORD dwDelay, _In_ UINT nNotifyMessage, _In_ UINT uMenuID = 0, _In_ BOOL bShow = TRUE);
  154. BOOL Create(_In_ CWindow* pNotifyWnd, _In_ UINT uID, _In_ LPCTSTR pszTooltipText, _In_ LPCTSTR pszBalloonText, _In_ LPCTSTR pszBalloonCaption, _In_ UINT nTimeout, _In_ BalloonStyle style, _In_ HICON hIcon, _In_ UINT nNotifyMessage, _In_ UINT uMenuID = 0, _In_ BOOL bNoSound = FALSE, _In_ BOOL bLargeIcon = FALSE, _In_ BOOL bRealtime = FALSE, _In_opt_ HICON hBalloonIcon = NULL, _In_ BOOL bQuietTime = FALSE, _In_ BOOL bShow = TRUE);
  155. BOOL Create(_In_ CWindow* pNotifyWnd, _In_ UINT uID, _In_ LPCTSTR pszTooltipText, _In_ LPCTSTR pszBalloonText, _In_ LPCTSTR pszBalloonCaption, _In_ UINT nTimeout, _In_ BalloonStyle style, _In_ CBitmap* pBitmap, _In_ UINT nNotifyMessage, _In_ UINT uMenuID = 0, _In_ BOOL bNoSound = FALSE, _In_ BOOL bLargeIcon = FALSE, _In_ BOOL bRealtime = FALSE, _In_opt_ HICON hBalloonIcon = NULL, _In_ BOOL bQuietTime = FALSE, _In_ BOOL bShow = TRUE);
  156. BOOL Create(_In_ CWindow* pNotifyWnd, _In_ UINT uID, _In_ LPCTSTR pszTooltipText, _In_ LPCTSTR pszBalloonText, _In_ LPCTSTR pszBalloonCaption, _In_ UINT nTimeout, _In_ BalloonStyle style, _In_ HICON* phIcons, _In_ int nNumIcons, _In_ DWORD dwDelay, _In_ UINT nNotifyMessage, _In_ UINT uMenuID = 0, _In_ BOOL bNoSound = FALSE, _In_ BOOL bLargeIcon = FALSE, _In_ BOOL bRealtime = FALSE, _In_opt_ HICON hBalloonIcon = NULL, _In_ BOOL bQuietTime = FALSE, _In_ BOOL bShow = TRUE);
  157. #endif //#ifdef _AFX
  158. BOOL RemoveTaskbarIcon(CWnd* pWnd);
  159. void MinimiseToTray(CWnd* pWnd);
  160. void MaximiseFromTray(CWnd* pWnd);
  161. //Sets or gets the Tooltip text
  162. BOOL SetTooltipText(_In_ LPCTSTR pszTooltipText);
  163. BOOL SetTooltipText(_In_ UINT nID);
  164. String GetTooltipText() const;
  165. int GetTooltipMaxSize();
  166. //Sets or gets the icon displayed
  167. BOOL SetIcon(_In_ HICON hIcon);
  168. BOOL SetIcon(_In_ CBitmap* pBitmap);
  169. BOOL SetIcon(_In_ LPCTSTR lpIconName);
  170. BOOL SetIcon(_In_ UINT nIDResource);
  171. BOOL SetIcon(_In_ HICON* phIcons, _In_ int nNumIcons, _In_ DWORD dwDelay);
  172. BOOL SetStandardIcon(_In_ LPCTSTR lpIconName);
  173. BOOL SetStandardIcon(_In_ UINT nIDResource);
  174. HICON GetIcon() const;
  175. BOOL UsingAnimatedIcon() const;
  176. //Sets or gets the window to send notification messages to
  177. #ifdef _AFX
  178. BOOL SetNotificationWnd(_In_ CWnd* pNotifyWnd);
  179. CWnd* GetNotificationWnd() const;
  180. #else
  181. BOOL SetNotificationWnd(_In_ CWindow* pNotifyWnd);
  182. CWindow* GetNotificationWnd() const;
  183. #endif //#ifdef _AFX
  184. //Modification of the tray icons
  185. BOOL Delete(_In_ BOOL bCloseHelperWindow = TRUE);
  186. BOOL Create(_In_ BOOL bShow = TRUE);
  187. BOOL Hide();
  188. BOOL Show();
  189. //Dynamic tray icon support
  190. HICON BitmapToIcon(_In_ CBitmap* pBitmap);
  191. static BOOL GetDynamicDCAndBitmap(_In_ CDC* pDC, _In_ CBitmap* pBitmap);
  192. //Modification of the menu to use as the context menu
  193. void SetMenu(_In_ HMENU hMenu, UINT menuId);
  194. CMenu& GetMenu();
  195. void SetDefaultMenuItem(_In_ UINT uItem, _In_ BOOL fByPos);
  196. void GetDefaultMenuItem(_Out_ UINT& uItem, _Out_ BOOL& fByPos) { uItem = m_nDefaultMenuItem; fByPos = m_bDefaultMenuItemByPos; };
  197. //Default handler for tray notification message
  198. virtual LRESULT OnTrayNotification(WPARAM uID, LPARAM lEvent);
  199. //Status information
  200. BOOL IsShowing() const { return !IsHidden(); };
  201. BOOL IsHidden() const { return m_bHidden; };
  202. //Sets or gets the Balloon style tooltip settings
  203. BOOL SetBalloonDetails(_In_ LPCTSTR pszBalloonText, _In_ LPCTSTR pszBalloonCaption, _In_ BalloonStyle style, _In_ UINT nTimeout, _In_ HICON hUserIcon = NULL, _In_ BOOL bNoSound = TRUE, _In_ BOOL bLargeIcon = FALSE, _In_ BOOL bRealtime = FALSE, _In_ HICON hBalloonIcon = NULL);
  204. String GetBalloonText() const;
  205. String GetBalloonCaption() const;
  206. BalloonStyle GetBalloonStyle() const;
  207. UINT GetBalloonTimeout() const;
  208. //Other functionality
  209. BOOL SetVersion(_In_ UINT uVersion);
  210. BOOL SetFocus();
  211. //Helper functions to load tray icon from resources
  212. static HICON LoadIcon(_In_ LPCTSTR lpIconName, _In_ BOOL bLargeIcon = FALSE);
  213. static HICON LoadIcon(_In_ UINT nIDResource, _In_ BOOL bLargeIcon = FALSE);
  214. static HICON LoadIcon(_In_ HINSTANCE hInstance, _In_ LPCTSTR lpIconName, _In_ BOOL bLargeIcon = FALSE);
  215. static HICON LoadIcon(_In_ HINSTANCE hInstance, _In_ UINT nIDResource, _In_ BOOL bLargeIcon = FALSE);
  216. protected:
  217. //Methods
  218. BOOL CreateHelperWindow();
  219. BOOL StartAnimation(_In_ HICON* phIcons, _In_ int nNumIcons, _In_ DWORD dwDelay);
  220. void StopAnimation();
  221. HICON GetCurrentAnimationIcon() const;
  222. virtual BOOL ProcessWindowMessage(_In_ HWND hWnd, _In_ UINT nMsg, _In_ WPARAM wParam, _In_ LPARAM lParam, _Inout_ LRESULT& lResult, _In_ DWORD dwMsgMapID);
  223. LRESULT OnTaskbarCreated(WPARAM wParam, LPARAM lParam);
  224. void OnTimer(UINT_PTR nIDEvent);
  225. void OnDestroy();
  226. DWORD GetNOTIFYICONDATASizeForOS();
  227. static CTrayWnd m_wndInvisible;
  228. //Enums
  229. enum ShellVersion
  230. {
  231. Version4 = 0, //PreWin2k
  232. Version5 = 1, //Win2k
  233. Version6 = 2, //XP
  234. VersionVista = 3, //Vista
  235. Version7 = 4, //Windows7
  236. };
  237. //Member variables
  238. NOTIFYICONDATA_4 m_NotifyIconData;
  239. BOOL m_bCreated;
  240. BOOL m_bHidden;
  241. #ifdef _AFX
  242. CWnd* m_pNotificationWnd;
  243. #else
  244. CWindow* m_pNotificationWnd;
  245. #endif //#ifdef _AFX
  246. CMenu m_Menu;
  247. UINT m_nDefaultMenuItem;
  248. BOOL m_bDefaultMenuItemByPos;
  249. ShellVersion m_ShellVersion;
  250. HICON m_hDynamicIcon; //Our cached copy of the last icon created with BitmapToIcon
  251. ATL::CHeapPtr<HICON> m_Icons;
  252. int m_nNumIcons;
  253. UINT_PTR m_nTimerID;
  254. int m_nCurrentIconIndex;
  255. int m_nTooltipMaxSize;
  256. };
  257. #endif //#ifndef _NTRAY_H__