SystemTray.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  1. /////////////////////////////////////////////////////////////////////////////
  2. // SystemTray.cpp : implementation file
  3. //
  4. // MFC VERSION
  5. //
  6. // This is a conglomeration of ideas from the MSJ "Webster" application,
  7. // sniffing round the online docs, and from other implementations such
  8. // as PJ Naughter's "CTrayNotifyIcon" (http://indigo.ie/~pjn/ntray.html)
  9. // especially the "CSystemTray::OnTrayNotification" member function.
  10. // Joerg Koenig suggested the icon animation stuff
  11. //
  12. // This class is a light wrapper around the windows system tray stuff. It
  13. // adds an icon to the system tray with the specified ToolTip text and
  14. // callback notification value, which is sent back to the Parent window.
  15. //
  16. // The tray icon can be instantiated using either the constructor or by
  17. // declaring the object and creating (and displaying) it later on in the
  18. // program. eg.
  19. //
  20. // CSystemTray m_SystemTray; // Member variable of some class
  21. //
  22. // ...
  23. // // in some member function maybe...
  24. // m_SystemTray.Create(pParentWnd, WM_MY_NOTIFY, "Click here",
  25. // hIcon, nSystemTrayID);
  26. //
  27. // Written by Chris Maunder ([email protected])
  28. // Copyright (c) 1998.
  29. //
  30. // Updated: 25 Jul 1998 - Added icon animation, and derived class
  31. // from CWnd in order to handle messages. (CJM)
  32. // (icon animation suggested by Joerg Koenig.
  33. // Added API to set default menu item. Code provided
  34. // by Enrico Lelina.
  35. //
  36. // Updated: 6 June 1999 - SetIcon can now load non-standard sized icons (Chip Calvert)
  37. // Added "bHidden" parameter when creating icon
  38. // (Thanks to Michael Gombar for these suggestions)
  39. // Restricted tooltip text to 64 characters.
  40. //
  41. // Updated: 9 Nov 1999 - Now works in WindowsCE.
  42. // Fix for use in NT services (Thomas Mooney, TeleProc, Inc)
  43. // Added W2K stuff by Michael Dunn
  44. //
  45. // Updated: 1 Jan 2000 - Added tray minimisation stuff.
  46. //
  47. // Updated: 21 Sep 2000 - Added GetDoWndAnimation - animation only occurs if the system
  48. // settings allow it (Matthew Ellis). Updated the GetTrayWndRect
  49. // function to include more fallback logic (Matthew Ellis)
  50. // NOTE: Signature of GetTrayWndRect has changed!
  51. //
  52. // Updated: 16 Jun 2002 - Fixed stupid errors so that it compiles clean on VC7
  53. //
  54. // This code may be used in compiled form in any way you desire. This
  55. // file may be redistributed unmodified by any means PROVIDING it is
  56. // not sold for profit without the authors written consent, and
  57. // providing that this notice and the authors name is included. If
  58. // the source code in this file is used in any commercial application
  59. // then acknowledgement must be made to the author of this file
  60. // (in whatever form you wish).
  61. //
  62. // This file is provided "as is" with no expressed or implied warranty.
  63. // The author accepts no liability for any damage caused through use.
  64. //
  65. // Expect bugs.
  66. //
  67. // Please use and enjoy. Please let me know of any bugs/mods/improvements
  68. // that you have found/implemented and I will fix/incorporate them into this
  69. // file.
  70. //
  71. /////////////////////////////////////////////////////////////////////////////
  72. #include "stdafx.h"
  73. #include "SystemTray.h"
  74. #ifdef _DEBUG
  75. #define new DEBUG_NEW
  76. #undef THIS_FILE
  77. static char THIS_FILE[] = __FILE__;
  78. #endif
  79. #ifndef _WIN32_WCE // Use C++ exception handling instead of structured.
  80. #undef TRY
  81. #undef CATCH
  82. #undef END_CATCH
  83. #define TRY try
  84. #define CATCH(ex_class, ex_object) catch(ex_class* ex_object)
  85. #define END_CATCH
  86. #endif // _WIN32_WCE
  87. #ifndef _countof
  88. #define _countof(x) ( sizeof(x) / sizeof(x[0]) )
  89. #endif
  90. IMPLEMENT_DYNAMIC(CSystemTray, CWnd)
  91. const UINT CSystemTray::m_nTimerID = 4567;
  92. UINT CSystemTray::m_nMaxTooltipLength = 64; // This may change...
  93. const UINT CSystemTray::m_nTaskbarCreatedMsg = ::RegisterWindowMessage(_T("TaskbarCreated"));
  94. CWnd CSystemTray::m_wndInvisible;
  95. BOOL CSystemTray::m_bShowWndAnimation;
  96. /////////////////////////////////////////////////////////////////////////////
  97. // CSystemTray construction/creation/destruction
  98. CSystemTray::CSystemTray()
  99. {
  100. Initialise();
  101. }
  102. CSystemTray::CSystemTray(CWnd* pParent, // The window that will recieve tray notifications
  103. UINT uCallbackMessage, // the callback message to send to parent
  104. LPCTSTR szToolTip, // tray icon tooltip
  105. HICON icon, // Handle to icon
  106. UINT uID, // Identifier of tray icon
  107. BOOL bHidden /*=FALSE*/, // Hidden on creation?
  108. LPCTSTR szBalloonTip /*=NULL*/, // Ballon tip (w2k only)
  109. LPCTSTR szBalloonTitle /*=NULL*/, // Balloon tip title (w2k)
  110. DWORD dwBalloonIcon /*=NIIF_NONE*/,// Ballon tip icon (w2k)
  111. UINT uBalloonTimeout /*=10*/) // Balloon timeout (w2k)
  112. {
  113. Initialise();
  114. Create(pParent, uCallbackMessage, szToolTip, icon, uID, bHidden,
  115. szBalloonTip, szBalloonTitle, dwBalloonIcon, uBalloonTimeout);
  116. }
  117. void CSystemTray::Initialise()
  118. {
  119. memset(&m_tnd, 0, sizeof(m_tnd));
  120. m_bEnabled = FALSE;
  121. m_bHidden = TRUE;
  122. m_bRemoved = TRUE;
  123. m_DefaultMenuItemID = 0;
  124. m_DefaultMenuItemByPos = TRUE;
  125. m_bShowIconPending = FALSE;
  126. m_uIDTimer = 0;
  127. m_hSavedIcon = NULL;
  128. m_pTargetWnd = NULL;
  129. m_uCreationFlags = 0;
  130. m_bShowWndAnimation = FALSE;
  131. m_bSingleClickSelect = FALSE;
  132. #ifdef SYSTEMTRAY_USEW2K
  133. OSVERSIONINFO os = { sizeof(os) };
  134. GetVersionEx(&os);
  135. m_bWin2K = ( VER_PLATFORM_WIN32_NT == os.dwPlatformId && os.dwMajorVersion >= 5 );
  136. #else
  137. m_bWin2K = FALSE;
  138. #endif
  139. }
  140. // update by Michael Dunn, November 1999
  141. //
  142. // New version of Create() that handles new features in Win 2K.
  143. //
  144. // Changes:
  145. // szTip: Same as old, but can be 128 characters instead of 64.
  146. // szBalloonTip: Text for a balloon tooltip that is shown when the icon
  147. // is first added to the tray. Pass "" if you don't want
  148. // a balloon.
  149. // szBalloonTitle: Title text for the balloon tooltip. This text is shown
  150. // in bold above the szBalloonTip text. Pass "" if you
  151. // don't want a title.
  152. // dwBalloonIcon: Specifies which icon will appear in the balloon. Legal
  153. // values are:
  154. // NIIF_NONE: No icon
  155. // NIIF_INFO: Information
  156. // NIIF_WARNING: Exclamation
  157. // NIIF_ERROR: Critical error (red circle with X)
  158. // uBalloonTimeout: Number of seconds for the balloon to remain visible.
  159. // Must be between 10 and 30 inclusive.
  160. BOOL CSystemTray::Create(CWnd* pParent, UINT uCallbackMessage, LPCTSTR szToolTip,
  161. HICON icon, UINT uID, BOOL bHidden /*=FALSE*/,
  162. LPCTSTR szBalloonTip /*=NULL*/,
  163. LPCTSTR szBalloonTitle /*=NULL*/,
  164. DWORD dwBalloonIcon /*=NIIF_NONE*/,
  165. UINT uBalloonTimeout /*=10*/)
  166. {
  167. #ifdef _WIN32_WCE
  168. m_bEnabled = TRUE;
  169. #else
  170. // this is only for Windows 95 (or higher)
  171. m_bEnabled = (GetVersion() & 0xff) >= 4;
  172. if (!m_bEnabled)
  173. {
  174. ASSERT(FALSE);
  175. return FALSE;
  176. }
  177. #endif
  178. m_nMaxTooltipLength = _countof(m_tnd.szTip);
  179. // Make sure we avoid conflict with other messages
  180. ASSERT(uCallbackMessage >= WM_APP);
  181. // Tray only supports tooltip text up to m_nMaxTooltipLength) characters
  182. ASSERT(AfxIsValidString(szToolTip));
  183. ASSERT(_tcslen(szToolTip) <= m_nMaxTooltipLength);
  184. // Create an invisible window
  185. CWnd::CreateEx(0, AfxRegisterWndClass(0), _T(""), WS_POPUP, 0,0,0,0, NULL, 0);
  186. // load up the NOTIFYICONDATA structure
  187. m_tnd.cbSize = sizeof(NOTIFYICONDATA);
  188. m_tnd.hWnd = pParent->GetSafeHwnd()? pParent->GetSafeHwnd() : m_hWnd;
  189. m_tnd.uID = uID;
  190. m_tnd.hIcon = icon;
  191. m_tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
  192. m_tnd.uCallbackMessage = uCallbackMessage;
  193. _tcsncpy(m_tnd.szTip, szToolTip, m_nMaxTooltipLength-1);
  194. #ifdef SYSTEMTRAY_USEW2K
  195. if (m_bWin2K && szBalloonTip)
  196. {
  197. // The balloon tooltip text can be up to 255 chars long.
  198. ASSERT(AfxIsValidString(szBalloonTip));
  199. ASSERT(lstrlen(szBalloonTip) < 256);
  200. // The balloon title text can be up to 63 chars long.
  201. if (szBalloonTitle)
  202. {
  203. ASSERT(AfxIsValidString(szBalloonTitle));
  204. ASSERT(lstrlen(szBalloonTitle) < 64);
  205. }
  206. // dwBalloonIcon must be valid.
  207. ASSERT(NIIF_NONE == dwBalloonIcon || NIIF_INFO == dwBalloonIcon ||
  208. NIIF_WARNING == dwBalloonIcon || NIIF_ERROR == dwBalloonIcon);
  209. // The timeout must be between 10 and 30 seconds.
  210. ASSERT(uBalloonTimeout >= 10 && uBalloonTimeout <= 30);
  211. m_tnd.uFlags |= NIF_INFO;
  212. _tcsncpy(m_tnd.szInfo, szBalloonTip, 255);
  213. if (szBalloonTitle)
  214. _tcsncpy(m_tnd.szInfoTitle, szBalloonTitle, 63);
  215. else
  216. m_tnd.szInfoTitle[0] = _T('\0');
  217. m_tnd.uTimeout = uBalloonTimeout * 1000; // convert time to ms
  218. m_tnd.dwInfoFlags = dwBalloonIcon;
  219. }
  220. #endif
  221. m_bHidden = bHidden;
  222. #ifdef SYSTEMTRAY_USEW2K
  223. if (m_bWin2K && m_bHidden)
  224. {
  225. m_tnd.uFlags = NIF_STATE;
  226. m_tnd.dwState = NIS_HIDDEN;
  227. m_tnd.dwStateMask = NIS_HIDDEN;
  228. }
  229. #endif
  230. m_uCreationFlags = m_tnd.uFlags; // Store in case we need to recreate in OnTaskBarCreate
  231. BOOL bResult = TRUE;
  232. if (!m_bHidden || m_bWin2K)
  233. {
  234. bResult = Shell_NotifyIcon(NIM_ADD, &m_tnd);
  235. m_bShowIconPending = m_bHidden = m_bRemoved = !bResult;
  236. }
  237. #ifdef SYSTEMTRAY_USEW2K
  238. if (m_bWin2K && szBalloonTip)
  239. {
  240. // Zero out the balloon text string so that later operations won't redisplay
  241. // the balloon.
  242. m_tnd.szInfo[0] = _T('\0');
  243. }
  244. #endif
  245. return bResult;
  246. }
  247. CSystemTray::~CSystemTray()
  248. {
  249. RemoveIcon();
  250. m_IconList.RemoveAll();
  251. DestroyWindow();
  252. }
  253. /////////////////////////////////////////////////////////////////////////////
  254. // CSystemTray icon manipulation
  255. //////////////////////////////////////////////////////////////////////////
  256. //
  257. // Function: SetFocus()
  258. //
  259. // Description:
  260. // Sets the focus to the tray icon. Microsoft's Win 2K UI guidelines
  261. // say you should do this after the user dismisses the icon's context
  262. // menu.
  263. //
  264. // Input:
  265. // Nothing.
  266. //
  267. // Returns:
  268. // Nothing.
  269. //
  270. //////////////////////////////////////////////////////////////////////////
  271. // Added by Michael Dunn, November, 1999
  272. //////////////////////////////////////////////////////////////////////////
  273. void CSystemTray::SetFocus()
  274. {
  275. #ifdef SYSTEMTRAY_USEW2K
  276. Shell_NotifyIcon ( NIM_SETFOCUS, &m_tnd );
  277. #endif
  278. }
  279. BOOL CSystemTray::MoveToRight()
  280. {
  281. RemoveIcon();
  282. return AddIcon();
  283. }
  284. BOOL CSystemTray::AddIcon()
  285. {
  286. if (!m_bRemoved)
  287. RemoveIcon();
  288. if (m_bEnabled)
  289. {
  290. m_tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
  291. if (!Shell_NotifyIcon(NIM_ADD, &m_tnd))
  292. m_bShowIconPending = TRUE;
  293. else
  294. m_bRemoved = m_bHidden = FALSE;
  295. }
  296. return (m_bRemoved == FALSE);
  297. }
  298. BOOL CSystemTray::RemoveIcon()
  299. {
  300. m_bShowIconPending = FALSE;
  301. if (!m_bEnabled || m_bRemoved)
  302. return TRUE;
  303. m_tnd.uFlags = 0;
  304. if (Shell_NotifyIcon(NIM_DELETE, &m_tnd))
  305. m_bRemoved = m_bHidden = TRUE;
  306. return (m_bRemoved == TRUE);
  307. }
  308. BOOL CSystemTray::HideIcon()
  309. {
  310. if (!m_bEnabled || m_bRemoved || m_bHidden)
  311. return TRUE;
  312. #ifdef SYSTEMTRAY_USEW2K
  313. if (m_bWin2K)
  314. {
  315. m_tnd.uFlags = NIF_STATE;
  316. m_tnd.dwState = NIS_HIDDEN;
  317. m_tnd.dwStateMask = NIS_HIDDEN;
  318. m_bHidden = Shell_NotifyIcon( NIM_MODIFY, &m_tnd);
  319. }
  320. else
  321. #endif
  322. RemoveIcon();
  323. return (m_bHidden == TRUE);
  324. }
  325. BOOL CSystemTray::ShowIcon()
  326. {
  327. if (m_bRemoved)
  328. return AddIcon();
  329. if (!m_bHidden)
  330. return TRUE;
  331. #ifdef SYSTEMTRAY_USEW2K
  332. if (m_bWin2K)
  333. {
  334. m_tnd.uFlags = NIF_STATE;
  335. m_tnd.dwState = 0;
  336. m_tnd.dwStateMask = NIS_HIDDEN;
  337. m_bHidden = !Shell_NotifyIcon ( NIM_MODIFY, &m_tnd );
  338. }
  339. else
  340. #endif
  341. AddIcon();
  342. return (m_bHidden == FALSE);
  343. }
  344. BOOL CSystemTray::SetIcon(HICON hIcon)
  345. {
  346. if (!m_bEnabled)
  347. return FALSE;
  348. m_tnd.uFlags = NIF_ICON;
  349. m_tnd.hIcon = hIcon;
  350. if (m_bHidden)
  351. return TRUE;
  352. else
  353. return Shell_NotifyIcon(NIM_MODIFY, &m_tnd);
  354. }
  355. BOOL CSystemTray::SetIcon(LPCTSTR lpszIconName)
  356. {
  357. HICON hIcon = (HICON) ::LoadImage(AfxGetResourceHandle(),
  358. lpszIconName,
  359. IMAGE_ICON,
  360. 0, 0,
  361. LR_DEFAULTCOLOR | LR_SHARED);
  362. return SetIcon(hIcon);
  363. }
  364. BOOL CSystemTray::SetIcon(UINT nIDResource)
  365. {
  366. return SetIcon(MAKEINTRESOURCE(nIDResource));
  367. }
  368. BOOL CSystemTray::SetStandardIcon(LPCTSTR lpIconName)
  369. {
  370. HICON hIcon = LoadIcon(NULL, lpIconName);
  371. return SetIcon(hIcon);
  372. }
  373. BOOL CSystemTray::SetStandardIcon(UINT nIDResource)
  374. {
  375. return SetStandardIcon(MAKEINTRESOURCE(nIDResource));
  376. }
  377. HICON CSystemTray::GetIcon() const
  378. {
  379. return (m_bEnabled)? m_tnd.hIcon : NULL;
  380. }
  381. BOOL CSystemTray::SetIconList(UINT uFirstIconID, UINT uLastIconID)
  382. {
  383. if (uFirstIconID > uLastIconID)
  384. return FALSE;
  385. const CWinApp* pApp = AfxGetApp();
  386. if (!pApp)
  387. {
  388. ASSERT(FALSE);
  389. return FALSE;
  390. }
  391. m_IconList.RemoveAll();
  392. TRY {
  393. for (UINT i = uFirstIconID; i <= uLastIconID; i++)
  394. m_IconList.Add(pApp->LoadIcon(i));
  395. }
  396. CATCH(CMemoryException, e)
  397. {
  398. e->ReportError();
  399. e->Delete();
  400. m_IconList.RemoveAll();
  401. return FALSE;
  402. }
  403. END_CATCH
  404. return TRUE;
  405. }
  406. BOOL CSystemTray::SetIconList(HICON* pHIconList, UINT nNumIcons)
  407. {
  408. m_IconList.RemoveAll();
  409. TRY {
  410. for (UINT i = 0; i <= nNumIcons; i++)
  411. m_IconList.Add(pHIconList[i]);
  412. }
  413. CATCH (CMemoryException, e)
  414. {
  415. e->ReportError();
  416. e->Delete();
  417. m_IconList.RemoveAll();
  418. return FALSE;
  419. }
  420. END_CATCH
  421. return TRUE;
  422. }
  423. BOOL CSystemTray::Animate(UINT nDelayMilliSeconds, int nNumSeconds /*=-1*/)
  424. {
  425. StopAnimation();
  426. m_nCurrentIcon = 0;
  427. m_StartTime = COleDateTime::GetCurrentTime();
  428. m_nAnimationPeriod = nNumSeconds;
  429. m_hSavedIcon = GetIcon();
  430. // Setup a timer for the animation
  431. m_uIDTimer = SetTimer(m_nTimerID, nDelayMilliSeconds, NULL);
  432. return (m_uIDTimer != 0);
  433. }
  434. BOOL CSystemTray::StepAnimation()
  435. {
  436. if (!m_IconList.GetSize())
  437. return FALSE;
  438. m_nCurrentIcon++;
  439. if (m_nCurrentIcon >= m_IconList.GetSize())
  440. m_nCurrentIcon = 0;
  441. return SetIcon(m_IconList[m_nCurrentIcon]);
  442. }
  443. BOOL CSystemTray::StopAnimation()
  444. {
  445. BOOL bResult = FALSE;
  446. if (m_uIDTimer)
  447. bResult = KillTimer(m_uIDTimer);
  448. m_uIDTimer = 0;
  449. if (m_hSavedIcon)
  450. SetIcon(m_hSavedIcon);
  451. m_hSavedIcon = NULL;
  452. return bResult;
  453. }
  454. /////////////////////////////////////////////////////////////////////////////
  455. // CSystemTray tooltip text manipulation
  456. BOOL CSystemTray::SetTooltipText(LPCTSTR pszTip)
  457. {
  458. ASSERT(AfxIsValidString(pszTip)); // (md)
  459. ASSERT(_tcslen(pszTip) < m_nMaxTooltipLength);
  460. if (!m_bEnabled)
  461. return FALSE;
  462. m_tnd.uFlags = NIF_TIP;
  463. _tcsncpy(m_tnd.szTip, pszTip, m_nMaxTooltipLength-1);
  464. if (m_bHidden)
  465. return TRUE;
  466. else
  467. return Shell_NotifyIcon(NIM_MODIFY, &m_tnd);
  468. }
  469. BOOL CSystemTray::SetTooltipText(UINT nID)
  470. {
  471. CString strText;
  472. VERIFY(strText.LoadString(nID));
  473. return SetTooltipText(strText);
  474. }
  475. CString CSystemTray::GetTooltipText() const
  476. {
  477. CString strText;
  478. if (m_bEnabled)
  479. strText = m_tnd.szTip;
  480. return strText;
  481. }
  482. /////////////////////////////////////////////////////////////////////////////
  483. // CSystemTray support for Win 2K features.
  484. //////////////////////////////////////////////////////////////////////////
  485. //
  486. // Function: ShowBalloon
  487. //
  488. // Description:
  489. // Shows a balloon tooltip over the tray icon.
  490. //
  491. // Input:
  492. // szText: [in] Text for the balloon tooltip.
  493. // szTitle: [in] Title for the balloon. This text is shown in bold above
  494. // the tooltip text (szText). Pass "" if you don't want a title.
  495. // dwIcon: [in] Specifies an icon to appear in the balloon. Legal values are:
  496. // NIIF_NONE: No icon
  497. // NIIF_INFO: Information
  498. // NIIF_WARNING: Exclamation
  499. // NIIF_ERROR: Critical error (red circle with X)
  500. // uTimeout: [in] Number of seconds for the balloon to remain visible. Can
  501. // be between 10 and 30 inclusive.
  502. //
  503. // Returns:
  504. // TRUE if successful, FALSE if not.
  505. //
  506. //////////////////////////////////////////////////////////////////////////
  507. // Added by Michael Dunn, November 1999
  508. //////////////////////////////////////////////////////////////////////////
  509. BOOL CSystemTray::ShowBalloon(LPCTSTR szText,
  510. LPCTSTR szTitle /*=NULL*/,
  511. DWORD dwIcon /*=NIIF_NONE*/,
  512. UINT uTimeout /*=10*/ )
  513. {
  514. #ifndef SYSTEMTRAY_USEW2K
  515. return FALSE;
  516. #else
  517. // Bail out if we're not on Win 2K.
  518. if (!m_bWin2K)
  519. return FALSE;
  520. // Verify input parameters.
  521. // The balloon tooltip text can be up to 255 chars long.
  522. ASSERT(AfxIsValidString(szText));
  523. ASSERT(lstrlen(szText) < 256);
  524. // The balloon title text can be up to 63 chars long.
  525. if (szTitle)
  526. {
  527. ASSERT(AfxIsValidString( szTitle));
  528. ASSERT(lstrlen(szTitle) < 64);
  529. }
  530. // dwBalloonIcon must be valid.
  531. ASSERT(NIIF_NONE == dwIcon || NIIF_INFO == dwIcon ||
  532. NIIF_WARNING == dwIcon || NIIF_ERROR == dwIcon);
  533. // The timeout must be between 10 and 30 seconds.
  534. ASSERT(uTimeout >= 10 && uTimeout <= 30);
  535. m_tnd.uFlags = NIF_INFO;
  536. _tcsncpy(m_tnd.szInfo, szText, 256);
  537. if (szTitle)
  538. _tcsncpy(m_tnd.szInfoTitle, szTitle, 64);
  539. else
  540. m_tnd.szInfoTitle[0] = _T('\0');
  541. m_tnd.dwInfoFlags = dwIcon;
  542. m_tnd.uTimeout = uTimeout * 1000; // convert time to ms
  543. BOOL bSuccess = Shell_NotifyIcon (NIM_MODIFY, &m_tnd);
  544. // Zero out the balloon text string so that later operations won't redisplay
  545. // the balloon.
  546. m_tnd.szInfo[0] = _T('\0');
  547. return bSuccess;
  548. #endif
  549. }
  550. /////////////////////////////////////////////////////////////////////////////
  551. // CSystemTray notification window stuff
  552. BOOL CSystemTray::SetNotificationWnd(CWnd* pWnd)
  553. {
  554. if (!m_bEnabled)
  555. return FALSE;
  556. // Make sure Notification window is valid
  557. if (!pWnd || !::IsWindow(pWnd->GetSafeHwnd()))
  558. {
  559. ASSERT(FALSE);
  560. return FALSE;
  561. }
  562. m_tnd.hWnd = pWnd->GetSafeHwnd();
  563. m_tnd.uFlags = 0;
  564. if (m_bHidden)
  565. return TRUE;
  566. else
  567. return Shell_NotifyIcon(NIM_MODIFY, &m_tnd);
  568. }
  569. CWnd* CSystemTray::GetNotificationWnd() const
  570. {
  571. return CWnd::FromHandle(m_tnd.hWnd);
  572. }
  573. // Hatr added
  574. // Hatr added
  575. // Change or retrive the window to send menu commands to
  576. BOOL CSystemTray::SetTargetWnd(CWnd* pTargetWnd)
  577. {
  578. m_pTargetWnd = pTargetWnd;
  579. return TRUE;
  580. } // CSystemTray::SetTargetWnd()
  581. CWnd* CSystemTray::GetTargetWnd() const
  582. {
  583. if (m_pTargetWnd)
  584. return m_pTargetWnd;
  585. else
  586. return AfxGetMainWnd();
  587. } // CSystemTray::GetTargetWnd()
  588. /////////////////////////////////////////////////////////////////////////////
  589. // CSystemTray notification message stuff
  590. BOOL CSystemTray::SetCallbackMessage(UINT uCallbackMessage)
  591. {
  592. if (!m_bEnabled)
  593. return FALSE;
  594. // Make sure we avoid conflict with other messages
  595. ASSERT(uCallbackMessage >= WM_APP);
  596. m_tnd.uCallbackMessage = uCallbackMessage;
  597. m_tnd.uFlags = NIF_MESSAGE;
  598. if (m_bHidden)
  599. return TRUE;
  600. else
  601. return Shell_NotifyIcon(NIM_MODIFY, &m_tnd);
  602. }
  603. UINT CSystemTray::GetCallbackMessage() const
  604. {
  605. return m_tnd.uCallbackMessage;
  606. }
  607. /////////////////////////////////////////////////////////////////////////////
  608. // CSystemTray menu manipulation
  609. BOOL CSystemTray::SetMenuDefaultItem(UINT uItem, BOOL bByPos)
  610. {
  611. #ifdef _WIN32_WCE
  612. return FALSE;
  613. #else
  614. if ((m_DefaultMenuItemID == uItem) && (m_DefaultMenuItemByPos == bByPos))
  615. return TRUE;
  616. m_DefaultMenuItemID = uItem;
  617. m_DefaultMenuItemByPos = bByPos;
  618. CMenu menu, *pSubMenu;
  619. if (!menu.LoadMenu(m_tnd.uID))
  620. return FALSE;
  621. pSubMenu = menu.GetSubMenu(0);
  622. if (!pSubMenu)
  623. return FALSE;
  624. ::SetMenuDefaultItem(pSubMenu->m_hMenu, m_DefaultMenuItemID, m_DefaultMenuItemByPos);
  625. return TRUE;
  626. #endif
  627. }
  628. void CSystemTray::GetMenuDefaultItem(UINT& uItem, BOOL& bByPos)
  629. {
  630. uItem = m_DefaultMenuItemID;
  631. bByPos = m_DefaultMenuItemByPos;
  632. }
  633. /////////////////////////////////////////////////////////////////////////////
  634. // CSystemTray message handlers
  635. BEGIN_MESSAGE_MAP(CSystemTray, CWnd)
  636. //{{AFX_MSG_MAP(CSystemTray)
  637. ON_WM_TIMER()
  638. //}}AFX_MSG_MAP
  639. #ifndef _WIN32_WCE
  640. ON_WM_SETTINGCHANGE()
  641. #endif
  642. ON_REGISTERED_MESSAGE(CSystemTray::m_nTaskbarCreatedMsg, OnTaskbarCreated)
  643. END_MESSAGE_MAP()
  644. void CSystemTray::OnTimer(UINT nIDEvent)
  645. {
  646. if (nIDEvent != m_uIDTimer)
  647. {
  648. ASSERT(FALSE);
  649. return;
  650. }
  651. COleDateTime CurrentTime = COleDateTime::GetCurrentTime();
  652. COleDateTimeSpan period = CurrentTime - m_StartTime;
  653. if (m_nAnimationPeriod > 0 && m_nAnimationPeriod < period.GetTotalSeconds())
  654. {
  655. StopAnimation();
  656. return;
  657. }
  658. StepAnimation();
  659. }
  660. // This is called whenever the taskbar is created (eg after explorer crashes
  661. // and restarts. Please note that the WM_TASKBARCREATED message is only passed
  662. // to TOP LEVEL windows (like WM_QUERYNEWPALETTE)
  663. LRESULT CSystemTray::OnTaskbarCreated(WPARAM /*wParam*/, LPARAM /*lParam*/)
  664. {
  665. InstallIconPending();
  666. return 0L;
  667. }
  668. #ifndef _WIN32_WCE
  669. void CSystemTray::OnSettingChange(UINT uFlags, LPCTSTR lpszSection)
  670. {
  671. CWnd::OnSettingChange(uFlags, lpszSection);
  672. if (uFlags == SPI_SETWORKAREA)
  673. InstallIconPending();
  674. }
  675. #endif
  676. LRESULT CSystemTray::OnTrayNotification(UINT wParam, LONG lParam)
  677. {
  678. //Return quickly if its not for this tray icon
  679. if (wParam != m_tnd.uID)
  680. return 0L;
  681. CMenu menu, *pSubMenu;
  682. CWnd *pTargetWnd = GetTargetWnd();
  683. if (!pTargetWnd)
  684. return 0L;
  685. // Clicking with right button brings up a context menu
  686. #if defined(_WIN32_WCE) //&& _WIN32_WCE < 211
  687. BOOL bAltPressed = ((GetKeyState(VK_MENU) & (1 << (sizeof(SHORT)*8-1))) != 0);
  688. if (LOWORD(lParam) == WM_LBUTTONUP && bAltPressed)
  689. #else
  690. if (LOWORD(lParam) == WM_RBUTTONUP)
  691. #endif
  692. {
  693. if (!menu.LoadMenu(m_tnd.uID))
  694. return 0;
  695. pSubMenu = menu.GetSubMenu(0);
  696. if (!pSubMenu)
  697. return 0;
  698. #ifndef _WIN32_WCE
  699. // Make chosen menu item the default (bold font)
  700. ::SetMenuDefaultItem(pSubMenu->m_hMenu, m_DefaultMenuItemID, m_DefaultMenuItemByPos);
  701. #endif
  702. // Display and track the popup menu
  703. CPoint pos;
  704. #ifdef _WIN32_WCE
  705. pos = CPoint(GetMessagePos());
  706. #else
  707. GetCursorPos(&pos);
  708. #endif
  709. pTargetWnd->SetForegroundWindow();
  710. #ifndef _WIN32_WCE
  711. pTargetWnd->SendMessage(WM_CUSTOMIZE_TRAY_MENU, (WPARAM)pSubMenu, 0);
  712. ::TrackPopupMenu(pSubMenu->m_hMenu, 0, pos.x, pos.y, 0,
  713. pTargetWnd->GetSafeHwnd(), NULL);
  714. #else
  715. pSubMenu->TrackPopupMenu(TPM_LEFTALIGN, pos.x, pos.y, pTargetWnd, NULL);
  716. #endif
  717. // BUGFIX: See "PRB: Menus for Notification Icons Don't Work Correctly"
  718. pTargetWnd->PostMessage(WM_NULL, 0, 0);
  719. menu.DestroyMenu();
  720. }
  721. #if defined(_WIN32_WCE) //&& _WIN32_WCE < 211
  722. if (LOWORD(lParam) == WM_LBUTTONDBLCLK && bAltPressed)
  723. #else
  724. else if((LOWORD(lParam) == WM_LBUTTONDBLCLK) || (LOWORD(lParam) == WM_LBUTTONUP && m_bSingleClickSelect))
  725. #endif
  726. {
  727. // double click received, the default action is to execute default menu item
  728. pTargetWnd->SetForegroundWindow();
  729. UINT uItem;
  730. if (m_DefaultMenuItemByPos)
  731. {
  732. if (!menu.LoadMenu(m_tnd.uID))
  733. return 0;
  734. pSubMenu = menu.GetSubMenu(0);
  735. if (!pSubMenu)
  736. return 0;
  737. uItem = pSubMenu->GetMenuItemID(m_DefaultMenuItemID);
  738. menu.DestroyMenu();
  739. }
  740. else
  741. uItem = m_DefaultMenuItemID;
  742. pTargetWnd->SendMessage(WM_COMMAND, uItem, 0);
  743. }
  744. return 1;
  745. }
  746. LRESULT CSystemTray::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
  747. {
  748. if (message == m_tnd.uCallbackMessage)
  749. return OnTrayNotification(wParam, lParam);
  750. return CWnd::WindowProc(message, wParam, lParam);
  751. }
  752. void CSystemTray::InstallIconPending()
  753. {
  754. // Is the icon display pending, and it's not been set as "hidden"?
  755. if (!m_bShowIconPending || m_bHidden)
  756. return;
  757. // Reset the flags to what was used at creation
  758. m_tnd.uFlags = m_uCreationFlags;
  759. // Try and recreate the icon
  760. m_bHidden = !Shell_NotifyIcon(NIM_ADD, &m_tnd);
  761. // If it's STILL hidden, then have another go next time...
  762. m_bShowIconPending = !m_bHidden;
  763. ASSERT(m_bHidden == FALSE);
  764. }
  765. /////////////////////////////////////////////////////////////////////////////
  766. // For minimising/maximising from system tray
  767. BOOL CALLBACK FindTrayWnd(HWND hwnd, LPARAM lParam)
  768. {
  769. TCHAR szClassName[256];
  770. GetClassName(hwnd, szClassName, 255);
  771. // Did we find the Main System Tray? If so, then get its size and keep going
  772. if (_tcscmp(szClassName, _T("TrayNotifyWnd")) == 0)
  773. {
  774. CRect *pRect = (CRect*) lParam;
  775. ::GetWindowRect(hwnd, pRect);
  776. return TRUE;
  777. }
  778. // Did we find the System Clock? If so, then adjust the size of the rectangle
  779. // we have and quit (clock will be found after the system tray)
  780. if (_tcscmp(szClassName, _T("TrayClockWClass")) == 0)
  781. {
  782. CRect *pRect = (CRect*) lParam;
  783. CRect rectClock;
  784. ::GetWindowRect(hwnd, rectClock);
  785. // if clock is above system tray adjust accordingly
  786. if (rectClock.bottom < pRect->bottom-5) // 10 = random fudge factor.
  787. pRect->top = rectClock.bottom;
  788. else
  789. pRect->right = rectClock.left;
  790. return FALSE;
  791. }
  792. return TRUE;
  793. }
  794. #ifndef _WIN32_WCE
  795. // enhanced version by Matthew Ellis <[email protected]>
  796. void CSystemTray::GetTrayWndRect(LPRECT lprect)
  797. {
  798. #define DEFAULT_RECT_WIDTH 150
  799. #define DEFAULT_RECT_HEIGHT 30
  800. HWND hShellTrayWnd = ::FindWindow(_T("Shell_TrayWnd"), NULL);
  801. if (hShellTrayWnd)
  802. {
  803. ::GetWindowRect(hShellTrayWnd, lprect);
  804. EnumChildWindows(hShellTrayWnd, FindTrayWnd, (LPARAM)lprect);
  805. return;
  806. }
  807. // OK, we failed to get the rect from the quick hack. Either explorer isn't
  808. // running or it's a new version of the shell with the window class names
  809. // changed (how dare Microsoft change these undocumented class names!) So, we
  810. // try to find out what side of the screen the taskbar is connected to. We
  811. // know that the system tray is either on the right or the bottom of the
  812. // taskbar, so we can make a good guess at where to minimize to
  813. APPBARDATA appBarData;
  814. appBarData.cbSize=sizeof(appBarData);
  815. if (SHAppBarMessage(ABM_GETTASKBARPOS,&appBarData))
  816. {
  817. // We know the edge the taskbar is connected to, so guess the rect of the
  818. // system tray. Use various fudge factor to make it look good
  819. switch(appBarData.uEdge)
  820. {
  821. case ABE_LEFT:
  822. case ABE_RIGHT:
  823. // We want to minimize to the bottom of the taskbar
  824. lprect->top = appBarData.rc.bottom-100;
  825. lprect->bottom = appBarData.rc.bottom-16;
  826. lprect->left = appBarData.rc.left;
  827. lprect->right = appBarData.rc.right;
  828. break;
  829. case ABE_TOP:
  830. case ABE_BOTTOM:
  831. // We want to minimize to the right of the taskbar
  832. lprect->top = appBarData.rc.top;
  833. lprect->bottom = appBarData.rc.bottom;
  834. lprect->left = appBarData.rc.right-100;
  835. lprect->right = appBarData.rc.right-16;
  836. break;
  837. }
  838. return;
  839. }
  840. // Blimey, we really aren't in luck. It's possible that a third party shell
  841. // is running instead of explorer. This shell might provide support for the
  842. // system tray, by providing a Shell_TrayWnd window (which receives the
  843. // messages for the icons) So, look for a Shell_TrayWnd window and work out
  844. // the rect from that. Remember that explorer's taskbar is the Shell_TrayWnd,
  845. // and stretches either the width or the height of the screen. We can't rely
  846. // on the 3rd party shell's Shell_TrayWnd doing the same, in fact, we can't
  847. // rely on it being any size. The best we can do is just blindly use the
  848. // window rect, perhaps limiting the width and height to, say 150 square.
  849. // Note that if the 3rd party shell supports the same configuraion as
  850. // explorer (the icons hosted in NotifyTrayWnd, which is a child window of
  851. // Shell_TrayWnd), we would already have caught it above
  852. if (hShellTrayWnd)
  853. {
  854. ::GetWindowRect(hShellTrayWnd, lprect);
  855. if (lprect->right - lprect->left > DEFAULT_RECT_WIDTH)
  856. lprect->left = lprect->right - DEFAULT_RECT_WIDTH;
  857. if (lprect->bottom - lprect->top > DEFAULT_RECT_HEIGHT)
  858. lprect->top = lprect->bottom - DEFAULT_RECT_HEIGHT;
  859. return;
  860. }
  861. // OK. Haven't found a thing. Provide a default rect based on the current work
  862. // area
  863. SystemParametersInfo(SPI_GETWORKAREA,0, lprect, 0);
  864. lprect->left = lprect->right - DEFAULT_RECT_WIDTH;
  865. lprect->top = lprect->bottom - DEFAULT_RECT_HEIGHT;
  866. }
  867. // Check to see if the animation has been disabled (Matthew Ellis <[email protected]>)
  868. BOOL CSystemTray::GetDoWndAnimation()
  869. {
  870. if(!m_bShowWndAnimation)
  871. return FALSE;
  872. ANIMATIONINFO ai;
  873. ai.cbSize=sizeof(ai);
  874. SystemParametersInfo(SPI_GETANIMATION,sizeof(ai),&ai,0);
  875. return ai.iMinAnimate?TRUE:FALSE;
  876. }
  877. #endif
  878. BOOL CSystemTray::RemoveTaskbarIcon(CWnd* pWnd)
  879. {
  880. LPCTSTR pstrOwnerClass = AfxRegisterWndClass(0);
  881. // Create static invisible window
  882. if (!::IsWindow(m_wndInvisible.m_hWnd))
  883. {
  884. if (!m_wndInvisible.CreateEx(0, pstrOwnerClass, _T(""), WS_POPUP,
  885. CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  886. NULL, 0))
  887. return FALSE;
  888. }
  889. pWnd->SetParent(&m_wndInvisible);
  890. return TRUE;
  891. }
  892. void CSystemTray::MinimiseToTray(CWnd* pWnd)
  893. {
  894. #ifndef _WIN32_WCE
  895. if (GetDoWndAnimation())
  896. {
  897. CRect rectFrom, rectTo;
  898. pWnd->GetWindowRect(rectFrom);
  899. GetTrayWndRect(rectTo);
  900. ::DrawAnimatedRects(pWnd->m_hWnd, IDANI_CAPTION, rectFrom, rectTo);
  901. }
  902. RemoveTaskbarIcon(pWnd);
  903. pWnd->ModifyStyle(WS_VISIBLE, 0);
  904. #endif
  905. }
  906. void CSystemTray::MaximiseFromTray(CWnd* pWnd)
  907. {
  908. #ifndef _WIN32_WCE
  909. if (GetDoWndAnimation())
  910. {
  911. CRect rectTo;
  912. pWnd->GetWindowRect(rectTo);
  913. CRect rectFrom;
  914. GetTrayWndRect(rectFrom);
  915. pWnd->SetParent(NULL);
  916. ::DrawAnimatedRects(pWnd->m_hWnd, IDANI_CAPTION, rectFrom, rectTo);
  917. }
  918. else
  919. pWnd->SetParent(NULL);
  920. pWnd->ModifyStyle(0, WS_VISIBLE);
  921. pWnd->RedrawWindow(NULL, NULL, RDW_UPDATENOW | RDW_ALLCHILDREN | RDW_FRAME |
  922. RDW_INVALIDATE | RDW_ERASE);
  923. // Move focus away and back again to ensure taskbar icon is recreated
  924. if (::IsWindow(m_wndInvisible.m_hWnd))
  925. m_wndInvisible.SetActiveWindow();
  926. pWnd->SetActiveWindow();
  927. pWnd->SetForegroundWindow();
  928. #endif
  929. }