// TrayIcon.cpp
// CTrayIcon
CTrayIcon::CTrayIcon()
{//初始化参数
m_IconExist = FALSE;
m_NotificationWnd = NULL;
memset(&m_nid, 0, sizeof(m_nid));
m_nid.cbSize = sizeof(m_nid);//这个参数不会改变
}
CTrayIcon::~CTrayIcon()
{
if (m_IconExist)
DeleteIcon();//删除图标
}
BOOL CTrayIcon::CreateIcon(CWnd* pNotifyWnd, UINT uID, HICON hIcon,
LPSTR lpszTip, UINT CallBackMessage)
{
//确定接受回调消息的窗口是有效的
ASSERT(pNotifyWnd && ::IsWindow(pNotifyWnd->GetSafeHwnd()));
ASSERT(CallBackMessage >= WM_USER);//确定回调消息不发生冲突
ASSERT(_tcslen(lpszTip) <= 64);//提示字串不能超过64个字符
m_NotificationWnd = pNotifyWnd;//获得m_NotificationWnd
//设置NOTIFYICONDATA结构
m_nid.hWnd = pNotifyWnd->GetSafeHwnd();
m_nid.uID = uID;
m_nid.hIcon = hIcon;
m_nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
m_nid.uCallbackMessage = CallBackMessage;
//设置NOTIFYICONDATA结构的提示字串
if (lpszTip)
lstrcpyn(m_nid.szTip, lpszTip, sizeof(m_nid.szTip));
else
m_nid.szTip[0] = '\0';
//显示图标
m_IconExist = Shell_NotifyIcon(NIM_ADD, &m_nid);
return m_IconExist;
}
BOOL CTrayIcon::DeleteIcon()
{//删除图标
if (!m_IconExist)
return FALSE;
m_IconExist = FALSE;
return Shell_NotifyIcon(NIM_DELETE, &m_nid);
}
LRESULT CTrayIcon::OnNotify(WPARAM WParam, LPARAM LParam)
{//处理图标返回的消息
if (WParam != m_nid.uID)//如果不是该图标的消息则迅速返回
return 0L;
//准备快捷菜单
CMenu menu;
if (!menu.LoadMenu(IDR_POPUP))//你必须确定资源中有ID为IDR_POPUP的菜单
return 0;
CMenu* pSubMenu = menu.GetSubMenu(0);//获得IDR_POPUP的子菜单
if (!pSubMenu)
return 0;
if (LParam == WM_RBUTTONUP)
{//右键单击弹出快捷菜单
//设置第一个菜单项为缺省
::SetMenuDefaultItem(pSubMenu->m_hMenu, 0, TRUE);
CPoint pos;
GetCursorPos(&pos);
//显示并跟踪菜单
m_NotificationWnd->SetForegroundWindow();
pSubMenu->TrackPopupMenu(TPM_RIGHTALIGN|TPM_LEFTBUTTON
|TPM_RIGHTBUTTON, pos.x, pos.y, m_NotificationWnd, NULL);
}
else if (LParam == WM_LBUTTONDOWN)
{//左键单击恢复窗口
m_NotificationWnd->ShowWindow(SW_SHOW);//恢复窗口
m_NotificationWnd->SetForegroundWindow();//放置在前面
}
else if (LParam == WM_LBUTTONDBLCLK)
{//左键双击执行缺省菜单项
m_NotificationWnd->SendMessage(WM_COMMAND,
pSubMenu->GetMenuItemID(0), 0);
}
return 1L;
}
BOOL CTrayIcon::SetTipText(LPCTSTR lpszTip)
{//设置提示文字
if (!m_IconExist)
return FALSE;
_tcscpy(m_nid.szTip, lpszTip);
m_nid.uFlags |= NIF_TIP;
return Shell_NotifyIcon(NIM_MODIFY, &m_nid);
}
BOOL CTrayIcon::SetTipText(UINT nID)
{//设置提示文字
CString szTip;
VERIFY(szTip.LoadString(nID));
return SetTipText(szTip);
}
BOOL CTrayIcon::ChangeIcon(HICON hIcon)
{//更改图标
if (!m_IconExist)
return FALSE;
m_nid.hIcon = hIcon;
m_nid.uFlags |= NIF_ICON;
return Shell_NotifyIcon(NIM_MODIFY, &m_nid);
}
BOOL CTrayIcon::ChangeIcon(UINT nID)
{//更改图标
HICON hIcon = AfxGetApp()->LoadIcon(nID);
return ChangeIcon(hIcon);
}
BOOL CTrayIcon::ChangeIcon(LPCTSTR lpszIconName)
{//更改图标
HICON hIcon = AfxGetApp()->LoadIcon(lpszIconName);
return ChangeIcon(hIcon);
}
BOOL CTrayIcon::ChangeStandardIcon(LPCTSTR lpszIconName)
{//更改为标准图标
HICON hIcon = AfxGetApp()->LoadStandardIcon(lpszIconName);
return ChangeIcon(hIcon);
}
BOOL CTrayIcon::SetNotificationWnd(CWnd * pNotifyWnd)
{//设置接受回调消息的窗口
if (!m_IconExist)
return FALSE;
//确定窗口是有效的
ASSERT(pNotifyWnd && ::IsWindow(pNotifyWnd->GetSafeHwnd()));
m_NotificationWnd = pNotifyWnd;
m_nid.hWnd = pNotifyWnd->GetSafeHwnd();
m_nid.uFlags |= NIF_MESSAGE;
return Shell_NotifyIcon(NIM_MODIFY, &m_nid);
}
CWnd* CTrayIcon::GetNotificationWnd() const
{//返回接受回调消息的窗口
return m_NotificationWnd;
}