MFC 托盘按钮和闪烁的制作

1 填写NOTIFYICONDATA结构体,并想任务栏发送消息

void  CTestaDlg::TrayMessage(DWORD dwmessage)//向任务栏托盘发送消息
{
    NOTIFYICONDATA icon;//结构体
    icon.hIcon = m_hIcon;
    icon.cbSize = sizeof(NOTIFYICONDATA);
    icon.hWnd = m_hWnd;
    icon.uID = 1;
    icon.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
    icon.uCallbackMessage = WM_SHOW;
    char* str = "hello";
    strcpy(icon.szTip,str);
    if(dwmessage == NIM_MODIFY)//修改任务栏图标通过定时器是闪烁
    {
        if(flash == 0)
            icon.hIcon = NULL;//图标变为空
        else
            icon.hIcon = m_hIcon;






    }
    Shell_NotifyIcon(dwmessage,&icon);//向任务栏发送消息


}


2 显示任务栏图标


TrayMessage(NIM_ADD);


3写回调函数,区分对图标的不同操作

void CTestaDlg::OnMessage(WPARAM wparam,LPARAM lparam)
{
  UINT uID;
  uID = (UINT)wparam;
  UINT uMouseMsg;
  uMouseMsg = (UINT)lparam;
  POINT pt;
  if(uMouseMsg == WM_RBUTTONDOWN)
  {
      if(uID == 1)
      {
          CMenu menu;
          menu.LoadMenu(IDR_MENU1);
          GetCursorPos(&pt);
          //menu.TrackPopupMenu(0,pt.x,pt.y,this);
           CMenu *pPopupMenu = menu.GetSubMenu(0);
           pPopupMenu->TrackPopupMenu(0,pt.x,pt.y,this);


      }

  }

  if(uMouseMsg == WM_LBUTTONDOWN)
  {
      
      if(uID == 1)
      {
          if(flag == 1)
          {
              ShowWindow(SW_SHOW);
              flag = 0;
          }
          else
          {
              ShowWindow(SW_HIDE);
              flag = 1;
          }

      }

  }
  if(uMouseMsg == WM_MOUSEMOVE)
  {
      if(uID==1 && flash ==1 )
      {
          KillTimer(0);
      }
  }



}

WM_SHOW为自定义消息

 

源码连接:http://download.csdn.net/detail/u010442009/6920213




你可能感兴趣的:(c++,builder学习)