1.minisysDlg.h头文件设置:
1)public: void setTray();//设置托盘 NOTIFYICONDATA nid;//NOTIFYICONDATA结构包含了系统用来处理托盘图标的信息 2)protected: // Generated message map functions //{ {AFX_MSG(CminisysDlg) afx_msg LRESULT onShowTask(WPARAM wParam,LPARAM lParam); //}}AFX_MSG 2.minisysDlg.cpp实现设置: 1) #define WM_SHOWTASK (WM_USER + 1) //设置响应消息 2) BEGIN_MESSAGE_MAP(CminisysDlg, CDialog) //{ {AFX_MSG_MAP(CminisysDlg) ON_MESSAGE(WM_SHOWTASK,onShowTask) //}}AFX_MSG_MAP END_MESSAGE_MAP() 3)最小化菜单响应 void CminisysDlg::OnSysCommand(UINT nID, LPARAM lParam) { if(nID==SC_MINIMIZE) { setTray();//最小化到托盘 } } 4)托盘函数 void CminisysDlg::setTray() { 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_SHOWTASK;//自定义的消息名称 nid.hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME)); strcpy(nid.szTip,"ClientTracer"); //信息提示条 Shell_NotifyIcon(NIM_ADD,&nid); //在托盘区添加图标 ShowWindow(SW_HIDE); //隐藏主窗 } 5)消息函数 LRESULT CminisysDlg::onShowTask(WPARAM wParam,LPARAM lParam) { //wParam接收的是图标的ID,而lParam接收的是鼠标的行为 if(wParam!=IDR_MAINFRAME) return 1; switch(lParam) { case WM_RBUTTONUP://右键起来时弹出快捷菜单,这里只有一个“关闭” { LPPOINT lpoint=new tagPOINT; ::GetCursorPos(lpoint);//得到鼠标位置 CMenu menu; menu.CreatePopupMenu();//声明一个弹出式菜单 //增加菜单项“关闭”,点击则发送消息WM_DESTROY给主窗口,将程序结束。 menu.AppendMenu(MF_STRING,WM_DESTROY,"退出"); //确定弹出式菜单的位置 SetForegroundWindow();//弹出菜单失去焦点后自动消失 menu.TrackPopupMenu(TPM_LEFTALIGN,lpoint->x,lpoint->y,this); //资源回收 HMENU hmenu=menu.Detach(); menu.DestroyMenu(); delete lpoint; } break; case WM_LBUTTONDBLCLK://双击左键的处理 { Shell_NotifyIcon(NIM_DELETE,&nid);//删除托盘图标 ShowWindow(SW_SHOWNORMAL);//显示主窗口 //this->SetWindowPos(&wndTopMost,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);//使窗口总是在最前面 } break; } return 0; } 6)继承WM_DESTROY: void CminisysDlg::OnDestroy() { CDialog::OnDestroy(); // TODO: Add your message handler code here Shell_NotifyIcon(NIM_DELETE,&nid); //退出程序后删除托盘图标 } 7) 映射ON_SIZE: void CminisysDlg::OnSize(UINT nType, int cx, int cy) { CDialog::OnSize(nType, cx, cy); // TODO: Add your message handler code here if(nType == SIZE_MINIMIZED) { ShowWindow(SW_HIDE); // 当最小化市,隐藏主窗口 } }3.说明:代码均是添加到现有MFC框架代码!
源代码下载地址: