//定义托盘图标结构体
NOTIFYICONDATA nid;
#define WM_ICONNOTIFY WM_USER + 909 //图标响应消息(lParam)
#define ID_TASKBARICON 100 //参数(wParam)
//初始化系统托盘图标
void CMainFrame::OnBtnMinimize()
{
//初始化系统托盘图标
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = m_hWnd;;
nid.uID = ID_TASKBARICON; //发出的消息中的wParam参数
nid.uFlags = NIF_ICON|NIF_MESSAGE|NIF_TIP;
nid.uCallbackMessage = WM_ICONNOTIFY; //点击托盘图标系统发出的消息(即发出的消息中的lParam参数)
nid.hIcon = LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_HIS_COMM));
strcpy(nid.szTip, "历史数据采集软件 1.0.3");
Shell_NotifyIcon(NIM_ADD, &nid); //加入图标
ShowWindow(SW_HIDE); //隐藏窗体
}
//手工添加消息处理函数
afx_msg LRESULT OnNotifyIcon(WPARAM wParam, LPARAM lParam);
ON_MESSAGE(WM_ICONNOTIFY,CMainFrame::OnNotifyIcon)
//双击系统托盘,使程序正常化
LRESULT CMainFrame::OnNotifyIcon(WPARAM wParam, LPARAM lParam)
{
POINT rpoint;
CMenu tmenu;
GetCursorPos(&rpoint);
//如果在托盘图标上双击左键
if ( ( wParam == ID_TASKBARICON ) && ( lParam == WM_LBUTTONDBLCLK ) )
{
ShowWindow(SW_SHOW); //显示窗体
Shell_NotifyIcon(NIM_DELETE, &nid); //删除系统托盘图标
}
//如果在托盘图标上双击右键
if ( ( wParam == ID_TASKBARICON ) && ( lParam == WM_RBUTTONDOWN ) )
{
tmenu.LoadMenu(IDR_MAINFRAME); //加载系统主菜单
SetForegroundWindow(); //将程序置于桌面顶部
tmenu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,rpoint.x, rpoint.y, this); //在点击图标处弹出系统菜单的第一项
}
return 0;
}
托盘图标闪烁
主要是通过一个定时器,当时间到的时候,替换原来的图标,如:
void CFlashIconDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
//记录当前要显示的图标
static nState=ICON_BLANK;
if(nIDEvent==EVENT_BLINK)
{
//图标闪烁定时到
switch(nState)
{
case ICON_BLANK:
m_hIconTray.hIcon=AfxGetApp()->LoadIcon(IDI_BLANK);
Shell_NotifyIcon(NIM_MODIFY,&m_hIconTray);
nState=m_bMale?ICON_POPEYE:ICON_OLIVE;
break;
case ICON_POPEYE:
m_hIconTray.hIcon=AfxGetApp()->LoadIcon(IDI_POPEYE);
Shell_NotifyIcon(NIM_MODIFY,&m_hIconTray);
nState=ICON_BLANK;
break;
case ICON_OLIVE:
m_hIconTray.hIcon=AfxGetApp()->LoadIcon(IDI_OLIVE);
Shell_NotifyIcon(NIM_MODIFY,&m_hIconTray);
nState=ICON_BLANK;
break;
}
}
CDialog::OnTimer(nIDEvent);
}