Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include "GIFAnimate.h"
CGIFAnimate::CGIFAnimate()
: m_pImage(NULL),
m_pDimensionID(NULL),
m_pItem(NULL),
m_CurrentFrame(1)
{
// Initialize GDI+.
GdiplusStartupInput gdiplusStartupInput;
GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
}
CGIFAnimate::~CGIFAnimate()
{
if(m_pItem != NULL)
free(m_pItem);
if(m_pDimensionID != NULL)
delete[] m_pDimensionID;
if(m_pImage != NULL)
delete m_pImage;
GdiplusShutdown(m_gdiplusToken);
}
void CGIFAnimate::PreCreate(CREATESTRUCT &cs)
{
// 添加自绘样式 CButton 是BS_OWNERDRAW
cs.style |= SS_OWNERDRAW;
}
LRESULT CGIFAnimate::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_TIMER: return OnTimer(uMsg, wParam, lParam);
}
return WndProcDefault(uMsg, wParam, lParam);
}
LRESULT CGIFAnimate::OnDrawItem(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(uMsg);
LPDRAWITEMSTRUCT pDrawItem = (LPDRAWITEMSTRUCT) lParam;
assert(m_pImage);
Graphics graphics(pDrawItem->hDC);
CRect rcClient = GetClientRect();
// 绘制当前帧
graphics.DrawImage(m_pImage, rcClient.left,rcClient.top,rcClient.Width(),
rcClient.Height());
return TRUE;
}
LRESULT CGIFAnimate::OnTimer(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
KillTimer((UINT_PTR)wParam);
m_pImage->SelectActiveFrame(&FrameDimensionTime, m_CurrentFrame);
SetTimer(1, getDelay(), NULL);
// 下一帧
++m_CurrentFrame;
assert(m_FrameCount);
m_CurrentFrame %= m_FrameCount;
Invalidate(FALSE);
return FinalWindowProc(uMsg, wParam, lParam);
}
LRESULT CGIFAnimate::OnMessageReflect(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (uMsg)
{
case WM_DRAWITEM: return OnDrawItem(uMsg, wParam, lParam);
}
return 0L;
}
void CGIFAnimate::Load(LPCWSTR sFileName)
{
m_pImage = new Image(sFileName);
// 获取维度数
UINT count = m_pImage->GetFrameDimensionsCount();
// 分配GUID数组用于存储维度
m_pDimensionID = new GUID[count];
m_pImage->GetFrameDimensionsList(m_pDimensionID, count);
// 获取该维度多少帧
m_FrameCount = m_pImage->GetFrameCount(&m_pDimensionID[0]);
// 获取指定属性的大小 (PropertyTagFrameDelay: 时间间隔)
UINT size = m_pImage->GetPropertyItemSize(PropertyTagFrameDelay);
// 分配空间, 获取内容
m_pItem = (PropertyItem *)malloc(size);
m_pImage->GetPropertyItem(PropertyTagFrameDelay, size, m_pItem);
}
void CGIFAnimate::Play()
{
m_CurrentFrame = 0;
m_pImage->SelectActiveFrame(&FrameDimensionTime, m_CurrentFrame);
SetTimer(1, getDelay(), NULL);
// 下一帧
++m_CurrentFrame;
Invalidate(FALSE);
}
void CGIFAnimate::Stop() {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.