⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 animatetrayicon.cpp

📁 一些关于C++开发的多媒体制作书籍的源代码
💻 CPP
字号:
/*
* Copyright (c) 2002, Bcdliang
* All rights reserved.
*
* 文件名称:AnimateTrayIcon.cpp
* 摘    要:类CAnimateTrayIcon的实现
*
* 当前版本:1.01
* 作    者:LIANG Zheng
* 完成日期:2002年8月11日
*/

#include "stdafx.h"
#include "AnimateTrayIcon.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

CAnimateTrayIcon *gpAnimateTrayIcon = NULL;

void CALLBACK EXPORT TimerProc(
   HWND hWnd,      // handle of CWnd that called SetTimer
   UINT nMsg,      // WM_TIMER
   UINT nIDEvent,   // timer identification
   DWORD dwTime    // system time
);

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////
/*
* Function   : CAnimateTrayIcon
* Description: Constructor. Initialize all the data members, and add an icon to the tray bar.
* Input args : None
* Output args: None
* Return     : None
*/
CAnimateTrayIcon::CAnimateTrayIcon(
		CWnd *pWnd,					// Window asossiated with the tray icon
		UINT uCallbackMessage,		// Callback msg ID used by the window
		UINT nResourceID,			// Resource ID of the images list
		UINT uImageWidth,			// Width(&height) of each image in the image list
		COLORREF crTransparent		// Color to be made transparent 
		)
{
	m_pWnd = pWnd;
//	m_uImages = uImages;
//	VERIFY(m_imgList.Create(16, 16, ILC_COLOR | ILC_COLOR8, 1, 1));
	VERIFY(m_imgList.Create(nResourceID, uImageWidth, 1, crTransparent));
	m_imgList.SetBkColor(CLR_NONE);
	m_uImages = m_imgList.GetImageCount();
	ASSERT(m_uImages > 0);
	m_nid.cbSize = sizeof(NOTIFYICONDATA);
	m_nid.hIcon = m_imgList.ExtractIcon(0);
	m_nid.hWnd = m_pWnd->GetSafeHwnd();
	m_nid.szTip[0] = '\0';
	m_nid.uCallbackMessage = uCallbackMessage;
	m_nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
	m_nid.uID = -1;

	m_uUpdateInterval = 500;
	m_bTrayAnimate = false;
	m_bWindowAnimate = false;
	m_bAlreadyInTray = false;
	m_uCurrentImage = 0;
	if (!AddToTray(_T("")))
	{
		TRACE("AddToTray fail!\n");
	}
	::gpAnimateTrayIcon = this;

	if (m_uImages > 1)
	{
		EnableTrayAnimate(true);
		EnableWindowAnimate(true);
	}

}

////////////////////////////////////////////////////////////////////////
/*
* Function   : ~CAnimateTrayIcon
* Description: Deconstructor. Delete the icon from the tray bar(if installed), and destroy the image list.
* Input args : None
* Output args: None
* Return     : None
*/
CAnimateTrayIcon::~CAnimateTrayIcon()
{
	DeleteFromTray();
	m_imgList.DeleteImageList();
}

////////////////////////////////////////////////////////////////////////
/*
* Function   : AddToTray
* Description: Add an icon to the tray bar(if not installed), and lauch the animate function.
* Input args : const CString &strTip, tool tip for tray icon
* Output args: None
* Return     : BOOL, true-success, false-failure
*/
BOOL CAnimateTrayIcon::AddToTray(const CString &strTip)
{
	if (m_bAlreadyInTray)
	{
		return false;
	}

//	if (m_hIcon != NULL)
//	{
//		DestroyIcon(m_hIcon);
//	}
//	if (m_nid.hIcon != NULL)
//	{
//		DestroyIcon(m_nid.hIcon);
//	}

	m_hIcon = m_imgList.ExtractIcon(m_uCurrentImage);
	lstrcpyn(m_nid.szTip, (LPCTSTR)strTip, sizeof(m_nid.szTip));
	m_nid.hIcon = m_hIcon;
	m_nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
	if (! Shell_NotifyIcon(NIM_ADD, &m_nid))
	{
		return false;
	}
	DestroyIcon(m_pWnd->SetIcon(m_hIcon, false));
	m_bAlreadyInTray = true;
	if (m_bTrayAnimate || m_bWindowAnimate)
	{
		m_pWnd->SetTimer((UINT)this, m_uUpdateInterval, TimerProc);
/*		guTimerID = ::timeSetEvent(m_uUpdateInterval, 50, TimerProc, 0, TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
		if (guTimerID == NULL)
		{
			return false;
		}
*/	}
	return true;
}

////////////////////////////////////////////////////////////////////////
/*
* Function   : DeleteFromTray
* Description: Delete the icon from the tray bar(if installed), and stop the animate function.
* Input args : None
* Output args: None
* Return     : BOOL, true-success, false-failure
*/
BOOL CAnimateTrayIcon::DeleteFromTray()
{
	if (!m_bAlreadyInTray)
	{
		return false;
	}

//	if (m_nid.hIcon != NULL)
//	{
//		DestroyIcon(m_nid.hIcon);
//	}
	m_nid.hIcon = NULL;
	m_nid.szTip[0] = '\0';
	m_nid.uFlags = NIF_MESSAGE;
	if (!Shell_NotifyIcon(NIM_DELETE, &m_nid))
	{
		return false;
	}
	m_bAlreadyInTray = false;

	if (!m_bWindowAnimate)
	{
		m_pWnd->KillTimer((UINT)this);
	}
	return true;
}

////////////////////////////////////////////////////////////////////////
/*
* Function   : ShowNextIcon
* Description: Change the tray icon/window icon to the next icon in the image list.
* Input args : None
* Output args: None
* Return     : BOOL, true-success, false-failure
*/
BOOL CAnimateTrayIcon::ShowNextIcon() 
{
//	if (m_hIcon != NULL)
//	{
//		DestroyIcon(m_hIcon);
//	}
	m_uCurrentImage = (m_uCurrentImage + 1) % m_uImages;
	m_hIcon = m_imgList.ExtractIcon(m_uCurrentImage);

	if (m_bAlreadyInTray && m_bTrayAnimate)
	{
		m_nid.hIcon = m_hIcon;
		m_nid.uFlags = NIF_MESSAGE | NIF_ICON;
		Shell_NotifyIcon(NIM_MODIFY, &m_nid);
	}

	if (m_bWindowAnimate)
	{
		DestroyIcon(m_pWnd->SetIcon(m_hIcon, false));
	}
	return true;
}

////////////////////////////////////////////////////////////////////////
/*
* Function   : ModifyTip
* Description: Modify the tray icon's tip
* Input args : const CString &strTip, tool tip for tray icon
* Output args: None
* Return     : BOOL, true-success, false-failure
*/
BOOL CAnimateTrayIcon::ModifyTip(const CString &strTip)
{
	if (!m_bAlreadyInTray)
	{
		return false;
	}

	lstrcpyn(m_nid.szTip, (LPCTSTR)strTip, sizeof(m_nid.szTip));
	m_nid.uFlags = NIF_MESSAGE | NIF_TIP;
	return Shell_NotifyIcon(NIM_MODIFY, &m_nid);
}

////////////////////////////////////////////////////////////////////////
/*
* Function   : EnableTrayAnimate
* Description: Enable/disable the tray icon animation
* Input args : BOOL bTrayAnimate, true-enable, false-disable
* Output args: None
* Return     : BOOL, true-success, false-failure
*/
BOOL CAnimateTrayIcon::EnableTrayAnimate(BOOL bTrayAnimate)
{
	if (!m_bAlreadyInTray)
	{
		return false;
	}
	m_bTrayAnimate = bTrayAnimate;
	if (m_bTrayAnimate)
	{
		m_pWnd->SetTimer((UINT)this, m_uUpdateInterval, TimerProc);
	}
	else
	{
		if (!m_bWindowAnimate)
		{
			m_pWnd->KillTimer((UINT)this);
		}
	}
	return true;
}

////////////////////////////////////////////////////////////////////////
/*
* Function   : EnableWindowAnimate
* Description: Enable/disable the window icon animation
* Input args : BOOL bWindowAnimate, true-enable, false-disable
* Output args: None
* Return     : BOOL, true-success, false-failure
*/
BOOL CAnimateTrayIcon::EnableWindowAnimate(BOOL bWindowAnimate)
{
	m_bWindowAnimate = bWindowAnimate;
	if (m_bWindowAnimate)
	{
		m_pWnd->SetTimer((UINT)this, m_uUpdateInterval, TimerProc);
	}
	else
	{
		if (!m_bTrayAnimate)
		{
			m_pWnd->KillTimer((UINT)this);
		}
	}
	return true;
}

////////////////////////////////////////////////////////////////////////
/*
* Function   : SetUpdateInterval
* Description: Set the update interval of animate icon.
* Input args : UINT uUpdateInterval, 50ms ~ 2000ms
* Output args: None
* Return     : None
*/
void CAnimateTrayIcon::SetUpdateInterval(UINT uUpdateInterval)
{
	ASSERT(uUpdateInterval >= 50 && uUpdateInterval<=2000);
	m_uUpdateInterval = uUpdateInterval;
	if (m_bTrayAnimate || m_bWindowAnimate)
	{
		m_pWnd->SetTimer((UINT)this, m_uUpdateInterval, TimerProc);
	}
}

////////////////////////////////////////////////////////////////////////
/*
* Function   : GetUpdateInterval
* Description: Get the update interval of animate icon.
* Input args : None
* Output args: None
* Return     : UINT, the update interval of animate icon.
*/
UINT CAnimateTrayIcon::GetUpdateInterval() const
{
	return m_uUpdateInterval;
}

////////////////////////////////////////////////////////////////////////
/*
* Function   : TimerProc
* Description: Callback function of WM_TIMER msg
* Input args : 
   HWND hWnd,      // handle of CWnd that called SetTimer
   UINT nMsg,      // WM_TIMER
   UINT nIDEvent,  // timer identification
   DWORD dwTime    // system time
* Output args: None
* Return     : None
*/
void CALLBACK EXPORT TimerProc(
   HWND hWnd,      // handle of CWnd that called SetTimer
   UINT nMsg,      // WM_TIMER
   UINT nIDEvent,  // timer identification
   DWORD dwTime    // system time
)
{
	::gpAnimateTrayIcon->ShowNextIcon();
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -