显示动态gif图片

DWORD WINAPI GifThread(LPVOID lpParam)
{
	//查找资源,加载资源到内存,锁定资源
	HRSRC hRsrc = FindResourceW(hInst, MAKEINTRESOURCEW(IDR_GIF), L"GIF");
	HGLOBAL hGlobal = LoadResource(hInst, hRsrc);
	LPVOID lpResData = LockResource(hGlobal);
	DWORD dwResSize = SizeofResource(hInst, hRsrc);

	//获得系统环境路径
	WCHAR szResPath[MAX_PATH] = L"\0";
	GetEnvironmentVariableW(L"TEMP", szResPath, sizeof(szResPath));
	wcscat(szResPath, L"\\hhhhgif.gif");

	//建立flash资源文件
	HANDLE hFile = CreateFileW(szResPath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, NULL, NULL);
	if (hFile != INVALID_HANDLE_VALUE)
	{
		DWORD dwWritten;
		WriteFile(hFile, lpResData, dwResSize, &dwWritten, NULL);
	}

	CloseHandle(hFile);
	FreeResource (hGlobal); //释放flash资源

	//显示gif
	HDC hdc = GetDC(g_hwnd);
	Image *image = new Image(szResPath);
	if (image == NULL)
	{
		return -1;
	}
	UINT count = 0;
	count = image->GetFrameDimensionsCount();
	GUID *pDimensionIDs = (GUID*)new GUID[count];
	image->GetFrameDimensionsList(pDimensionIDs, count);
	WCHAR strGuid[39];
	StringFromGUID2(pDimensionIDs[0], strGuid, 39);
	UINT frameCount = image->GetFrameCount(&pDimensionIDs[0]);
	delete []pDimensionIDs;

	int size = image->GetPropertyItemSize(PropertyTagFrameDelay);
	PropertyItem *pItem = NULL;
	pItem = (PropertyItem*)malloc(size);
	image->GetPropertyItem(PropertyTagFrameDelay,size,pItem);

	UINT fcount=0;
	GUID Guid = FrameDimensionTime;

	while (g_gif)
	{
		Graphics graphics(hdc);
		graphics.DrawImage(image, 50, 120, 400, 200);
		image->SelectActiveFrame(&Guid, fcount++);

		if (fcount == frameCount)
			fcount=0;

		long lPause = ((long*)pItem->value)[fcount]*10;
		Sleep(lPause);
	}
	ReleaseDC(g_hwnd, hdc);

	return 0;
}
#define TIMER_FIR 1
#define TIMER_SEC 2
//利用两个定时器,分别显示多帧的gif
BOOL CALLBACK GifDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{	
	PAINTSTRUCT ps;
	HDC hdc;
	HBRUSH hBrush, hOldbrush;
	RECT rect;

	static Image *image;
	static UINT count;
	static WCHAR strGuid[39];
	static UINT frameCount;
	static PropertyItem* pItem=NULL;
	static UINT fcount;
	static GUID Guid;
	static long lPause;
	static int size;
	static GUID *pDimensionIDs;

	//gdi+用到的两个变量
	GdiplusStartupInput m_gdiplusStartupInput;
	static ULONG_PTR m_pGdiToken;

	static WCHAR szResPath[MAX_PATH] = L"\0";

	switch (message)
	{
	case WM_INITDIALOG:
		{
			//装载gdi+
			GdiplusStartup(&m_pGdiToken, &m_gdiplusStartupInput, NULL);
			//查找资源,加载资源到内存,锁定资源
			HRSRC hRsrc = FindResourceW(hInst, MAKEINTRESOURCEW(IDR_GIF), L"GIF");
			HGLOBAL hGlobal = LoadResource(hInst, hRsrc);
			LPVOID lpResData = LockResource(hGlobal);
			DWORD dwResSize = SizeofResource(hInst, hRsrc);

			//获得系统环境路径
			GetEnvironmentVariableW(L"TEMP", szResPath, sizeof(szResPath));
			wcscat(szResPath, L"\\hhhhgif.gif");

			//建立flash资源文件
			HANDLE hFile = CreateFileW(szResPath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, NULL, NULL);
			if (hFile != INVALID_HANDLE_VALUE)
			{
				DWORD dwWritten;
				WriteFile(hFile, lpResData, dwResSize, &dwWritten, NULL);
			}
			CloseHandle(hFile);
			FreeResource (hGlobal); //释放flash资源

			SetTimer(hDlg,TIMER_FIR,0,NULL);
		}
		break;

	case WM_TIMER:
		{
			switch(wParam)
			{
			case TIMER_FIR:
				{
					hdc=GetDC(hDlg);
					image=new Image(szResPath);
					count=0;
					count=image->GetFrameDimensionsCount();
					pDimensionIDs=(GUID*)new GUID[count];
					image->GetFrameDimensionsList(pDimensionIDs,count);
					StringFromGUID2(pDimensionIDs[0],strGuid,39);
					frameCount=image->GetFrameCount(&pDimensionIDs[0]);
					delete []pDimensionIDs;
					size=image->GetPropertyItemSize(PropertyTagFrameDelay);
					//PropertyItem* pItem=NULL;
					// pItem=(PropertyItem*)malloc(size);
					pItem=(PropertyItem*)new PropertyItem[size];
					image->GetPropertyItem(PropertyTagFrameDelay,size,pItem);
					fcount=0;
					Guid=FrameDimensionTime;

					Graphics  graphics(hdc);
					graphics.DrawImage(image,0,0);
					image->SelectActiveFrame(&Guid,fcount++);
					if(fcount==frameCount)
						fcount=0;
					lPause=((long*)pItem->value)[fcount]*10;

					ReleaseDC(hDlg,hdc);
					KillTimer (hDlg, TIMER_FIR) ;
					SetTimer(hDlg,TIMER_SEC,lPause,NULL);
					InvalidateRect (hDlg, NULL, FALSE) ;
					break;
				}
			case TIMER_SEC:
				{
					image->SelectActiveFrame(&Guid,fcount++);
					if(fcount==frameCount)
						fcount=0;
					lPause=((long*)pItem->value)[fcount]*10;

					KillTimer(hDlg,TIMER_SEC);
					SetTimer(hDlg,TIMER_SEC,lPause,NULL);
					InvalidateRect (hDlg, NULL, FALSE) ;
				}
			}
		}
		break; 

	case WM_PAINT:
		{
			hdc = BeginPaint(hDlg, &ps);
			// TODO: 在此添加任意绘图代码...
			//hBrush = CreateSolidBrush(RGB(68,68,68));  //创建新画刷
			//GetClientRect(hDlg, &rect);    //获得主窗口的面积
			//hOldbrush = (HBRUSH)SelectObject(hdc, hBrush);  //把画刷选入设备
			//Rectangle(hdc, rect.left, rect.top, rect.right, rect.bottom);
			//FrameRect(hdc, &rect, hBrush);  //重画窗口边框
			//SelectObject(hdc, hOldbrush); //把旧画刷选入设备

			Graphics graphics(hdc);
			graphics.DrawImage(image,0,0);

			EndPaint(hDlg, &ps);	
		}
		break;

	}
	return FALSE;
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值