只写出代码,具体的资源不说了
1.ON_MESSAGE(WM_TRAY_NOTIFICATION,OnTrayNotification)
2.afx_msg LRESULT OnTrayNotification(WPARAM wParam,LPARAM lParam);
3.
//系统托盘
LRESULT CNetCapDlg::OnTrayNotification(WPARAM wParam,LPARAM lParam)
{
// static BOOL Flag = TRUE;
if ((wParam == IDI_ICON1)&&(lParam == WM_LBUTTONDOWN))
{
this->OnShowTray();
}
else if ((lParam == WM_RBUTTONDOWN))
{
CMenu oMenu;
if (oMenu.LoadMenu(IDR_TRAY))
{
CMenu* pPopup = oMenu.GetSubMenu(0);
ASSERT(pPopup != NULL);
CPoint oPoint;
if (IsWindowVisible())// 将显示改为隐藏
oMenu.ModifyMenu(ID_SHOW_TRAY,MF_STRING,ID_SHOW_TRAY,"隐藏(&H)");
else
oMenu.ModifyMenu(ID_SHOW_TRAY,MF_STRING,ID_SHOW_TRAY,"显示(&S)");
//确定鼠标位置,以便在该位置附近显示菜单
GetCursorPos( &oPoint );
SetForegroundWindow();
pPopup->TrackPopupMenu(
TPM_LEFTALIGN | TPM_RIGHTBUTTON,
oPoint.x, oPoint.y, this);
}
}
return 0;
}
4.OnInitDialog()中添
//系统拖盘
NOTIFYICONDATA NofityData;
NofityData.cbSize=sizeof(NOTIFYICONDATA);
NofityData.hWnd=this->m_hWnd;
NofityData.uID=IDI_ICON1;
NofityData.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
NofityData.uCallbackMessage=WM_TRAY_NOTIFICATION;
HICON hIcon;
hIcon=AfxGetApp()->LoadIcon(IDI_ICON1);
NofityData.hIcon=hIcon;
lstrcpyn(NofityData.szTip,"NetCap",sizeof(NofityData.szTip));
::Shell_NotifyIcon(NIM_ADD,&NofityData);
5.函数退出时候,删除托盘
void CNetCapDlg::OnQuitTray()
{
// TODO: Add your command handler code here
CDialog::OnCancel();
/
NOTIFYICONDATA NofityData1;
NofityData1.cbSize=sizeof(NOTIFYICONDATA);
NofityData1.hWnd=this->m_hWnd;
NofityData1.uID=IDI_ICON1;
NofityData1.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
::Shell_NotifyIcon(NIM_DELETE,&NofityData1);
}
6.修改托盘信息
NOTIFYICONDATA NofityData;
NofityData.cbSize=sizeof(NOTIFYICONDATA);
NofityData.hWnd=this->m_hWnd;
NofityData.uID=IDR_MAINFRAME;
NofityData.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
HICON hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
NofityData.hIcon = hIcon;
NofityData.uCallbackMessage=WM_MY_TRAY_NOTIFICATION;
lstrcpyn(NofityData.szTip,"ServiceCPP",sizeof(NofityData.szTip));
::Shell_NotifyIcon(NIM_MODIFY,&NofityData);