托盘上的菜单右键弹出后,然后再别的地方,比如桌面,菜单无法隐藏掉,一直在托盘上,除非操作下菜单才能隐藏掉,只需要加上一句话
::SetForegroundWindow(m_nid.hWnd);
就可以解决这个问题。
下面是托盘创建
class CXXXDlg{private:NOTIFYICONDATA m_trayid; //作为托盘ID};
//创建托盘void CXXXDlg::CreateTray(HICON icon){ m_trayid.cbSize = (DWORD)sizeof(NOTIFYICONDATA); m_trayid.hWnd = this->m_hWnd; m_trayid.uID = 128; m_trayid.uFlags = NIF_ICON|NIF_MESSAGE|NIF_TIP; m_trayid.uCallbackMessage = UM_SHOW_THIRDBIND; m_trayid.hIcon = icon; wcscpy_s(m_trayid.szTip, _T("我是托盘")); Shell_NotifyIcon(NIM_ADD, &m_trayid);}
//显示托盘tooltipvoid CXXXDlg::ShowTrayTooltip(){ m_trayid.cbSize=sizeof(NOTIFYICONDATA); m_trayid.uFlags = NIF_INFO; m_trayid.uVersion = NOTIFYICON_VERSION; m_trayid.uTimeout = 5000; m_trayid.dwInfoFlags = NIIF_INFO; wcscpy_s(m_trayid.szInfoTitle, L"提示" ); //气泡标题 wcscpy_s(m_trayid.szInfo, L"我是托盘tooltip"); //气泡内容 Shell_NotifyIcon( NIM_MODIFY, &m_trayid);}
LRESULT CXXXDlg::onShowTask(WPARAM wParam,LPARAM lParam) //托盘响应消息 { if(wParam !=128) return 1; switch(lParam) { case WM_RBUTTONUP: { ::SetForegroundWindow(m_nid.hWnd); //点击别的地方,托盘隐藏 LPPOINT lpoint=new tagPOINT; ::GetCursorPos(lpoint); m_menu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON, lpoint->x, lpoint->y, this); delete lpoint; break; } case WM_LBUTTONDBLCLK: { this->ShowWindow(SW_SHOW); SetForegroundWindow(); } break; } return 0; }