home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / mfc / URLCheck.ZIP / TrayIcon.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-08  |  6.6 KB  |  269 lines

  1. /////////////////////////////////////////////////////////////////////////////
  2. // TrayIcon.cpp : implementation file
  3. //
  4. // This is a conglomeration of ideas from the MSJ "Webster" application,
  5. // sniffing round the online docs, and from other implementations such
  6. // as PJ Naughter's "CTrayIconifyIcon" (http://indigo.ie/~pjn/ntray.html)
  7. // especially the "CTrayIcon::OnTrayNotification" member function.
  8. //
  9. // This class is a light wrapper around the windows system tray stuff. It
  10. // adds an icon to the system tray with the specified ToolTip text and 
  11. // callback notification value, which is sent back to the Parent window.
  12. //
  13. // The tray icon can be instantiated using either the constructor or by
  14. // declaring the object and creating (and displaying) it later on in the
  15. // program. eg.
  16. //
  17. //        CTrayIcon m_TrayIcon;    // Member variable of some class
  18. //        
  19. //        ... 
  20. //        // in some member function maybe...
  21. //        m_TrayIcon.Create(pParentWnd, WM_MY_NOTIFY, "Click here", 
  22. //                          hIcon, nTrayIconID);
  23. //
  24. // Clobbered together by Chris Maunder.
  25. // 
  26. //
  27. /////////////////////////////////////////////////////////////////////////////
  28.  
  29. #include "stdafx.h"
  30. #include "TrayIcon.h"
  31.  
  32. #ifdef _DEBUG
  33. #define new DEBUG_NEW
  34. #undef THIS_FILE
  35. static char THIS_FILE[] = __FILE__;
  36. #endif
  37.  
  38. IMPLEMENT_DYNAMIC(CTrayIcon, CObject)
  39.  
  40. /////////////////////////////////////////////////////////////////////////////
  41. // CTrayIcon construction/creation/destruction
  42.  
  43. CTrayIcon::CTrayIcon()
  44. {
  45.     memset(&m_tnd, 0, sizeof(m_tnd));
  46.     m_bEnabled = FALSE;
  47.     m_bHidden  = FALSE;
  48. }
  49.  
  50. CTrayIcon::CTrayIcon(CWnd* pWnd, UINT uCallbackMessage, LPCTSTR szToolTip, 
  51.                      HICON icon, UINT uID)
  52. {
  53.     Create(pWnd, uCallbackMessage, szToolTip, icon, uID);
  54.     m_bHidden = FALSE;
  55. }
  56.  
  57. BOOL CTrayIcon::Create(CWnd* pWnd, UINT uCallbackMessage, LPCTSTR szToolTip, 
  58.                        HICON icon, UINT uID)
  59. {
  60.     // this is only for Windows 95 (or higher)
  61.     VERIFY(m_bEnabled = ( GetVersion() & 0xff ) >= 4);
  62.     if (!m_bEnabled) return FALSE;
  63.  
  64.     //Make sure Notification window is valid
  65.     VERIFY(m_bEnabled = (pWnd && ::IsWindow(pWnd->GetSafeHwnd())));
  66.     if (!m_bEnabled) return FALSE;
  67.     
  68.     //Make sure we avoid conflict with other messages
  69.     ASSERT(uCallbackMessage >= WM_USER);
  70.  
  71.     //Tray only supports tooltip text up to 64 characters
  72.     ASSERT(_tcslen(szToolTip) <= 64);
  73.  
  74.     // load up the NOTIFYICONDATA structure
  75.     m_tnd.cbSize = sizeof(NOTIFYICONDATA);
  76.     m_tnd.hWnd     = pWnd->GetSafeHwnd();
  77.     m_tnd.uID     = uID;
  78.     m_tnd.hIcon  = icon;
  79.     m_tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
  80.     m_tnd.uCallbackMessage = uCallbackMessage;
  81.     strcpy (m_tnd.szTip, szToolTip);
  82.  
  83.     // Set the tray icon
  84.     VERIFY(m_bEnabled = Shell_NotifyIcon(NIM_ADD, &m_tnd));
  85.     return m_bEnabled;
  86. }
  87.  
  88. CTrayIcon::~CTrayIcon()
  89. {
  90.     RemoveIcon();
  91. }
  92.  
  93.  
  94. /////////////////////////////////////////////////////////////////////////////
  95. // CTrayIcon icon manipulation
  96.  
  97. void CTrayIcon::MoveToRight()
  98. {
  99.     HideIcon();
  100.     ShowIcon();
  101. }
  102.  
  103. void CTrayIcon::RemoveIcon()
  104. {
  105.     if (!m_bEnabled) return;
  106.  
  107.     m_tnd.uFlags = 0;
  108.     Shell_NotifyIcon(NIM_DELETE, &m_tnd);
  109.     m_bEnabled = FALSE;
  110. }
  111.  
  112. void CTrayIcon::HideIcon()
  113. {
  114.     if (m_bEnabled && !m_bHidden) {
  115.         m_tnd.uFlags = NIF_ICON;
  116.         Shell_NotifyIcon (NIM_DELETE, &m_tnd);
  117.         m_bHidden = TRUE;
  118.     }
  119. }
  120.  
  121. void CTrayIcon::ShowIcon()
  122. {
  123.     if (m_bEnabled && m_bHidden) {
  124.         m_tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
  125.         Shell_NotifyIcon(NIM_ADD, &m_tnd);
  126.         m_bHidden = FALSE;
  127.     }
  128. }
  129.  
  130. BOOL CTrayIcon::SetIcon(HICON hIcon)
  131. {
  132.     if (!m_bEnabled) return FALSE;
  133.  
  134.     m_tnd.uFlags = NIF_ICON;
  135.     m_tnd.hIcon = hIcon;
  136.  
  137.     return Shell_NotifyIcon(NIM_MODIFY, &m_tnd);
  138. }
  139.  
  140. BOOL CTrayIcon::SetIcon(LPCTSTR lpszIconName)
  141. {
  142.     HICON hIcon = AfxGetApp()->LoadIcon(lpszIconName);
  143.  
  144.     return SetIcon(hIcon);
  145. }
  146.  
  147. BOOL CTrayIcon::SetIcon(UINT nIDResource)
  148. {
  149.     HICON hIcon = AfxGetApp()->LoadIcon(nIDResource);
  150.  
  151.     return SetIcon(hIcon);
  152. }
  153.  
  154. BOOL CTrayIcon::SetStandardIcon(LPCTSTR lpIconName)
  155. {
  156.     HICON hIcon = LoadIcon(NULL, lpIconName);
  157.  
  158.     return SetIcon(hIcon);
  159. }
  160.  
  161. BOOL CTrayIcon::SetStandardIcon(UINT nIDResource)
  162. {
  163.     HICON hIcon = LoadIcon(NULL, MAKEINTRESOURCE(nIDResource));
  164.  
  165.     return SetIcon(hIcon);
  166. }
  167.  
  168. HICON CTrayIcon::GetIcon() const
  169. {
  170.     HICON hIcon = NULL;
  171.     if (m_bEnabled)
  172.         hIcon = m_tnd.hIcon;
  173.  
  174.     return hIcon;
  175. }
  176.  
  177. /////////////////////////////////////////////////////////////////////////////
  178. // CTrayIcon tooltip text manipulation
  179.  
  180. BOOL CTrayIcon::SetTooltipText(LPCTSTR pszTip)
  181. {
  182.     if (!m_bEnabled) return FALSE;
  183.  
  184.     m_tnd.uFlags = NIF_TIP;
  185.     _tcscpy(m_tnd.szTip, pszTip);
  186.  
  187.     return Shell_NotifyIcon(NIM_MODIFY, &m_tnd);
  188. }
  189.  
  190. BOOL CTrayIcon::SetTooltipText(UINT nID)
  191. {
  192.     CString strText;
  193.     VERIFY(strText.LoadString(nID));
  194.  
  195.     return SetTooltipText(strText);
  196. }
  197.  
  198. CString CTrayIcon::GetTooltipText() const
  199. {
  200.     CString strText;
  201.     if (m_bEnabled)
  202.         strText = m_tnd.szTip;
  203.  
  204.     return strText;
  205. }
  206.  
  207. /////////////////////////////////////////////////////////////////////////////
  208. // CTrayIcon notification window stuff
  209.  
  210. BOOL CTrayIcon::SetNotificationWnd(CWnd* pWnd)
  211. {
  212.     if (!m_bEnabled) return FALSE;
  213.  
  214.     //Make sure Notification window is valid
  215.     ASSERT(pWnd && ::IsWindow(pWnd->GetSafeHwnd()));
  216.  
  217.     m_tnd.hWnd = pWnd->GetSafeHwnd();
  218.     m_tnd.uFlags = 0;
  219.  
  220.     return Shell_NotifyIcon(NIM_MODIFY, &m_tnd);
  221. }
  222.  
  223. CWnd* CTrayIcon::GetNotificationWnd() const
  224. {
  225.     return CWnd::FromHandle(m_tnd.hWnd);
  226. }
  227.  
  228. /////////////////////////////////////////////////////////////////////////////
  229. // CTrayIcon implentation of OnTrayNotification
  230.  
  231. LRESULT CTrayIcon::OnTrayNotification(UINT wParam, LONG lParam) 
  232. {
  233.     //Return quickly if its not for this tray icon
  234.     if (wParam != m_tnd.uID)
  235.         return 0L;
  236.  
  237.     CMenu menu, *pSubMenu;
  238.  
  239.     // Clicking with right button brings up a context menu
  240.     if (LOWORD(lParam) == WM_RBUTTONUP)
  241.     {    
  242.         if (!menu.LoadMenu(m_tnd.uID)) return 0;
  243.         if (!(pSubMenu = menu.GetSubMenu(0))) return 0;
  244.  
  245.         // Make first menu item the default (bold font)
  246.         ::SetMenuDefaultItem(pSubMenu->m_hMenu, 0, TRUE);
  247.  
  248.         //Display and track the popup menu
  249.         CPoint pos;
  250.         GetCursorPos(&pos);
  251.         ::SetForegroundWindow(m_tnd.hWnd);  
  252.         ::TrackPopupMenu(pSubMenu->m_hMenu, 0, pos.x, pos.y, 0, m_tnd.hWnd, NULL);
  253.  
  254.         //pSubMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, pos.x, pos.y, this, NULL);
  255.         menu.DestroyMenu();
  256.     } 
  257.     else if (LOWORD(lParam) == WM_LBUTTONDBLCLK) 
  258.     {
  259.         if (!menu.LoadMenu(m_tnd.uID)) return 0;
  260.         if (!(pSubMenu = menu.GetSubMenu(0))) return 0;
  261.  
  262.         // double click received, the default action is to execute first menu item
  263.         ::SetForegroundWindow(m_tnd.hWnd);
  264.         ::SendMessage(m_tnd.hWnd, WM_COMMAND, pSubMenu->GetMenuItemID(0), 0);
  265.         menu.DestroyMenu();
  266.     }
  267.  
  268.     return 1;
  269. }