SystemTray.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /****************************************************************/
  2. /* */
  3. /* SYSTEMTRAY.CPP */
  4. /* */
  5. /* Implementation of the CSystemTray class */
  6. /* This class is a part of the Popup killer utility */
  7. /* */
  8. /* Programmed by LYFZ van der Meer */
  9. /* Copyright LYFZ Software Solutions 2002 */
  10. /* */
  11. /* Last updated: 20 februari 2002 */
  12. /* */
  13. /****************************************************************/
  14. #include "stdafx.h"
  15. #include "SystemTray.h"
  16. #ifdef _DEBUG
  17. #define new DEBUG_NEW
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21. IMPLEMENT_DYNAMIC(CSystemTray, CWnd)
  22. /********************************************************************/
  23. /* */
  24. /* Function name : CSystemTray::CSystemTray */
  25. /* Description : Constructor */
  26. /* */
  27. /********************************************************************/
  28. CSystemTray::CSystemTray()
  29. {
  30. ZeroMemory(&m_NotifyIconData, sizeof(m_NotifyIconData));
  31. }
  32. /********************************************************************/
  33. /* */
  34. /* Function name : CSystemTray::~CSystemTray */
  35. /* Description : Destructor */
  36. /* */
  37. /********************************************************************/
  38. CSystemTray::~CSystemTray()
  39. {
  40. // remove icon from system tray
  41. m_NotifyIconData.uFlags = 0;
  42. Shell_NotifyIcon(NIM_DELETE, &m_NotifyIconData);
  43. // destroy dummy window
  44. DestroyWindow();
  45. }
  46. BEGIN_MESSAGE_MAP(CSystemTray, CWnd)
  47. //{{AFX_MSG_MAP(CSystemTray)
  48. //}}AFX_MSG_MAP
  49. END_MESSAGE_MAP()
  50. /********************************************************************/
  51. /* */
  52. /* Function name : CSystemTray::Create */
  53. /* Description : Create systemtray icon */
  54. /* */
  55. /********************************************************************/
  56. BOOL CSystemTray::Create(UINT uCallbackMessage, LPCTSTR lpszToolTip, HICON hIcon, UINT uID)
  57. {
  58. // create dummy window
  59. if (!CWnd::CreateEx(0, AfxRegisterWndClass(0), "CSystemTray Dummy Window", WS_POPUP, 0,0,0,0, NULL, 0))
  60. return FALSE;
  61. // setup NOTIFYICONDATA
  62. m_NotifyIconData.cbSize = sizeof(NOTIFYICONDATA);
  63. m_NotifyIconData.hWnd = m_hWnd;
  64. m_NotifyIconData.uID = uID;
  65. m_NotifyIconData.hIcon = hIcon;
  66. m_NotifyIconData.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
  67. m_NotifyIconData.uCallbackMessage = uCallbackMessage;
  68. lstrcpy(m_NotifyIconData.szTip, lpszToolTip);
  69. // add tray icon
  70. return Shell_NotifyIcon(NIM_ADD, &m_NotifyIconData);
  71. }
  72. /********************************************************************/
  73. /* */
  74. /* Function name : CSystemTray::SetIcon */
  75. /* Description : Modify icon in the system tray */
  76. /* */
  77. /********************************************************************/
  78. BOOL CSystemTray::SetIcon(UINT nIDResource)
  79. {
  80. HICON hIcon = AfxGetApp()->LoadIcon(nIDResource);
  81. m_NotifyIconData.uFlags = NIF_ICON;
  82. m_NotifyIconData.hIcon = hIcon;
  83. return Shell_NotifyIcon(NIM_MODIFY, &m_NotifyIconData);
  84. }
  85. /********************************************************************/
  86. /* */
  87. /* Function name : CSystemTray::SetNotificationWnd */
  88. /* Description : Modify notification window */
  89. /* */
  90. /********************************************************************/
  91. BOOL CSystemTray::SetNotificationWnd(CWnd* pWnd)
  92. {
  93. m_NotifyIconData.hWnd = pWnd->GetSafeHwnd();
  94. m_NotifyIconData.uFlags = 0;
  95. return Shell_NotifyIcon(NIM_MODIFY, &m_NotifyIconData);
  96. }
  97. /********************************************************************/
  98. /* */
  99. /* Function name : CSystemTray::OnTrayNotification */
  100. /* Description : Handle notification messages */
  101. /* */
  102. /********************************************************************/
  103. LRESULT CSystemTray::OnTrayNotification(UINT wParam, LONG lParam)
  104. {
  105. // is it for us ?
  106. if (wParam != m_NotifyIconData.uID)
  107. return 0L;
  108. CWnd *pMainWnd = AfxGetMainWnd();
  109. switch(LOWORD(lParam))
  110. {
  111. case WM_RBUTTONUP:
  112. {
  113. // right button click brings up a context menu
  114. CMenu menu, *pSubMenu;
  115. if (!menu.LoadMenu(m_NotifyIconData.uID))
  116. return 0;
  117. pSubMenu = menu.GetSubMenu(0);
  118. if (pSubMenu == NULL)
  119. return 0;
  120. CPoint pos;
  121. GetCursorPos(&pos);
  122. // display popup menu
  123. pSubMenu->TrackPopupMenu(0, pos.x, pos.y, pMainWnd, NULL);
  124. menu.DestroyMenu();
  125. break;
  126. }
  127. case WM_LBUTTONDBLCLK:
  128. {
  129. // double click brings up the main dialog
  130. pMainWnd->ShowWindow(SW_SHOW);
  131. pMainWnd->SetForegroundWindow();
  132. break;
  133. }
  134. default:
  135. break;
  136. }
  137. return 1;
  138. }
  139. /********************************************************************/
  140. /* */
  141. /* Function name : CSystemTray::WindowProc */
  142. /* Description : Dispatch messages through the window’s message */
  143. /* map */
  144. /* */
  145. /********************************************************************/
  146. LRESULT CSystemTray::WindowProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
  147. {
  148. if (uMsg == m_NotifyIconData.uCallbackMessage)
  149. {
  150. return OnTrayNotification(wParam, lParam);
  151. }
  152. return CWnd::WindowProc(uMsg, wParam, lParam);
  153. }