VC中使程序最小化到系统托盘区
//定义托盘图标结构体
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;
}