mfc 托盘技术(taskbar status area)

本文介绍了如何在MFC程序中创建任务栏图标并添加托盘菜单。通过`InitNotifyIconData`函数设置图标和提示信息,并在用户交互时响应自定义消息`WM_SHELLNOTIFY`,实现右键菜单和左键点击的相应功能。在程序关闭时,使用`OnDestroy`函数删除图标,确保干净退出。此外,还展示了在最小化窗口时如何隐藏窗口。
摘要由CSDN通过智能技术生成

 1.让自己所编软件的图标显示在 taskbar status area(任务栏)处

创建一个基于对话框的MFC程序HelloWorld,在CHelloWorldDlg类中新建一个成员函数void InitNotifyIconData(void)。其具体实现如下

void CsupernotepadDlg::InitNotifyIconData(void)
{
 /*NOTIFYCONDATA structure contains the information that the system
 needs to process taskbar status area message*/
 NOTIFYICONDATA nd;

 //the size of this structure,in bytes
 nd.cbSize = sizeof(NOTIFYICONDATA); 

 //a handle to the window that receives the notification message associated with an icon in the taskbar status area
 nd.hWnd = m_hWnd;

 //the application-defined identifier of the taskbar icon
 nd.uID = IDR_MAINFRAME; 

 //Flags that indicate which of the other members contain valid data
 nd.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;

 // is the identifier.The system use it to send notification to the window identified in hWnd
 nd.uCallbackMessage = WM_SHELLNOTIFY;    // 自定义消息。当在托盘图标上有鼠标事件时,发送该消息

 nd.hIcon = m_hIcon;

 lstrcpy(nd.szTip,_T("HelloWorld"));
 //send the message to the taskbar status area
 Shell_NotifyIcon(NIM_ADD,&nd);
}

// 自定义消息(即上面的SHELLNOTIFY消息)响应函数

LRESULT CsupernotepadDlg::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;
   int pMenuID = 0;

   VERIFY(menu.LoadMenu(IDR_MENU1));//得到菜单
   CMenu* pPopup = menu.GetSubMenu(pMenuID);//得到子菜单
   ASSERT(pPopup != NULL);
   
   DWORD Made = pPopup->TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_NONOTIFY|TPM_RETURNCMD,lpoint->x,lpoint->y,this,NULL);//使用子菜单
   
   switch(Made)
   {
   case  ID_MENUITEM32771: {DestroyWindow();} break;
   case  ID_MENUITEM32772: {ShowWindow(SW_SHOW);ShowWindow(SW_SHOWNORMAL);}  break; 
   case  ID_MENUITEM32773: {CAboutDlg dlgAbout;dlgAbout.DoModal();}  break; 
   }

   pPopup->DestroyMenu();
   HMENU hmenu=menu.Detach();//资源回收
   menu.DestroyMenu();
   delete lpoint;
  }

  break;
 case WM_LBUTTONDOWN://左键的处理
  {
  
   ShowWindow(SW_SHOW);  //简单的显示主窗口完事儿
   ShowWindow(SW_SHOWNORMAL); //可以避免原来的最小化
  }
  break;
 }
 return 0;
}

最后,需要在HelloWorldDlg.h中添加如下代码

#define WM_SHELLNOTIFY (WM_APP + 1)

在cpp文件中加入自定义消息宏,将自定义消息和响应函数关联起来

ON_MESSAGE(SHELLNOTIFY,OnShowTask)

2.关闭程序时自动清除图标

在类视图中,右击CsupernotepadDlg,选择“属性”(properties),点击“消息”(message),添加WM_ONDESTROY,在生成的OnDestroy函数中填写如下代码

void CsupernotepadDlg::OnDestroy()
{
 CDialog::OnDestroy();

 // TODO: Add your message handler code here
 NOTIFYICONDATA nd;
 nd.cbSize = sizeof(NOTIFYICONDATA);
 nd.hWnd  = m_hWnd;
 nd.uID       = IDR_MAINFRAME;

 //DELETE THE ICON
 Shell_NotifyIcon(NIM_DELETE,&nd);
}

3.实现点击最小化按钮时,程序最小化到任务栏

在CHelloWorldDlg类中加载onsize消息(方法同上),

void CHelloWorld::OnSize(UINT nType, int cx, int cy)
{
 CDialog::OnSize(nType, cx, cy);

 // TODO: Add your message handler code here
 if( nType = SIZE_MINIMIZED )
 {
  //hide this window and passes the activition to another window
  ShowWindow(SW_HIDE);
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值