MFC GDI+显示GIF文件

在头文件里面添加
Image* image;
	GUID Guid ;
	UINT frameCount;
	UINT framePos;


界面类的构造函数里面添加

image = NULL;
	frameCount = 0;
	framePos = 0;

加载GIF文件

void CFormatDlg::LoadGif()
{
	WCHAR strModule[MAX_PATH * 2] = {0};
	GetModuleFileNameW(NULL, strModule, MAX_PATH * 2);
	::PathRemoveFileSpecW(strModule);
	wsprintfW(strModule + wcslen(strModule), L"\\%s.gif", L"wait");

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


定时器播放。SetTimer(0, 50, NULL);

void CGIFDlg::OnTimer(UINT_PTR nIDEvent)
{
	if (image){
		Graphics gh(m_hWnd);
		gh.DrawImage(image, 0, 0, image->GetWidth(), image->GetHeight());

		//设置当前需要显示的帧数
		image->SelectActiveFrame(&Guid, framePos);
		framePos++;
		if (framePos == frameCount){
			framePos = 0;
		}
	}

	CDialogEx::OnTimer(nIDEvent);
}

VS2012工程地址:http://download.csdn.net/detail/sz76211822/9532053



在此说明一下。不要使用网上的CPictureEx 类。为什么呢。查看一段调用的代码:

if (m_GifPic.Load(MAKEINTRESOURCE(IDR_FORMAT), _T("Gif")))	{
			m_GifPic.ShowWindow(SW_SHOW);
			m_GifPic.Draw();
		}
这样是没有问题的。正确调用了CPictureEx 的成员函数。但是在XP系统下是有问题的。看看 Load成员函数的代码:

BOOL CPictureEx::Load(LPCTSTR szResourceName, LPCTSTR szResourceType)
{
 ASSERT(szResourceName);
 ASSERT(szResourceType);

 HRSRC hPicture = FindResource(AfxGetResourceHandle(),szResourceName,szResourceType);
 HGLOBAL hResData;
 if (!hPicture || !(hResData = LoadResource(AfxGetResourceHandle(),hPicture)))
 {
  TRACE(_T("Load (resource): Error loading resource %s\n"),szResourceName);
  return FALSE;
 };
 DWORD dwSize = SizeofResource(AfxGetResourceHandle(),hPicture);

 // hResData is not the real HGLOBAL (we can't lock it)
 // let's make it real

 HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD,dwSize);
 if (!hGlobal)
 {
  TRACE(_T("Load (resource): Error allocating memory\n"));
  FreeResource(hResData);
  return FALSE;
 };
 
 char *pDest = reinterpret_cast (GlobalLock(hGlobal));
 char *pSrc = reinterpret_cast (LockResource(hResData));
 if (!pSrc || !pDest)
 {
  TRACE(_T("Load (resource): Error locking memory\n"));
  GlobalFree(hGlobal);
  FreeResource(hResData);
  return FALSE;
 };
 CopyMemory(pDest,pSrc,dwSize);
 FreeResource(hResData);
 GlobalUnlock(hGlobal);

 BOOL bRetValue = Load(hGlobal,dwSize);
 GlobalFree(hGlobal);
 return bRetValue;
}
经过打断点,在xp系统中运行到CopyMemory总是莫名其妙的停止了,然后程序退出~~。


你可能感兴趣的:(基础)