123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- #include "stdafx.h"
- #include "TrayIcon.h"
- #include "resource.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- IMPLEMENT_DYNAMIC(CTrayIcon, CObject)
- CTrayIcon::CTrayIcon()
- {
- memset(&m_tnd, 0, sizeof(m_tnd));
- m_bEnabled = FALSE;
- m_bHidden = FALSE;
- }
- CTrayIcon::CTrayIcon(CWnd* pWnd, UINT uCallbackMessage, LPCTSTR szToolTip,
- HICON icon, UINT uID)
- {
- Create(pWnd, uCallbackMessage, szToolTip, icon, uID);
- m_bHidden = FALSE;
- }
- BOOL CTrayIcon::Create(CWnd* pWnd, UINT uCallbackMessage, LPCTSTR szToolTip,
- HICON icon, UINT uID)
- {
-
- VERIFY(m_bEnabled = ( GetVersion() & 0xff ) >= 4);
- if (!m_bEnabled) return FALSE;
-
- VERIFY(m_bEnabled = (pWnd && ::IsWindow(pWnd->GetSafeHwnd())));
- if (!m_bEnabled) return FALSE;
-
-
- ASSERT(uCallbackMessage >= WM_USER);
-
- ASSERT(_tcslen(szToolTip) <= 64);
-
- m_tnd.cbSize = sizeof(NOTIFYICONDATA);
- m_tnd.hWnd = pWnd->GetSafeHwnd();
- m_tnd.uID = uID;
- m_tnd.hIcon = icon;
- m_tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
- m_tnd.uCallbackMessage = uCallbackMessage;
- lstrcpy (m_tnd.szTip, szToolTip);
-
- (m_bEnabled = Shell_NotifyIcon(NIM_ADD, &m_tnd));
- return m_bEnabled;
- }
- CTrayIcon::~CTrayIcon()
- {
- RemoveIcon();
- }
- void CTrayIcon::MoveToRight()
- {
- HideIcon();
- ShowIcon();
- }
- void CTrayIcon::RemoveIcon()
- {
- if (!m_bEnabled) return;
- m_tnd.uFlags = 0;
- Shell_NotifyIcon(NIM_DELETE, &m_tnd);
- m_bEnabled = FALSE;
- }
- void CTrayIcon::HideIcon()
- {
- if (m_bEnabled && !m_bHidden) {
- m_tnd.uFlags = NIF_ICON;
- Shell_NotifyIcon (NIM_DELETE, &m_tnd);
- m_bHidden = TRUE;
- }
- }
- void CTrayIcon::ShowIcon()
- {
- if (m_bEnabled && m_bHidden) {
- m_tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
- Shell_NotifyIcon(NIM_ADD, &m_tnd);
- m_bHidden = FALSE;
- }
- }
- BOOL CTrayIcon::SetIcon(HICON hIcon)
- {
- if (!m_bEnabled) return FALSE;
- m_tnd.uFlags = NIF_ICON;
- m_tnd.hIcon = hIcon;
- return Shell_NotifyIcon(NIM_MODIFY, &m_tnd);
- }
- BOOL CTrayIcon::SetIcon(LPCTSTR lpszIconName)
- {
- HICON hIcon = AfxGetApp()->LoadIcon(lpszIconName);
- return SetIcon(hIcon);
- }
- BOOL CTrayIcon::SetIcon(UINT nIDResource)
- {
- HICON hIcon = AfxGetApp()->LoadIcon(nIDResource);
- return SetIcon(hIcon);
- }
- BOOL CTrayIcon::SetStandardIcon(LPCTSTR lpIconName)
- {
- HICON hIcon = LoadIcon(NULL, lpIconName);
- return SetIcon(hIcon);
- }
- BOOL CTrayIcon::SetStandardIcon(UINT nIDResource)
- {
- HICON hIcon = LoadIcon(NULL, MAKEINTRESOURCE(nIDResource));
- return SetIcon(hIcon);
- }
-
- HICON CTrayIcon::GetIcon() const
- {
- HICON hIcon = NULL;
- if (m_bEnabled)
- hIcon = m_tnd.hIcon;
- return hIcon;
- }
- BOOL CTrayIcon::SetTooltipText(LPCTSTR pszTip)
- {
- if (!m_bEnabled) return FALSE;
- m_tnd.uFlags = NIF_TIP;
- _tcscpy(m_tnd.szTip, pszTip);
- return Shell_NotifyIcon(NIM_MODIFY, &m_tnd);
- }
- BOOL CTrayIcon::SetTooltipText(UINT nID)
- {
- CString strText;
- VERIFY(strText.LoadString(nID));
- return SetTooltipText(strText);
- }
- CString CTrayIcon::GetTooltipText() const
- {
- CString strText;
- if (m_bEnabled)
- strText = m_tnd.szTip;
- return strText;
- }
- BOOL CTrayIcon::SetNotificationWnd(CWnd* pWnd)
- {
- if (!m_bEnabled) return FALSE;
-
- ASSERT(pWnd && ::IsWindow(pWnd->GetSafeHwnd()));
- m_tnd.hWnd = pWnd->GetSafeHwnd();
- m_tnd.uFlags = 0;
- return Shell_NotifyIcon(NIM_MODIFY, &m_tnd);
- }
- CWnd* CTrayIcon::GetNotificationWnd() const
- {
- return CWnd::FromHandle(m_tnd.hWnd);
- }
- LRESULT CTrayIcon::OnTrayNotification(UINT wParam, LONG lParam)
- {
-
- if (wParam != m_tnd.uID)
- return 0L;
- CMenu menu, *pSubMenu;
-
- if (LOWORD(lParam) == WM_RBUTTONUP)
- {
- if (!menu.LoadMenu(m_tnd.uID)) return 0;
- if (!(pSubMenu = menu.GetSubMenu(0))) return 0;
-
- ::SetMenuDefaultItem(pSubMenu->m_hMenu, 0, TRUE);
-
-
- CPoint pos;
- GetCursorPos(&pos);
- ::SetForegroundWindow(m_tnd.hWnd);
- ::TrackPopupMenu(pSubMenu->m_hMenu, 0, pos.x, pos.y, 0, m_tnd.hWnd, NULL);
-
- menu.DestroyMenu();
- }
- return 1;
- }
|