Gif.cpp
上传用户:hyb6888
上传日期:2016-01-24
资源大小:5186k
文件大小:20k
- // Gif.cpp: implementation of the CGif class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "GIF.h"
- #include "stdio.h"
- static UINT GifThread(LPVOID p_Gif,LPVOID hwnd);
- BMPIMAGE::BMPIMAGE()
- {
- pImageData = NULL;
- }
- BMPIMAGE::~BMPIMAGE()
- {
- if (pImageData)
- GlobalFree(pImageData);
- }
- ////////////////////////////////////////////////////////
- // myCPtrList class
- ////////////////////////////////////////////////////////
- myCPtrList::myCPtrList()
- {
- m_pNodeTail=&m_pNodeHead;
- m_pNodeFree=NULL;
- m_nCount=-1;
- }
- myCPtrList::~myCPtrList()
- {
- }
- myPOSITION myCPtrList::AddTail(void* newElement)//把一个图像加入集合中
- {
- BMPIMAGE * pBmpImage;
- CNode *pnd;
- pBmpImage=(BMPIMAGE*) newElement;
- pnd=new CNode;
- pnd->data=pBmpImage;
- m_pNodeTail->pNext=pnd;
- pnd->pPrev=m_pNodeTail;
- m_pNodeTail=pnd;
- m_nCount++;
- return 0;
- }
- //只释放节点,而数据由外部释放
- void myCPtrList::RemoveTail() // 删除元素
- {
- if((m_pNodeTail != &m_pNodeHead) && (m_nCount > -1))
- {
- m_pNodeTail=m_pNodeTail->pPrev;
- m_pNodeTail->pNext->data;
- delete m_pNodeTail->pNext;
- m_nCount--;
- }
- }
- void*myCPtrList::GetTail()//得到最后一个元素
- {
- void* pp=NULL;
- if(m_pNodeTail!=NULL)
- pp=m_pNodeTail->data;
- return pp;
- }
- myPOSITION myCPtrList::FindIndex(int nIndex) //得到nIndex指定的元素,可能为空
- {
- CNode *pp=NULL;
- int i;
- pp=m_pNodeHead.pNext;
- if((nIndex > -1) && (nIndex < m_nCount))
- {
- for(i=0;i<=nIndex;i++)
- pp=pp->pNext;
- }
- else
- pp=NULL;
- return (myPOSITION)pp;
- }
- void*myCPtrList::GetAt(myPOSITION position)//得到指定位置的一个元素
- {
- CNode *pp=NULL;
- pp=(CNode *)position;
- return pp->data;
- }
- bool myCPtrList:: IsEmpty ()
- {
- return m_nCount <0;
- }
- //
- CGIF::CGIF()
- {
- hMapFile = NULL;
- lpMapFileBase = NULL;
- }
- CGIF::~CGIF()
- {
- BMPIMAGE * pBmpImage;
- while (!m_ImageList.IsEmpty ())
- {
- pBmpImage = (BMPIMAGE *) m_ImageList.GetTail ();
- m_ImageList.RemoveTail ();
- delete pBmpImage;
- }
- if(lpMapFileBase!=NULL)
- UnmapViewOfFile(lpMapFileBase);
- if(hMapFile!=NULL)
- CloseHandle(hMapFile);
- }
- BOOL CGIF::LoadGIF(LPCTSTR GifFileName)
- {
- //pFileContent;
- int ret=0 ;
- HANDLE hfile;
- LPUSTR pFileContent;
- if(strlen(GifFileName)==0)
- return 1;
- if(lpMapFileBase!=NULL)
- UnmapViewOfFile(lpMapFileBase);
- if(hMapFile!=NULL)
- CloseHandle(hMapFile);
- {
- hfile=CreateFile(GifFileName,
- GENERIC_READ,FILE_SHARE_READ ,NULL,
- OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
- if(hfile==INVALID_HANDLE_VALUE)
- {
- //MessageBox(0,"文件没有打开",0,0);
- return 1;
- }
- ret=1;
- if ( (hMapFile=CreateFileMapping(hfile,NULL,PAGE_READONLY,0,0,"GIFte1")) == NULL)
- {
- //MessageBox(0,"文件句柄的误",0,0);
- CloseHandle(hfile);
- return 1;
- }
- if ( (lpMapFileBase = (LPVOID) MapViewOfFile( hMapFile, FILE_MAP_READ,0,0,0)) == NULL)
- {
- //MessageBox(0,"文件可能为空",0,0);
- CloseHandle(hMapFile);
- CloseHandle(hfile);
- return 1;
- }
- CloseHandle(hfile);
- pFileContent=(LPUSTR)lpMapFileBase;
- }
- //Clear
- BMPIMAGE * pBmpImage;
- while (!m_ImageList.IsEmpty ())
- {
- pBmpImage = (BMPIMAGE *) m_ImageList.GetTail ();
- m_ImageList.RemoveTail ();
- delete pBmpImage;
- }
- UINT HeaderSize = AnalizeFileHeader(pFileContent);
- m_pDataArea = pFileContent + HeaderSize;
- /*{
- char ss[1000];
- sprintf(ss,"%d",pFileContent);
- MessageBox(0,ss,"not 781",0);
- }*/
- m_CurTColorIndex = -1;
- m_CurDelayTime = 0;
-
- LPUSTR p = m_pDataArea;
- UINT Offset;
- while (1)
- {
- if (p[0] == 0x21 && p[1] == 0xf9 && p[2] == 0x04)
- {
- Offset = GetGrphContent(p);
- p += Offset;
- }
- else if (p[0] == 0x21 && p[1] == 0x01 && p[2] == 0x0c)
- {
- Offset = ShowText(p);
- p += Offset;
- }
- else if (p[0] == 0x21 && p[1] == 0xff && p[2] == 0x0b)
- {
- Offset = GetAppContent(p);
- p += Offset;
- }
- else if (p[0] == 0x21 && p[1] == 0xfe)
- {
- Offset = GetNoteContent(p);
- p += Offset;
- }
- else if (p[0] == 0x2c)
- {
- Offset = GetImage(p);
- p += Offset;
- }
- else break;
- }
- GlobalFree(pFileContent);
- return 0;
- }
- UINT CGIF::AnalizeFileHeader(LPUSTR pFileContent)
- {
- // GIF File Header
- GIFHEADER gifHeader;//
- int sz;
- sz=sizeof(GIFINFO);
- sz=sizeof(GIFHEADER);
- memcpy(&gifHeader,pFileContent,sz);
- m_GifInfo.Width = gifHeader.ScreenWidth ;
- m_GifInfo.Height = gifHeader.ScreenDepth ;
- m_GifInfo.ColorMode = (gifHeader.GlobalFlagByte & 0x07) + 1;//肅︹家Α 1,2,3,4,5,6,7,8
- m_GifInfo.InitPixelBits = m_GifInfo.ColorMode + 1;
- m_GifInfo.ColorType = 1;
- switch (m_GifInfo.ColorMode)
- {
- case 1:
- m_GifInfo.ColorType = m_GifInfo.ColorType << 1;
- break;
- case 2:
- m_GifInfo.ColorType = m_GifInfo.ColorType << 2;
- break;
- case 3:
- m_GifInfo.ColorType = m_GifInfo.ColorType << 3;
- break;
- case 4:
- m_GifInfo.ColorType = m_GifInfo.ColorType << 4;
- break;
- case 5:
- m_GifInfo.ColorType = m_GifInfo.ColorType << 5;
- break;
- case 6:
- m_GifInfo.ColorType = m_GifInfo.ColorType << 6;
- break;
- case 7:
- m_GifInfo.ColorType = m_GifInfo.ColorType << 7;
- break;
- case 8:
- m_GifInfo.ColorType = m_GifInfo.ColorType << 8;
- break;
- }
- //end
- if (gifHeader.GlobalFlagByte & 0x80)
- {
- //read pal data and save it to BitMap pal
- LPUSTR p = pFileContent + sz;
- MYRGB * pRGB = (MYRGB *)p;
- for (UINT j = 0; j < m_GifInfo.ColorType ; j ++)
- {
- m_CurRgbQuad[j].rgbRed = pRGB->bRed;
- m_CurRgbQuad[j].rgbGreen = pRGB->bGreen;
- m_CurRgbQuad[j].rgbBlue = pRGB->bBlue;
- m_CurRgbQuad[j].rgbReserved = 0;
- pRGB++;
- }
- p += 3 * m_GifInfo.ColorType;//pass pal
- return sz+ 3 * m_GifInfo.ColorType;
- }
-
- return sz;
- }
- UINT CGIF::GetGrphContent(LPUSTR pGrCtrl)
- {
- GRAPHCTRL GraphCtrl;
- memcpy(&GraphCtrl,pGrCtrl,sizeof(GRAPHCTRL));
- USHORT Flag = GraphCtrl.PackedField;
- m_CurTColorIndex = -1;
- m_CurDelayTime = 0;
-
- if (Flag & 1)
- m_CurTColorIndex = GraphCtrl.TranColorIndex ;
- if (Flag & 2)
- m_CurDelayTime = GraphCtrl.DelayTime ;
- return sizeof(GRAPHCTRL);
- }
- UINT CGIF::ShowText(LPUSTR pText)
- {
- TEXTCTRL TextCtrl;
- memcpy(&TextCtrl,pText,sizeof(TEXTCTRL));
- return sizeof(TEXTCTRL)+TextCtrl.Data [0] - 256 + 1;
- }
- UINT CGIF::GetAppContent(LPUSTR pApp)
- {
- APPCTRL AppCtrl;
- memcpy(&AppCtrl,pApp,sizeof(APPCTRL));
- return sizeof(APPCTRL)+AppCtrl.Data [0] - 256 + 1;
- }
- UINT CGIF::GetNoteContent(LPUSTR pNote)
- {
- NOTEHCTRL NoteCtrl;
- memcpy(&NoteCtrl,pNote,sizeof(NOTEHCTRL));
- return sizeof(NOTEHCTRL)+NoteCtrl.Data[0] - 256 + 1;
- }
- UINT CGIF::GetImage(LPUSTR pData)
- {
- IMAGEDATAINFO ImageData;
- memcpy(&ImageData,pData,sizeof(IMAGEDATAINFO));
- LPUSTR p = pData + sizeof(IMAGEDATAINFO);
- UINT PalSize = 0;
- m_CurSaveMode = 0;
- m_CurX = ImageData.ImageLeft;
- m_CurY = ImageData.ImageTop ;
- m_CurWidth = ImageData.ImageWidth ;
- m_CurHeight = ImageData.ImageHeight;
- if (ImageData.LocalFlagByte & 0x80)
- {
- MYRGB * pRGB = (MYRGB *)p;
- for (UINT j = 0; j < m_GifInfo.ColorType ; j ++)
- {
- m_CurRgbQuad[j].rgbRed = pRGB->bRed;
- m_CurRgbQuad[j].rgbGreen = pRGB->bGreen;
- m_CurRgbQuad[j].rgbBlue = pRGB->bBlue;
- m_CurRgbQuad[j].rgbReserved = 0;
- pRGB++;
- }
- p += 3 * m_GifInfo.ColorType;//pass pal
- PalSize = 3 * m_GifInfo.ColorType;
- }
- else if (ImageData.LocalFlagByte & 0x40)
- {
- m_CurSaveMode = 1;
- }
- UINT InitBits = *p++;
-
- UINT offset;
- UINT dataCount = GetCodeCountOnChar(p,offset);
- LPUSTR pCodeData = GetCodeDataOnChar(p);
- LPUSTR pTrueCodeData = GetCodeDataOnBits (pCodeData,m_GifInfo.InitPixelBits,dataCount);
- ConvertToBmpImage(pTrueCodeData);
- GlobalFree(pCodeData);
- GlobalFree(pTrueCodeData);
- return sizeof(IMAGEDATAINFO) + offset + PalSize;
- }
- USHORT CGIF::GetOneCode(LPUSTR CodeStr ,UINT CodeStrLen, UINT OffsetChar , UINT OffsetBits, UINT Length)
- {
-
- UCHAR *pValue,*lp = (UCHAR*)CodeStr;
-
- UINT value = 0;
- pValue = (UCHAR *)&value;
- pValue[0] = lp[OffsetChar+0];
- if (OffsetChar + 1 < CodeStrLen)
- pValue[1] = lp[OffsetChar+1];
- if (OffsetChar + 2 < CodeStrLen)
- pValue[2] = lp[OffsetChar+2];
- if (OffsetChar + 3 < CodeStrLen)
- pValue[3] = lp[OffsetChar+3];
- value = value >> OffsetBits;
- value = value << (32 - Length);
- value = value >> (32 - Length);
- return (USHORT)value;
- }
- UINT CGIF::GetCodeCountOnChar (LPUSTR CodeDataStr,UINT &AllDataLen)
- {
- UINT dataCount = 0;
- UCHAR dataLen;
- LPUSTR p1 = CodeDataStr;
- AllDataLen = 0;
- while (1)
- {
- dataLen = *p1 ++;
- p1 += dataLen;
- dataCount += dataLen;
- AllDataLen += (dataLen + 1);
- if (dataLen == 0x00) break;
- }
- AllDataLen ++;
- return dataCount;
- }
- LPUSTR CGIF::GetCodeDataOnChar (LPUSTR CodeDataStr)
- {
- UINT offset;
- UINT dataCount = GetCodeCountOnChar(CodeDataStr,offset);
- LPUSTR pData,p2;
- pData = (LPUSTR)GlobalAlloc (GPTR,dataCount+1);
- p2 = pData;
- UCHAR dataLen;
- LPUSTR p1 = CodeDataStr;
- while (1)
- {
- dataLen = *p1 ++;
- if (dataLen == 0x00)
- break;
- memcpy(p2,p1,dataLen);
- p1 += dataLen;
- p2 += dataLen;
- dataCount += dataLen;
- }
- return pData;
- }
- LPUSTR CGIF::GetCodeDataOnBits (LPUSTR CodeDataStr ,UINT InitLen ,UINT &CodeDataLen)
- {
- LPUSTR p = CodeDataStr;
- LPUSTR pData = (LPUSTR ) GlobalAlloc (GPTR ,m_CurWidth*m_CurHeight*2);
- LPUSTR pTrueData = pData;
-
- int i = 0;
- UINT BitsCount = 0;
- USHORT TCode,s = m_GifInfo.ColorType + 2;//
- UINT TableLen,iLen = InitLen;
- UINT Pos,index,j = 0;
- UINT SrcCodeLEN = CodeDataLen;
- while (1)
- {
- TCode = GetOneCode(p ,CodeDataLen,BitsCount / 8, BitsCount % 8, iLen);
- BitsCount += iLen;
- if (TCode < m_GifInfo.ColorType)
- {
- LZWTable[j].Header = TCode;
- LZWTable[j].Code = s ++;
- if (j != 0)
- {
- LZWTable[j - 1].Tail = TCode;
- }
- j++;
- }
- else if (TCode > m_GifInfo.ColorType + 1)
- {
- if (j == 0) continue;
- LZWTable[j].Header = TCode;
- LZWTable[j].Code = s ++;
- index = j - 1;
- Pos = j;
- while (1)
- {
- Pos = LZWTable[Pos].Header - (m_GifInfo.ColorType + 2) ;
- if (LZWTable[Pos].Header < m_GifInfo.ColorType)
- break;
- }
- LZWTable[index].Tail = LZWTable[Pos].Header;
- j++;
- }
- else if (TCode == m_GifInfo.ColorType)
- {
- iLen = InitLen;
- TableLen = j;
- j = 0;
- GetPartImageDataFromTable(pData,LZWTable,TableLen);
- //pData+=TableLen;
- s = m_GifInfo.ColorType + 2 ;
- }
- else if (TCode == m_GifInfo.ColorType + 1)
- {
- //end build table
- iLen = InitLen;
- TableLen = j;
- GetPartImageDataFromTable(pData,LZWTable,TableLen);
- //pData+=TableLen;
- s = m_GifInfo.ColorType + 2;
- break;
- }
- if (s == 3)
- iLen = 2;
- else if (s == 5)
- iLen = 3;
- else if (s == 9)
- iLen = 4;
- else if (s == 17)
- iLen = 5;
- else if (s == 33)
- iLen = 6;
- else if (s == 65)
- iLen = 7;
- else if (s == 129)
- iLen = 8;
- else if (s == 257)
- iLen = 9;
- else if ( s == 513 )
- iLen = 10;
- else if (s == 1025 )
- iLen = 11;
- else if (s == 2049 )
- iLen = 12;
-
- if (BitsCount / 8 > SrcCodeLEN)
- {
- iLen = InitLen;
- TableLen = j;
- GetPartImageDataFromTable(pData,LZWTable,TableLen);
- s = m_GifInfo.ColorType + 2;
- break;
- }
-
- }
- CodeDataLen = BitsCount / 8;
-
- return pTrueData;
- }
- void CGIF::GetPartImageDataFromTable(LPUSTR &pImage,LZWTABLE * Table,UINT TableLen)
- {
- UINT i;
- LPUSTR p = pImage;
- USHORT *TailStack = new USHORT[5200];
- int j;
- int sp;
- for (i = 0 ; i < TableLen; i++)
- {
- if (Table[i].Header < m_GifInfo.ColorType)
- {
- *p++ = (UCHAR)Table[i].Header ;
- }
- else
- {
- sp = 0;
- j = Table[i].Header - (m_GifInfo.ColorType + 2);
- while (Table[j].Header > m_GifInfo.ColorType + 1)
- {
- TailStack[sp++] = Table[j].Tail ;
- j = Table[j].Header;
- j -= (m_GifInfo.ColorType + 2);
- }
- *p++ = (UCHAR)Table[j].Header;
- *p++ = (UCHAR)Table[j].Tail;
- sp --;
- while (sp >= 0)
- {
- *p++ = (UCHAR)TailStack[sp--];
- }
- }
- }
- int len = p - pImage;
- pImage = p;
- delete []TailStack;
- }
- BOOL CGIF::ConvertToBmpImage(LPUSTR SrcData)
- {
- LPUSTR pSrcData = SrcData;
-
- UINT i,j,k,Step;
- if (m_CurSaveMode )
- {
- LPUSTR p2 = (LPUSTR)GlobalAlloc(GPTR,m_CurWidth*m_CurHeight);
- LPUSTR pTmp = p2;
-
- k = 0; Step = 8;
- for (j = 0; j < m_CurHeight ; j ++)
- {
- memcpy(pTmp+k*m_CurWidth,pSrcData+j*m_CurWidth,m_CurWidth);
- k += Step;
- if (k >= m_CurHeight) break;
- }
- j++;
- k = 4; Step = 8;
- for (; j < m_CurHeight ; j ++)
- {
- memcpy(pTmp+k*m_CurWidth,pSrcData+j*m_CurWidth,m_CurWidth);
- k += Step;
- if (k >= m_CurHeight) break;
- }
- j++;
- k = 2; Step = 4;
- for (; j < m_CurHeight ; j ++)
- {
- memcpy(pTmp+k*m_CurWidth,pSrcData+j*m_CurWidth,m_CurWidth);
- k += Step;
- if (k >= m_CurHeight) break;
- }
- j++;
- k = 1; Step = 2;
- for (; j < m_CurHeight ; j ++)
- {
- memcpy(pTmp+k*m_CurWidth,pSrcData+j*m_CurWidth,m_GifInfo.Width);
- k += Step;
- if (k >= m_CurHeight) break;
- }
-
- pSrcData = p2;
- }
- BMPIMAGE * pBmpImage = new BMPIMAGE;
- pBmpImage->tColor = m_CurTColorIndex;
- pBmpImage->DelayTime = m_CurDelayTime;
- pBmpImage->Left = m_CurX;
- pBmpImage->Top = m_CurY;
- pBmpImage->Width = m_CurWidth ;
- pBmpImage->Height = m_CurHeight ;
- pBmpImage->SaveMode = m_CurSaveMode;
-
- memcpy(&pBmpImage->Palette ,&m_CurRgbQuad,sizeof(RGBQUAD)*256);
- USHORT TrueWidth = WIDTHBYTES(m_CurWidth * 8);
- pBmpImage->pImageData = (LPUSTR)GlobalAlloc(GPTR,TrueWidth*m_CurHeight );
- LPUSTR p0,p1;
- p0 = pSrcData;
- p1 = pBmpImage->pImageData;
- for (j = 0; j < m_CurHeight ; j ++)
- {
- k = (m_CurHeight - j - 1);
- for (i = 0 ; i < m_CurWidth ;i ++)
- {
- p1[ k * TrueWidth + i] = p0[j*m_CurWidth+i];
- }
- }
-
- if (m_CurSaveMode)
- {
- GlobalFree(pSrcData);
- }
- m_ImageList.AddTail (pBmpImage);
- return 1;
- }
- BOOL CGIF::ShowImage(HDC hDC, POINT StartPos, UINT ImageNo)
- {
- BITMAPINFOHEADER BmpInfoHeader;
- BmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
-
- BmpInfoHeader.biPlanes = 1;
- BmpInfoHeader.biBitCount = 8;
- BmpInfoHeader.biCompression = BI_RGB;
- BmpInfoHeader.biSizeImage = 0;
- BmpInfoHeader.biXPelsPerMeter = 0;
- BmpInfoHeader.biYPelsPerMeter = 0;
- BmpInfoHeader.biClrUsed = 0;
- BmpInfoHeader.biClrImportant = 0;
-
- myPOSITION Pos;
- Pos = m_ImageList.FindIndex(ImageNo);
- if (Pos)
- {
- BMPIMAGE * pBmpImage = (BMPIMAGE *)m_ImageList.GetAt(Pos );
- BmpInfoHeader.biWidth = pBmpImage->Width;
- BmpInfoHeader.biHeight = pBmpImage->Height;
- BITMAPINFO *pBmInfo = (BITMAPINFO *)GlobalAlloc(GPTR,sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
- memcpy(&pBmInfo->bmiHeader ,&BmpInfoHeader,sizeof(BITMAPINFOHEADER));
- memcpy(pBmInfo->bmiColors ,pBmpImage->Palette ,sizeof(RGBQUAD)*256);
-
- if (pBmpImage->tColor >= 0)
- {
- HDC hMemDC = ::CreateCompatibleDC(hDC);
- HBITMAP hBmp = (HBITMAP)::CreateCompatibleBitmap(hDC,pBmpImage->Width,pBmpImage->Height);
- HBITMAP hOldBmp = (HBITMAP)::SelectObject(hMemDC,hBmp);
- ::StretchDIBits(hMemDC,0 ,0,pBmpImage->Width,pBmpImage->Height,
- 0,0,pBmpImage->Width,pBmpImage->Height ,pBmpImage->pImageData,pBmInfo,
- DIB_RGB_COLORS,SRCCOPY);
- COLORREF cRgb = RGB(pBmpImage->Palette [pBmpImage->tColor].rgbRed,
- pBmpImage->Palette [pBmpImage->tColor].rgbGreen,
- pBmpImage->Palette [pBmpImage->tColor].rgbBlue );
- BitBltEx(hDC,pBmpImage->Left+StartPos.x ,pBmpImage->Top+StartPos.y ,pBmpImage->Width,pBmpImage->Height,hMemDC,0,0 ,cRgb);
- ::SelectObject(hMemDC,hOldBmp);
- ::DeleteObject(hBmp);
- ::DeleteDC(hMemDC);
- }
- else ::StretchDIBits(hDC,pBmpImage->Left+StartPos.x ,pBmpImage->Top+StartPos.y ,pBmpImage->Width,pBmpImage->Height,
- 0,0,pBmpImage->Width,pBmpImage->Height ,pBmpImage->pImageData,pBmInfo,
- DIB_RGB_COLORS,SRCCOPY);
-
- GlobalFree(pBmInfo);
- }
- else
- return 0;
- return 1;
- }
- BOOL BitBltEx(
- HDC hdcDest, // 目标设备上下文句柄
- int nXDest, int nYDest, // 目标矩形左上角的坐标
- int nWidth,int nHeight, // 目标矩形
- HDC hdcSrc, // 源设备上下文句柄
- int nXSrc, int nYSrc, // 源矩形左上角的坐标
- COLORREF cTransparentColor )
- {
- HDC hDC1,hDC2;
- hDC1 = CreateCompatibleDC( hdcSrc );
- hDC2 = CreateCompatibleDC( hdcSrc );
-
- HBITMAP bmp1Old, bmp1 = CreateBitmap(nWidth, nHeight, 1, 1, NULL);
- bmp1Old = (HBITMAP)SelectObject(hDC1, bmp1);
- COLORREF cColor = SetBkColor(hdcSrc, cTransparentColor);
- ::BitBlt(hDC1,0,0,nWidth,nHeight,hdcSrc,nXSrc,nYSrc,SRCCOPY);
- HBITMAP bmp2Old, bmp2 = CreateBitmap(nWidth, nHeight, 1, 1, NULL);
- bmp2Old = (HBITMAP)SelectObject(hDC2, bmp2);
-
- ::BitBlt(hDC2,0,0,nWidth,nHeight,hDC1,0,0,NOTSRCCOPY);
-
- HDC hDC3;
- hDC3 = CreateCompatibleDC( hdcSrc );
- HBITMAP bmp3Old, bmp3 = CreateCompatibleBitmap(hdcSrc, nWidth, nHeight);
- bmp3Old = (HBITMAP)SelectObject(hDC3, bmp3);
- ::BitBlt(hDC3,0,0,nWidth,nHeight,hdcSrc,nXSrc,nYSrc,SRCCOPY);
- ::BitBlt(hDC3,0,0,nWidth,nHeight,hDC2,0,0,SRCAND);
- SelectObject(hDC2, bmp2Old);
- DeleteObject(bmp2);
- DeleteDC(hDC2);
-
- HDC hDC4;
- hDC4 = CreateCompatibleDC( hdcSrc );
- HBITMAP bmp4Old, bmp4 = CreateCompatibleBitmap(hdcSrc, nWidth, nHeight);
- bmp4Old = (HBITMAP)SelectObject(hDC4, bmp4);
- ::BitBlt(hDC4,0,0,nWidth,nHeight,hdcDest,nXDest,nYDest,SRCCOPY);
- ::BitBlt(hDC4,0,0,nWidth,nHeight,hDC1,0,0,SRCAND);
- SelectObject(hDC1, bmp1Old);
- DeleteObject(bmp1);
- DeleteDC(hDC1);
-
-
- HDC hDC5;
- hDC5 = CreateCompatibleDC( hdcSrc );
- HBITMAP bmp5Old, bmp5 = CreateCompatibleBitmap(hdcSrc, nWidth, nHeight);
- bmp5Old = (HBITMAP)SelectObject(hDC5, bmp5);
- ::BitBlt(hDC5,0,0,nWidth,nHeight,hDC3,0,0,SRCCOPY);
- ::BitBlt(hDC5,0,0,nWidth,nHeight,hDC4,0,0,SRCPAINT);
-
- SelectObject(hDC3, bmp3Old);
- DeleteObject(bmp3);
- DeleteDC(hDC3);
- SelectObject(hDC4, bmp4Old);
- DeleteObject(bmp4);
- DeleteDC(hDC4);
- ::BitBlt(hdcDest,nXDest,nYDest,nWidth,nHeight,hDC5,0,0,SRCCOPY);
-
- SelectObject(hDC5, bmp5Old);
- DeleteObject(bmp5);
- DeleteDC(hDC5);
- return 1;
- }
- BOOL CGIF::GetImageInfo(RECT &Rect,COLORREF &tColor ,UINT ImageNo)
- {
- myPOSITION Pos;
- Pos = m_ImageList.FindIndex(ImageNo);
- if (Pos)
- {
- BMPIMAGE * pBmpImage = (BMPIMAGE *)m_ImageList.GetAt(Pos );
- Rect.left = pBmpImage->Left ;
- Rect.top = pBmpImage->Top ;
- Rect.right = pBmpImage->Left + pBmpImage->Width ;
- Rect.bottom = pBmpImage->Top + pBmpImage->Height ;
- tColor = pBmpImage->tColor ;
- return 1;
- }
- else return 0;
- }
- //AfxBeginThread(GifThread,(LPVOID)m_hWnd);
- //可以采用多线程实现动画
- static UINT GifThread(LPVOID p_Gif,LPVOID hwnd)
- {
- CGIF *m_Gif=(CGIF *)p_Gif;
- // TODO: Add your control notification handler code here
- HDC m_hDC=GetDC((HWND)hwnd);
- HDC hMemDC = ::CreateCompatibleDC(m_hDC);
- HBITMAP hBmp = (HBITMAP)::CreateCompatibleBitmap(m_hDC ,800,600);
- HBITMAP hOldBmp = (HBITMAP)::SelectObject(hMemDC,hBmp);
- ::BitBlt (hMemDC,0,0,800,600,m_hDC,0,0,SRCCOPY);
- HDC hMemDC1 = ::CreateCompatibleDC(m_hDC);
- HBITMAP hBmp1 = (HBITMAP)::CreateCompatibleBitmap(m_hDC ,800,600);
- HBITMAP hOldBmp1 = (HBITMAP)::SelectObject(hMemDC1,hBmp1);
- POINT pt;
- pt.x=30;
- pt.y=30;
- int i = 0;
- int k = 0;
- MSG msg;
- RECT Rect;
- COLORREF tColor;
- while (1)
- {
- if (!m_Gif->ShowImage (hMemDC1,pt,i++))
- {
- i = 0;
- m_Gif->ShowImage (hMemDC1,pt,i++);
- k++;
- if (k > 10) break;
- }//hMemDC1中保存背景图片
- ::BitBlt (hMemDC,0,0,800,600,m_hDC,0,0,SRCCOPY);
- if (m_Gif->GetImageInfo (Rect,tColor,i-1))
- {
- ::BitBlt (m_hDC ,0,0,Rect.right-Rect.left+pt.x + 50,Rect.bottom-Rect.top+pt.y + 50,hMemDC1,0,0,SRCCOPY);
- ::BitBlt (hMemDC1,0,0,Rect.right-Rect.left+pt.x + 50,Rect.bottom-Rect.top+pt.y+50,hMemDC,0,0,SRCCOPY);
- }
- else
- {
- ::BitBlt (m_hDC ,0,0,800,600,hMemDC1,0,0,SRCCOPY);
- ::BitBlt (hMemDC1,0,0,800,600,hMemDC,0,0,SRCCOPY);
- }
- if(::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE ))
- {
- ::TranslateMessage(&msg);
- ::DispatchMessage(&msg);
- }
- Sleep(100);
- ::BitBlt (m_hDC,0,0,800,600,hMemDC,0,0,SRCCOPY);
- if(::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE ))
- {
- ::TranslateMessage(&msg);
- ::DispatchMessage(&msg);
- }
-
- }
- ::SelectObject(hMemDC,hOldBmp);
- ::DeleteObject(hBmp);
- ::DeleteDC(hMemDC);
- ::SelectObject(hMemDC1,hOldBmp1);
- ::DeleteObject(hBmp1);
- ::DeleteDC(hMemDC1);
- ReleaseDC((HWND)hwnd,m_hDC);
- return 1;
- }