[转] WTL-Vista/Win7中内建的缓冲动画(buffered animation)

Windows Vista/Windows 7操作系统除了提供GDI双缓冲绘图内建支持外,也添加了几个API用于创建缓冲动画(buffered animation),用于在GDI程序中实现一些平滑渐变过渡的动画效果。这里有一篇文章介绍如何在Win32程序中使用这些API创建GDI动画效果:

Using the Windows Vista/Windows 7 Built-In Buffered Animation API

幸运的是,我们的WTL库也对这些新的API进行了封装,使得在WTL中应用这些API非常方便。WTL中的封装类是CBufferedAnimationImpl和CBufferedAnimationWindowImpl。

下面是一个使用CBufferedAnimationImpl创建的一个小程序,当用户按下空格键时,客户区的图片会自动切换,而且在切换时有非常平滑的“消隐渐变”(fade)的效果:

ScreenShot00146
程序主窗口的源代码:

const int PIC_COUNT = 4;
class CMainWindow :
	public CWindowImpl
   
   
    
    ,

   
   
	public CBufferedAnimationImpl
   
   

   
   
{
public:
	typedef CMainWindow _thisClass;
	typedef CBufferedAnimationImpl<_thisClass,int> _baseBufAnimationImpl;
	BEGIN_MSG_MAP(_thisClass)
		MSG_WM_KEYUP(OnKeyUp)
		MSG_WM_CREATE(OnCreate)
		MSG_WM_DESTROY(OnDestroy)
		CHAIN_MSG_MAP(_baseBufAnimationImpl)
	END_MSG_MAP()
	CMainWindow() : _baseBufAnimationImpl(0)
	{}
	int OnCreate(LPCREATESTRUCT /*lpCreateStruct*/)
	{
		for (int i=0;i
   
   
		{
			m_Pictures[i] = AtlLoadGdiplusImage(IDB_BITMAP1+i,_T("JPG"));
			ATLASSERT(!m_Pictures[i].IsNull());
		}
		//The default duration is 500ms
		SetDuration(400);
		CSize bmpSize;
		m_Pictures[0].GetSize(bmpSize);
		ResizeClient(bmpSize.cx,bmpSize.cy);
		CenterWindow();
		return 0;
	}
	void OnDestroy()
	{
		PostQuitMessage(0);
	}
	void DoPaint(CDCHandle dc, RECT& rect, int picIndex)
	{
		CRect rc(rect);
		dc.FillSolidRect(&rc,WHITE_COLOR);
		CSize bmpSize;
		m_Pictures[picIndex].GetSize(bmpSize);
		CDC dcImage;
		dcImage.CreateCompatibleDC(dc);
		HBITMAP hOldBitmap = dcImage.SelectBitmap(m_Pictures[picIndex]);
		dc.BitBlt(0,0,bmpSize.cx,bmpSize.cy,dcImage,0,0,SRCCOPY);
		dcImage.SelectBitmap(hOldBitmap);
	}
	void OnKeyUp(UINT nChar, UINT /*nRepCnt*/, UINT /*nFlags*/)
	{
		//Start animation if the user hit the space bar
		if (nChar == VK_SPACE)
			DoAnimation(GetNextPictureIndex());
	}
	int GetNextPictureIndex()
	{
		static int picIndex = 0;
		picIndex = (picIndex+1)%PIC_COUNT;
		return picIndex;
	}
private:
	CBitmap m_Pictures[PIC_COUNT];
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值