您的位置:首页 > 其它

显示动态gif图片

2014-04-15 15:57 387 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  gif显示