MFC添加托盘全过程

.h中

 

NOTIFYICONDATA nid;

 afx_msg LONG OnNotifyIcon(WPARAM wParam, LPARAM lParam);//定义的一个

 

。c中

 ON_MESSAGE(WM_NOTIFYICON, OnNotifyIcon)//手工添加托盘消息映射

 

void CDlg::InitTray()
{
 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_NOTIFYICON;//自定义的消息名称
 nid.hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));
 // strcpy(nid.szTip,_I("MMarket"));//信息提示
 lstrcpy(nid.szTip,_T("Mobile Market"));
 Shell_NotifyIcon(NIM_ADD,&nid);//在托盘区添加图标 
}


LONG CDlg::OnNotifyIcon(WPARAM wParam, LPARAM lParam)
{
 //wParam中是响应消息的图标ID,lParam中则是Windows的消息

 //m_menu.AttachMenu(GetMenu()->GetSafeHmenu(),NULL,CSize(16,15));
 switch ( lParam )
 {
 case WM_LBUTTONDOWN:// 用户在托盘图标上单击鼠标左、右键,弹出菜单
  {
   CWnd *pwnd=GetTopWindow();
   if(IsIconic())//pwnd->m_hWnd==this->m_hWnd
   {
    OnNomalFrm();
    this->SetForegroundWindow();
   }
   else
   {
    OnMinFrm();
   }
  }
  break;
 case WM_RBUTTONDOWN:
  {
   CMenuEx *pPopup,menu;
   menu.LoadMenu(IDR_MENU_TRAY);
   //menu.SetImageLeft(IDB_BITMAP1,TRUE);
   //menu.LoadToolBar(IDR_TOOLBAR1,0);
   //pMenu = (CMenuEx *)menu.GetSubMenu(0);
   pPopup = (CMenuEx *)menu.GetSubMenu(0);
   //pPopup->Detach();
   //pPopup->AttachMenu(pMenu.m_hMenu,NULL,CSize(16,15));

   ASSERT(pPopup != NULL);
   CPoint Point;
   //if(IsIconic()) pPopup->EnableMenuItem(IDC_MIN,MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
#if 0
   if (IsWindowVisible())
   {// 根据对话框窗口的显示/隐藏状态修改菜单名称
    if(!IsIconic())
     ;//pPopup->EnableMenuItem(IDR_MENU_SHOW,MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
   }
#endif
   //else
   // pPopup->EnableMenuItem(ID_MENUITEM_HIDE,MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
   // 确定鼠标位置以便在该位置附近显示菜单
   GetCursorPos( &Point );
   SetForegroundWindow();
   //pPopup->SetSelectColor();
   pPopup->TrackPopupMenu(
    TPM_LEFTALIGN | TPM_RIGHTBUTTON,
    Point.x, Point.y, this);
   //m_menu.Detach();
   //m_menu.AttachMenu(pPopup->GetSafeHmenu(),NULL,CSize(16,15));


  }         
  break;
 case WM_LBUTTONDBLCLK:
  ShowWindow(SW_SHOWNORMAL);
  this->SetForegroundWindow();
  CenterWindow();
  break;
 default:
  break;
 }
 return 0;
}

 

Shell_NotifyIcon(NIM_DELETE, &nid);//最后释放

你可能感兴趣的:(windows,shell,null,delete,mfc,menu)