AnimateTray.cpp
上传用户:cn05999
上传日期:2020-06-29
资源大小:84k
文件大小:5k
- // AnimateTray.cpp : implementation file
- //
- #include "stdafx.h"
- #include "CProgressCtrl.h"
- #include "AnimateTray.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CAnimateTray
- CAnimateTray::CAnimateTray()
- {
- memset(&m_NotifyIconData,0,sizeof(m_NotifyIconData));
- m_bCreated=FALSE;
- m_bHidden=FALSE;
- m_pNotificationWnd=NULL;
- // m_bTimerOn=FALSE;
- m_bAnimated=FALSE;
- m_bMenuOn=FALSE;
- }
- CAnimateTray::~CAnimateTray()
- {
- // if(m_bTimerOn)
- KillTimer(m_nTimerID);
- RemoveIcon();
- }
- BEGIN_MESSAGE_MAP(CAnimateTray, CWnd)
- //{{AFX_MSG_MAP(CAnimateTray)
- ON_WM_TIMER()
- ON_WM_CREATE()
- ON_WM_DESTROY()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CAnimateTray message handlers
- BOOL CAnimateTray::CreateTray(CWnd *pNotifyWnd, UINT uID, LPCTSTR pszTooltipText, HICON hIcon, UINT nNotifyMessage)
- {
- ASSERT(pNotifyWnd&&::IsWindow(pNotifyWnd->GetSafeHwnd()));
- m_pNotificationWnd=pNotifyWnd;
- ASSERT(nNotifyMessage>=WM_USER);
- m_NotifyIconData.cbSize=sizeof(m_NotifyIconData);
- m_NotifyIconData.hWnd=pNotifyWnd->GetSafeHwnd();
- m_NotifyIconData.uID=uID;
- m_NotifyIconData.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP;
- m_NotifyIconData.uCallbackMessage=nNotifyMessage;
- m_NotifyIconData.hIcon=hIcon;
- _tcscpy(m_NotifyIconData.szTip,pszTooltipText);
- BOOL rVal=Shell_NotifyIcon(NIM_ADD,&m_NotifyIconData);
- m_bCreated=rVal;
-
- return rVal;
- }
- BOOL CAnimateTray::CreateTray(CWnd *pNotifyWnd, UINT uID, LPCTSTR pszTooltipText, HICON *phIcons, int nNumIcons, DWORD dwDelay, UINT nNotifyMessage)
- {
- //窗体显示是否正常(隐藏)
- m_bIsWindowNormal=true;
- //至少2 个图标
- ASSERT(nNumIcons>=2);
-
- ASSERT(dwDelay);
- //时间触发器窗体,它封装了产生任务栏动画的代码
- m_nCurrentIconIndex=0;
- m_phIcons=new HICON[nNumIcons];
- memcpy(m_phIcons,phIcons,nNumIcons*sizeof(HICON));
-
- m_nNumIcons=nNumIcons;
- m_dwDelay=dwDelay;
- BOOL bSuccess=CreateTray(pNotifyWnd,uID,pszTooltipText,
- phIcons[0],nNotifyMessage);
- m_bAnimated=TRUE;
- // if(m_nTimerID=SetTimer(0,400,NULL))
- // AfxMessageBox("Failed to Set Timer");
- // else AfxMessageBox("Set Timer");
- return bSuccess;
- }
- void CAnimateTray::RemoveIcon()
- {
- if(m_bCreated)
- {
- m_NotifyIconData.uFlags=0;
- Shell_NotifyIcon(NIM_DELETE,&m_NotifyIconData);
- m_bCreated=FALSE;
- }
- }
- void CAnimateTray::OnTimer(UINT nIDEvent)
- {
- // TODO: Add your message handler code here and/or call default
- if(nIDEvent==3)
- // AfxMessageBox("Timer is On");
- ++m_nCurrentIconIndex;
- m_nCurrentIconIndex=m_nCurrentIconIndex%m_nNumIcons;
- m_NotifyIconData.uFlags=NIF_ICON;
- m_NotifyIconData.hIcon=m_phIcons[m_nCurrentIconIndex];
- AfxGetMainWnd()->SetIcon(m_NotifyIconData.hIcon,FALSE);
- Shell_NotifyIcon(NIM_MODIFY,&m_NotifyIconData);
-
- CWnd::OnTimer(nIDEvent);
- }
- LRESULT CAnimateTray::OnTrayNotification(WPARAM wID, LPARAM lEvent)
- {
- if(wID!=m_NotifyIconData.uID)
- return 0L;
- CMenu menu;
- if(!menu.LoadMenu(wID))
- return 0;
- CMenu* pSubMenu=menu.GetSubMenu(0);
- if(!pSubMenu)
- return 0;
- //测试消息
- //可以在此添加其它的消息响应
- //该类仅仅封闭了WM_RBUTTONUP和WM_LBUTTONDBLCLK消息
- if(lEvent==WM_RBUTTONUP)
- {
- if(!m_bMenuOn)
- {
- ::SetMenuDefaultItem(pSubMenu->m_hMenu,0,TRUE);
- //显示并追踪鼠标信息
- CPoint pos;
- GetCursorPos(&pos);
- ::SetForegroundWindow(m_NotifyIconData.hWnd);
- ::TrackPopupMenu(pSubMenu->m_hMenu,0,pos.x,pos.y,0,m_NotifyIconData.hWnd,NULL);
- m_bMenuOn=TRUE;
- }
- else
- {
- CPoint pos;
- GetCursorPos(&pos);
- ::SetForegroundWindow(m_NotifyIconData.hWnd);
- ::TrackPopupMenu(NULL,0,pos.x,pos.y,0,m_NotifyIconData.hWnd,NULL);
- m_bMenuOn=FALSE;
- }
- }
- else if(lEvent==WM_LBUTTONDBLCLK)
- {
- if(!m_bIsWindowNormal)//将窗体激活
- ::SendMessage(m_NotifyIconData.hWnd,WM_COMMAND,pSubMenu->GetMenuItemID(0),0);
- else //将窗体隐藏
- ::SendMessage(m_NotifyIconData.hWnd,WM_COMMAND,pSubMenu->GetMenuItemID(1),0);
- }
- return 1;
- }
- int CAnimateTray::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CWnd::OnCreate(lpCreateStruct) == -1)
- return -1;
-
- // TODO: Add your specialized creation code here
- return 0;
- }
- void CAnimateTray::OnDestroy()
- {
- CWnd::OnDestroy();
-
- // TODO: Add your message handler code here
- KillTimer(m_nTimerID);
- }
- BOOL CAnimateTray::SetIcon(HICON hIcon)
- {
- if(!m_bCreated)
- return FALSE;
- m_bAnimated=FALSE;
- m_NotifyIconData.uFlags=NIF_ICON;
- m_NotifyIconData.hIcon=hIcon;
-
- return Shell_NotifyIcon(NIM_DELETE,&m_NotifyIconData);
- }
- BOOL CAnimateTray::SetIcon(UINT nIDResourec)
- {
- HICON hIcon=LoadIcon(NULL,MAKEINTRESOURCE(nIDResourec));
-
- return SetIcon(hIcon);
- }
- BOOL CAnimateTray::SetIcon(LPCTSTR lpIconName)
- {
- HICON hIcon=AfxGetApp()->LoadIcon(lpIconName);
-
- return SetIcon(hIcon);
- }
- BOOL CAnimateTray::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
- {
- // TODO: Add your specialized code here and/or call the base class
-
- return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
- }
- void CAnimateTray::OnTimer1(UINT nIDEvent)
- {
- OnTimer(nIDEvent);
- }