//放在主界面对话框对应的位置
OnInitDialog()
{
NOTIFYICONDATA m_tnid;
m_tnid.cbSize=sizeof(NOTIFYICONDATA);//设置结构大小//
m_tnid.hWnd=this->m_hWnd;//设置图标对应的窗口
m_tnid.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;//图标属性
m_tnid.uCallbackMessage=WM_ICON_NOTIFY;//应用程序定义的回调消息ID
CString szToolTip;
szToolTip=_T("INeed 客户端程序");
_tcscpy_s(m_tnid.szTip, szToolTip);//帮助信息
m_tnid.uID=IDR_MAINFRAME;//应用程序图标
m_tnid.hIcon=m_hIcon;//图标句柄
PNOTIFYICONDATA m_ptnid=&m_tnid;
::Shell_NotifyIcon(NIM_ADD,m_ptnid);//增加图标到系统盘
}
#define WM_ICON_NOTIFY WM_USER+100
ON_MESSAGE(WM_ICON_NOTIFY, OnTrayNotification)
LRESULT OnTrayNotification(WPARAM wParam, LPARAM lParam);
LRESULT CXXXXDlg::OnTrayNotification( WPARAM wParam, LPARAM lParam )
{
switch(lParam)
{
case WM_LBUTTONDOWN:
{
AfxGetApp()->m_pMainWnd->ShowWindow(SW_SHOWNORMAL);
SetForegroundWindow();
break;
}
case WM_RBUTTONUP:
{
POINT point;
HMENU hMenu, hSubMenu;
GetCursorPos(&point); //鼠标位置
hMenu = LoadMenu(NULL,
MAKEINTRESOURCE(IDR_MENU_TRAY)); // 加载菜单
hSubMenu = GetSubMenu(hMenu, 0);//得到子菜单(因为弹出式菜单是子菜单)
SetForegroundWindow(); // 激活窗口并置前
TrackPopupMenu(hSubMenu, 0,
point.x, point.y, 0, m_hWnd, NULL);
}
}
return 1;
}
OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
if(nID == SC_MINIMIZE)
{
ShowWindow(FALSE); //隐藏窗口
}
}
OnClose()
{
NOTIFYICONDATA nd = {0};
nd.cbSize = sizeof(NOTIFYICONDATA);
nd.hWnd = m_hWnd;
nd.uID = IDR_MAINFRAME;
nd.uFlags = NIF_ICON|NIF_MESSAGE|NIF_TIP;
nd.uCallbackMessage = WM_ICON_NOTIFY;
nd.hIcon = m_hIcon;
Shell_NotifyIcon(NIM_DELETE, &nd);
}
0则评论给“mfc 托盘设置”