source: trunk/src/NTray.hpp @ 390

Revision 390, 9.6 KB checked in by Eoin, 12 years ago (diff)

Adding some WTL:: namespace prefixes.

Line 
1/*
2Module : NTray.h
3Purpose: Interface for a MFC class to encapsulate Shell_NotifyIcon
4Created: PJN / 14-05-1997
5History: None
6
7Copyright (c) 1997 - 2006 by PJ Naughter (Web: www.naughter.com, Email: pjna@naughter.com)
8
9All rights reserved.
10
11Copyright / Usage Details:
12
13You are allowed to include the source code in any product (commercial, shareware, freeware or otherwise)
14when your product is released in binary form. You are allowed to modify the source code in any way you want
15except you cannot modify the copyright details at the top of each module. If you want to distribute source
16code with your application, then you are only allowed to distribute versions released by the author. This is
17to maintain a single distribution point for the source code.
18
19*/
20
21
22/////////////////////////// Macros / Defines ///////////////////////////
23
24#ifndef _NTRAY_H__
25#define _NTRAY_H__
26
27#ifndef CTRAYNOTIFYICON_EXT_CLASS
28#define CTRAYNOTIFYICON_EXT_CLASS
29#endif
30
31#ifndef CTRAYNOTIFYICON_EXT_API
32#define CTRAYNOTIFYICON_EXT_API
33#endif
34
35#ifndef __ATLWIN_H__
36#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")
37#endif
38
39#ifndef _AFX
40#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
41#define CTrayNotifyIconString WTL::CString
42#else
43#define CTrayNotifyIconString CString
44#endif
45
46
47/////////////////////////// Classes ///////////////////////////////////////////
48
49//forward declaration
50class CTrayNotifyIcon;
51
52//Class which handles subclassing the top level window and handles the timer
53//required for tray icon animation and taskbar creation message
54class CTRAYNOTIFYICON_EXT_CLASS CTrayIconHooker : public CWindowImpl<CTrayIconHooker>
55{
56public:
57//Constructors / Destructors
58  CTrayIconHooker();
59  ~CTrayIconHooker();
60
61//Methods
62#ifdef _AFX
63  BOOL Init(CTrayNotifyIcon* pTrayIcon, CWnd* pNotifyWnd);
64#else
65  BOOL Init(CTrayNotifyIcon* pTrayIcon, CWindow* pNotifyWnd);
66#endif 
67  void StartUsingAnimation(HICON* phIcons, int nNumIcons, DWORD dwDelay);
68  void StopUsingAnimation();
69  BOOL UsingAnimatedIcon() const;
70  HICON GetCurrentIcon() const;
71
72protected:
73//Methods
74  virtual BOOL ProcessWindowMessage(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam, LRESULT& lResult, DWORD dwMsgMapID);
75  void OnTimer(UINT_PTR nIDEvent);
76  LRESULT OnTaskbarCreated(WPARAM wParam, LPARAM lParam);
77
78//Member variables
79  CTrayNotifyIcon* m_pTrayIcon;
80  HICON*           m_phIcons;
81  int              m_nNumIcons;
82  UINT_PTR         m_nTimerID;
83  int              m_nCurrentIconIndex;
84};
85
86//the actual tray notification class wrapper
87class CTRAYNOTIFYICON_EXT_CLASS CTrayNotifyIcon
88{
89public:
90//Enums / Typedefs
91#ifndef CTRAYNOTIFYICON_NOWIN2K
92  enum BalloonStyle
93  {
94    Warning,
95    Error,
96    Info,
97    None,
98    User
99  };
100#endif
101
102//We use our own definitions of the NOTIFYICONDATA structs so that
103//we can use all the functionality without requiring client code to
104//define _WIN32_IE >= 0x500
105  typedef struct _NOTIFYICONDATA_1 //The version of the structure supported by Shell v4
106  {
107    DWORD cbSize;
108    HWND hWnd;
109    UINT uID;
110    UINT uFlags;
111    UINT uCallbackMessage;
112    HICON hIcon;
113    TCHAR szTip[64];
114  } NOTIFYICONDATA_1;
115
116  typedef struct _NOTIFYICONDATA_2 //The version of the structure supported by Shell v5
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  } NOTIFYICONDATA_2;
136
137  typedef struct _NOTIFYICONDATA_3 //The version of the structure supported by Shell v6
138  {
139    DWORD cbSize;
140    HWND hWnd;
141    UINT uID;
142    UINT uFlags;
143    UINT uCallbackMessage;
144    HICON hIcon;
145    TCHAR szTip[128];
146    DWORD dwState;
147    DWORD dwStateMask;
148    TCHAR szInfo[256];
149    union 
150    {
151      UINT uTimeout;
152      UINT uVersion;
153    } DUMMYUNIONNAME;
154    TCHAR szInfoTitle[64];
155    DWORD dwInfoFlags;
156    GUID guidItem;
157  } NOTIFYICONDATA_3;
158
159//Constructors / Destructors
160  CTrayNotifyIcon();
161  virtual ~CTrayNotifyIcon();
162
163//Create the tray icon
164#ifdef _AFX
165  BOOL Create(CWnd* pNotifyWnd, UINT uID, LPCTSTR pszTooltipText, HICON hIcon, UINT nNotifyMessage, UINT uMenuID = 0);
166  BOOL Create(CWnd* pNotifyWnd, UINT uID, LPCTSTR pszTooltipText, WTL::CBitmap* pBitmap, UINT nNotifyMessage, UINT uMenuID = 0);
167  BOOL Create(CWnd* pNotifyWnd, UINT uID, LPCTSTR pszTooltipText, HICON* phIcons, int nNumIcons, DWORD dwDelay, UINT nNotifyMessage, UINT uMenuID = 0);
168  BOOL 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);                 
169  BOOL Create(CWnd* pNotifyWnd, UINT uID, LPCTSTR pszTooltipText, LPCTSTR pszBalloonText, LPCTSTR pszBalloonCaption, UINT nTimeout, BalloonStyle style, WTL::CBitmap* pBitmap, UINT nNotifyMessage, UINT uMenuID = 0, BOOL bNoSound = FALSE);
170  BOOL 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);
171#else
172  BOOL Create(CWindow* pNotifyWnd, UINT uID, LPCTSTR pszTooltipText, HICON hIcon, UINT nNotifyMessage, UINT uMenuID = 0);
173  BOOL Create(CWindow* pNotifyWnd, UINT uID, LPCTSTR pszTooltipText, WTL::CBitmap* pBitmap, UINT nNotifyMessage, UINT uMenuID = 0);
174  BOOL Create(CWindow* pNotifyWnd, UINT uID, LPCTSTR pszTooltipText, HICON* phIcons, int nNumIcons, DWORD dwDelay, UINT nNotifyMessage, UINT uMenuID = 0);
175  BOOL Create(CWindow* pNotifyWnd, UINT uID, LPCTSTR pszTooltipText, LPCTSTR pszBalloonText, LPCTSTR pszBalloonCaption, UINT nTimeout, BalloonStyle style, HICON hIcon, UINT nNotifyMessage, UINT uMenuID = 0, BOOL bNoSound = FALSE);                 
176  BOOL Create(CWindow* pNotifyWnd, UINT uID, LPCTSTR pszTooltipText, LPCTSTR pszBalloonText, LPCTSTR pszBalloonCaption, UINT nTimeout, BalloonStyle style, WTL::CBitmap* pBitmap, UINT nNotifyMessage, UINT uMenuID = 0, BOOL bNoSound = FALSE);
177  BOOL Create(CWindow* 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);
178#endif 
179
180//Sets or gets the Tooltip text
181  BOOL                  SetTooltipText(LPCTSTR pszTooltipText);
182  BOOL                  SetTooltipText(UINT nID);
183  CTrayNotifyIconString GetTooltipText() const;
184
185//Sets or gets the icon displayed
186  BOOL SetIcon(HICON hIcon);
187  BOOL SetIcon(WTL::CBitmap* pBitmap);
188  BOOL SetIcon(LPCTSTR lpIconName);
189  BOOL SetIcon(UINT nIDResource);
190  BOOL SetIcon(HICON* phIcons, int nNumIcons, DWORD dwDelay);
191  BOOL SetStandardIcon(LPCTSTR lpIconName);
192  BOOL SetStandardIcon(UINT nIDResource);
193  HICON GetIcon() const;
194  BOOL UsingAnimatedIcon() const;
195
196//Sets or gets the window to send notification messages to
197#ifdef _AFX
198  BOOL SetNotificationWnd(CWnd* pNotifyWnd);
199  CWnd* GetNotificationWnd() const;
200#else
201  BOOL SetNotificationWnd(CWindow* pNotifyWnd);
202  ATL::CWindow* GetNotificationWnd() const;
203#endif
204
205//Modification of the tray icons
206  BOOL Delete();
207  BOOL Create();
208  BOOL Hide();
209  BOOL Show();
210
211//Dynamic tray icon support
212  HICON BitmapToIcon(WTL::CBitmap* pBitmap);
213  static BOOL GetDynamicDCAndBitmap(WTL::CDC* pDC, WTL::CBitmap* pBitmap);
214
215//Modification of the menu to use as the context menu
216  void SetMenu(HMENU hMenu);
217  WTL::CMenu& GetMenu();
218  void SetDefaultMenuItem(UINT uItem, BOOL fByPos);
219  void GetDefaultMenuItem(UINT& uItem, BOOL& fByPos) { uItem = m_nDefaultMenuItem; fByPos = m_bDefaultMenuItemByPos; };
220
221//Default handler for tray notification message
222  virtual LRESULT OnTrayNotification(WPARAM uID, LPARAM lEvent);
223
224//Status information
225  BOOL IsShowing() const { return !IsHidden(); };
226  BOOL IsHidden() const { return m_bHidden; };
227
228//Sets or gets the Balloon style tooltip settings
229  BOOL                  SetBalloonDetails(LPCTSTR pszBalloonText, LPCTSTR pszBalloonCaption, BalloonStyle style, UINT nTimeout, HICON hUserIcon=NULL, BOOL bNoSound=FALSE);
230  CTrayNotifyIconString GetBalloonText() const;
231  CTrayNotifyIconString GetBalloonCaption() const;
232  BalloonStyle          GetBalloonStyle() const;
233  UINT                  GetBalloonTimeout() const;
234
235//Other functionality
236  BOOL SetVersion(UINT uVersion);
237  BOOL SetFocus();
238  static DWORD GetShellVersion();
239
240//Helper functions to load tray icon from resources
241  static HICON LoadIconResource(LPCTSTR lpIconName);
242  static HICON LoadIconResource(UINT nIDResource);
243
244protected:
245//Member variables
246  NOTIFYICONDATA_3 m_NotifyIconData;
247  BOOL             m_bCreated;
248  BOOL             m_bHidden;
249#ifdef _AFX 
250  CWnd*            m_pNotificationWnd;
251#else
252  CWindow*         m_pNotificationWnd;
253#endif 
254  CTrayIconHooker  m_HookWnd;
255  WTL::CMenu       m_Menu;
256  UINT             m_nDefaultMenuItem;
257  BOOL             m_bDefaultMenuItemByPos;
258  static DWORD     sm_dwShellVersion;
259  HICON            m_hDynamicIcon; //Our cached copy of the last icon created with BitmapToIcon
260
261//Methods
262  static DWORD GetNOTIFYICONDATASizeForOS();
263
264  friend class CTrayIconHooker;
265};
266
267#endif //_NTRAY_H__
Note: See TracBrowser for help on using the repository browser.