SystemTray.cpp
资源名称:GGBT.rar [点击查看]
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:4k
源码类别:
P2P编程
开发平台:
Visual C++
- // SystemTray.cpp: implementation of the CSystemTray class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "testbt.h"
- #include "SystemTray.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CSystemTray::CSystemTray()
- {
- m_bEnabled = FALSE;
- m_bHidden = FALSE;
- memset(&m_tnd, 0, sizeof(m_tnd));
- m_DefaultMenuItemID = 0;
- m_DefaultMenuItemByPos = TRUE;
- }
- CSystemTray::~CSystemTray()
- {
- RemoveIcon();
- }
- BOOL CSystemTray::Create(CWnd* pParent, UINT uCallbackMessage, LPCTSTR szToolTip,
- HICON icon, UINT uID)
- {
- // this is only for Windows 95 (or higher)
- VERIFY(m_bEnabled = ( GetVersion() & 0xff ) >= 4);
- if (!m_bEnabled) return FALSE;
- // Make sure Notification window is valid (not needed - CJM)
- VERIFY(m_bEnabled = (pParent && ::IsWindow(pParent->GetSafeHwnd())));
- // if (!m_bEnabled) return FALSE;
- // Make sure we avoid conflict with other messages
- ASSERT(uCallbackMessage >= WM_USER);
- // Tray only supports tooltip text up to 64 characters
- ASSERT(_tcslen(szToolTip) <= 64);
- // Create an invisible window
- // CWnd::CreateEx(0, AfxRegisterWndClass(0), _T(""), WS_POPUP, 0,0,10,10, NULL, 0);
- // load up the NOTIFYICONDATA structure
- m_tnd.cbSize = sizeof(NOTIFYICONDATA);
- m_tnd.hWnd = pParent->GetSafeHwnd();
- m_tnd.uID = uID;
- m_tnd.hIcon = icon;
- m_tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
- m_tnd.uCallbackMessage = uCallbackMessage;
- _tcscpy(m_tnd.szTip, szToolTip);
- // Set the tray icon
- VERIFY(m_bEnabled = Shell_NotifyIcon(NIM_ADD, &m_tnd));
- return m_bEnabled;
- }
- void CSystemTray::MoveToRight()
- {
- HideIcon();
- ShowIcon();
- }
- void CSystemTray::RemoveIcon()
- {
- if (!m_bEnabled) return;
- m_tnd.uFlags = 0;
- Shell_NotifyIcon(NIM_DELETE, &m_tnd);
- m_bEnabled = FALSE;
- }
- void CSystemTray::HideIcon()
- {
- if (m_bEnabled && !m_bHidden) {
- m_tnd.uFlags = NIF_ICON;
- Shell_NotifyIcon (NIM_DELETE, &m_tnd);
- m_bHidden = TRUE;
- }
- }
- void CSystemTray::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 CSystemTray::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);
- }
- LRESULT CSystemTray::OnTrayNotification(UINT wParam, LONG lParam)
- {
- //Return quickly if its not for this tray icon
- if (wParam != m_tnd.uID)
- return 0L;
- CMenu menu, *pSubMenu;
- CWnd* pTarget = AfxGetMainWnd();
- // Clicking with right button brings up a context menu
- if (LOWORD(lParam) == WM_RBUTTONUP)
- {
- if (!menu.LoadMenu(m_tnd.uID)) return 0;
- if (!(pSubMenu = menu.GetSubMenu(0))) return 0;
- // Make chosen menu item the default (bold font)
- ::SetMenuDefaultItem(pSubMenu->m_hMenu, m_DefaultMenuItemID, m_DefaultMenuItemByPos);
- // Display and track the popup menu
- CPoint pos;
- GetCursorPos(&pos);
- pTarget->SetForegroundWindow();
- ::TrackPopupMenu(pSubMenu->m_hMenu, TPM_LEFTALIGN |TPM_RIGHTBUTTON, pos.x, pos.y, 0,
- pTarget->GetSafeHwnd(), NULL);
- // BUGFIX: See "PRB: Menus for Notification Icons Don't Work Correctly"
- pTarget->PostMessage(WM_NULL, 0, 0);
- menu.DestroyMenu();
- }
- else if (LOWORD(lParam) == WM_LBUTTONDBLCLK)
- {
- // double click received, the default action is to execute default menu item
- pTarget->SetForegroundWindow();
- UINT uItem;
- if (m_DefaultMenuItemByPos)
- {
- if (!menu.LoadMenu(m_tnd.uID)) return 0;
- if (!(pSubMenu = menu.GetSubMenu(0))) return 0;
- uItem = pSubMenu->GetMenuItemID(m_DefaultMenuItemID);
- }
- else
- uItem = m_DefaultMenuItemID;
- pTarget->SendMessage(WM_COMMAND, uItem, 0);
- menu.DestroyMenu();
- }
- return 1;
- }