经常能够看到软件运行后在托盘产生图标 ,其实也就是对结构 NOTIFYICONDATA 的设置
再调用Shell_NotifyIcon就能完成
NOTIFYICONDATA notifycd;
notifycd.cbSize=sizeof(NOTIFYICONDATA);
notifycd.hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);//图标资源
notifycd.hWnd=m_hWnd;
notifycd.uID=IDR_MAINFRAME;
notifycd.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
lstrcpy( notifycd.szTip, _T( "" ) ); //默认的tooltip上的文字
notifycd.uCallbackMessage = WM_SHELLNOTIFY; //一旦在任务栏上产生图标,就回触发一定的消息
Shell_NotifyIcon(NIM_ADD,¬ifycd);
任务栏图标上的消息处理映射
ON_MESSAGE( WM_SHELLNOTIFY, OnShellNotify)
显示,删除,修改函数详细操作
//在托盘区显示图标
BOOL AddNotifyIcon(UINT Icon,LPCTSTR sztip, UINT ID)
{
HICON hIcon;
hIcon=AfxGetApp()->LoadIcon(Icon);
NOTIFYICONDATA idata;
idata.cbSize=sizeof(NOTIFYICONDATA);
idata.hIcon=hIcon;
CWnd *pWnd=AfxGetMainWnd();
idata.hWnd=GetSafeHwnd();
strcpy(idata.szTip,sztip);
idata.uCallbackMessage=WM_SHELLNOTIFY;
idata.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
idata.uID=ID;
return Shell_NotifyIcon(NIM_ADD,&idata);
}
//在托盘区删除图标
BOOL DeleteNotifyIcon(UINT Icon,LPCTSTR sztip, UINT ID)
{
HICON hIcon;
hIcon=AfxGetApp()->LoadIcon(Icon);
NOTIFYICONDATA idata;
idata.cbSize=sizeof(NOTIFYICONDATA);
idata.hIcon=hIcon;
CWnd *pWnd=AfxGetMainWnd();
idata.hWnd=GetSafeHwnd();
strcpy(idata.szTip,sztip);
idata.uCallbackMessage=WM_SHELLNOTIFY;
idata.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
idata.uID=ID;
return Shell_NotifyIcon(NIM_DELETE,&idata);
}
//在托盘区修改图标
BOOL ModifyNotifyIcon(UINT Icon,LPCTSTR sztip, UINT ID)
{
HICON hIcon;
hIcon=AfxGetApp()->LoadIcon(Icon);
NOTIFYICONDATA idata;
idata.cbSize=sizeof(NOTIFYICONDATA);
idata.hIcon=hIcon;
CWnd *pWnd=AfxGetMainWnd();
idata.hWnd=GetSafeHwnd();
strcpy(idata.szTip,sztip);
idata.uCallbackMessage=WM_SHELLNOTIFY;
idata.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
idata.uID=ID;
return Shell_NotifyIcon(NIM_MODIFY,&idata);
}