MFC 对话框显示gif图片 转
在头文件里面添加:
Image* image;
GUID Guid ;
UINT frameCount;
UINT framePos; ULONG_PTR gdiplusToken;
afx_msg void OnTimer(UINT_PTR nIDEvent); void LoadGif();
需要依赖头文件:
#include <Gdiplusheaders.h>
#include <Gdiplus.h>
using namespace Gdiplus;
界面类的构造函数里面添加:
//启动gdi 要不然 Image::FromFile 会失败
GdiplusStartupInput gdiplusStartupInput;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
image = NULL;
frameCount = 0;
framePos = 0;
添加定时器消息处理:
BEGIN_MESSAGE_MAP(CMFCApplication1Dlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, &CMFCApplication1Dlg::OnBnClickedButton1)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_BUTTON2, &CMFCApplication1Dlg::OnBnClickedButton2)
END_MESSAGE_MAP()
加载GIF文件:
void CMFCApplication1Dlg::LoadGif()
{
WCHAR strModule[MAX_PATH * 2] = { 0 };
GetModuleFileNameW(NULL, strModule, MAX_PATH * 2);
::PathRemoveFileSpecW(strModule);
wsprintfW(strModule + wcslen(strModule), L"\\%s.gif", L"sVcodec");
image = Image::FromFile(strModule);
//获得有多少个维度,对于gif就一个维度
UINT count = image->GetFrameDimensionsCount();
GUID *pDimensionIDs = (GUID*)new GUID[count];
image->GetFrameDimensionsList(pDimensionIDs, count);
WCHAR strGuid[39];
StringFromGUID2(pDimensionIDs[0], strGuid, 39);
frameCount = image->GetFrameCount(&pDimensionIDs[0]);
//delete[] pDimensionIDs;
//获得各帧之间的时间间隔
//先获得有多少个时间间隔,PropertyTagFrameDelay是GDI+中预定义的一个GIG属性ID值,表示标签帧数据的延迟时间
UINT FrameDelayNums = image->GetPropertyItemSize(PropertyTagFrameDelay);
PropertyItem* lpPropertyItem = new PropertyItem[FrameDelayNums];
image->GetPropertyItem(PropertyTagFrameDelay, FrameDelayNums, lpPropertyItem);
Guid = FrameDimensionTime;
image->SelectActiveFrame(&Guid, framePos);
}
定时器处理:
void CMFCApplication1Dlg::OnTimer(UINT_PTR nIDEvent)
{
if (image)
{
CWnd* pic = GetDlgItem(IDC_STATIC_PIC);
Graphics gh(pic->GetDC()->m_hDC);
CRect rect;
GetDlgItem(IDC_STATIC_PIC)->GetClientRect(&rect);
gh.DrawImage(image, 0, 0, rect.Width(), rect.Height());
//gh.DrawImage(image, 0, 0, image->GetWidth(), image->GetHeight());
//设置当前需要显示的帧数
image->SelectActiveFrame(&Guid, framePos);
framePos++;
if (framePos == frameCount)
{
framePos = 0;
}
}
CDialogEx::OnTimer(nIDEvent);
}
启动定时器:
SetTimer(0, 400, NULL);
LoadGif();
需要依赖gdi 库:
gdiplus.lib
参考地址:https://blog.csdn.net/sz76211822/article/details/51507616