nid.cbSize = sizeof(nid); //大小赋值
nid.hWnd = m_hWnd; //父窗口
nid.uID = IDR_MAINFRAME; //icon ID
nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; //托盘所拥有的状态
nid.uCallbackMessage = UM_ICONNOTIFY; //回调消息
nid.hIcon = m_hIcon; //icon 变量
CString str="PCRemote远程协助软件........."; //气泡提示
lstrcpyn(nid.szTip, (LPCSTR)str, sizeof(nid.szTip) / sizeof(nid.szTip[0]));
Shell_NotifyIcon(NIM_ADD, &nid); //显示托盘
//自定义消息
enum
{
UM_ICONNOTIFY= WM_USER+0x100,
};
cpp文件中写入代码:
void CPCRemoteDlg::OnIconNotify(WPARAM wParam, LPARAM lParam)
{
switch ((UINT)lParam)
{
case WM_LBUTTONDOWN: // click or dbclick left button on icon
case WM_LBUTTONDBLCLK: // should show desktop
if (!IsWindowVisible())
ShowWindow(SW_SHOW);
else
ShowWindow(SW_HIDE);
break;
case WM_RBUTTONDOWN: // click right button, show menu
CMenu menu;
menu.LoadMenu(IDR_MENU_NOTIFY);
CPoint point;
GetCursorPos(&point);
SetForegroundWindow();
menu.GetSubMenu(0)->TrackPopupMenu(
TPM_LEFTBUTTON|TPM_RIGHTBUTTON,
point.x, point.y, this, NULL);
PostMessage(WM_USER, 0, 0);
break;
}
}
//自定义消息
ON_MESSAGE(UM_ICONNOTIFY, OnIconNotify)
void CPCRemoteDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
Shell_NotifyIcon(NIM_DELETE,&nid);
CDialog::OnClose();
}