typedef struct _NOTIFYICONDATA { DWORD cbSize; //以字节为单位的这个结构的大小 HWND hWnd; //接收托盘图标通知消息的窗口句柄 UINT uID; //应用程序定义的该图标的ID号 UINT uFlags; //设置该图标的属性 UINT uCallbackMessage; //应用程序定义的消息ID号,此消息传递给hWnd HICON hIcon; //图标的句柄 char szTip[64]; //鼠标停留在图标上显示的提示信息 } NOTIFYICONDATA, *PNOTIFYICONDATA; |
WINSHELLAPI BOOL WINAPI Shell_NotifyIcon( DWORD dwMessage, PNOTIFYICONDATA pnid); |
m_tnid.cbSize=sizeof(NOTIFYICONDATA); m_tnid.hWnd=this->m_hWnd; m_tnid.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP; m_tnid.uCallbackMessage=MYWM_NOTIFYICON; //用户定义的回调消息 CString szToolTip; szToolTip=_T("托盘图标实例"); _tcscpy(m_tnid.szTip, szToolTip); m_tnid.uID=IDR_MAINFRAME; HICON hIcon; hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME); m_tnid.hIcon=hIcon; ::Shell_NotifyIcon(NIM_ADD,&m_tnid); if(hIcon)::DestroyIcon(hIcon); |
#define MYWM_NOTIFYICON WM_USER+1 |
LRESULT CMainFrame::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) { switch(message){ case MYWM_NOTIFYICON: //如果是用户定义的消息 if(lParam==WM_LBUTTONDBLCLK) { //鼠标双击时主窗口出现 AfxGetApp()- >m_pMainWnd->ShowWindow(SW_SHOW); } else if(lParam==WM_RBUTTONDOWN){ //鼠标右键单击弹出菜单 CMenu menu; menu.LoadMenu(IDR_RIGHT_MENU); //载入事先定义的菜单 CMenu* pMenu=menu.GetSubMenu(0); CPoint pos; GetCursorPos( &pos); pMenu- >TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,pos.x,pos.y,AfxGetMainWnd()); } break; case WM_SYSCOMMAND: //如果是系统消息 if(wParam==SC_MINIMIZE){ //接收到最小化消息时主窗口隐藏 AfxGetApp()- >m_pMainWnd->ShowWindow(SW_HIDE); return 0; } break; } return CFrameWnd::WindowProc(message, wParam, lParam); } |