在头文件MainFrm.h中声明NOTIFYICONDATA nid;
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
//***** 初始化系统托盘图标 *****//
nid.cbSize=(DWORD)sizeof(NOTIFYICONDATA);
nid.hWnd=this->m_hWnd;
nid.uID=IDR_MAINFRAME;
nid.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP ;
nid.uCallbackMessage=WM_USER+1;//自定义的消息名称
nid.hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));
strcpy(nid.szTip,"双击打开多功能电子钟");//信息提示条
Shell_NotifyIcon(NIM_ADD,&nid);//在托盘区添加图标
//***** 初始化系统托盘图标 *****//
//CenterWindow();
}
LRESULT CMainFrame::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message){
case WM_USER+1: //如果是用户定义的消息
if(lParam==WM_LBUTTONDOWN) //WM_LBUTTONDBLCLK不怎么灵
{
//鼠标单击时主窗口出现
::SetForegroundWindow(this->m_hWnd); //::SetWindowPos(this->m_hWnd,HWND_TOP,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
AfxGetApp()->m_pMainWnd->ShowWindow(SW_SHOW);
WindowIsHide=false;
return 0;
}
else if(lParam==WM_RBUTTONDOWN) // && WindowIsHide
{
::SetForegroundWindow(this->m_hWnd); //将主框架窗口置前,左击其他地方,菜单消失
//鼠标右键单击弹出菜单
CMenu menu;
menu.LoadMenu(IDR_MAINFRAME);
CMenu* pMenu=menu.GetSubMenu(0);
CPoint point;
GetCursorPos(&point);
pMenu->TrackPopupMenu(TPM_LEFTALIGN,point.x,point.y,AfxGetMainWnd());
::PostMessage(this->m_hWnd, WM_NULL, 0,0); //左击其他地方,菜单消失
menu.DestroyMenu();
return 0;
}
break;
case WM_SYSCOMMAND: //如果是系统消息
if(wParam==SC_MINIMIZE||wParam==SC_CLOSE||wParam==SC_MAXIMIZE||wParam==SC_SIZE)
{
//主窗口隐藏
AfxGetApp()->m_pMainWnd->ShowWindow(SW_HIDE);
WindowIsHide=true;
return 0;
}
break;
}
return CFrameWnd::WindowProc(message, wParam, lParam);
}
void CMainFrame::OnDestroy()
{
CFrameWnd::OnDestroy();
::Shell_NotifyIcon(NIM_DELETE,&nid);
}