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

来源:互联网 发布:斐讯路由器淘宝下架 编辑:程序博客网 时间:2024/06/05 03:37

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];
};