UIGifAnim.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. #include "StdAfx.h"
  2. #include "UIGifAnim.h"
  3. ///////////////////////////////////////////////////////////////////////////////////////
  4. DECLARE_HANDLE(HZIP); // An HZIP identifies a zip file that has been opened
  5. typedef DWORD ZRESULT;
  6. typedef struct
  7. {
  8. int index; // index of this file within the zip
  9. char name[MAX_PATH]; // filename within the zip
  10. DWORD attr; // attributes, as in GetFileAttributes.
  11. FILETIME atime,ctime,mtime;// access, create, modify filetimes
  12. long comp_size; // sizes of item, compressed and uncompressed. These
  13. long unc_size; // may be -1 if not yet known (e.g. being streamed in)
  14. } ZIPENTRY;
  15. typedef struct
  16. {
  17. int index; // index of this file within the zip
  18. TCHAR name[MAX_PATH]; // filename within the zip
  19. DWORD attr; // attributes, as in GetFileAttributes.
  20. FILETIME atime,ctime,mtime;// access, create, modify filetimes
  21. long comp_size; // sizes of item, compressed and uncompressed. These
  22. long unc_size; // may be -1 if not yet known (e.g. being streamed in)
  23. } ZIPENTRYW;
  24. #define OpenZip OpenZipU
  25. #define CloseZip(hz) CloseZipU(hz)
  26. extern HZIP OpenZipU(void *z,unsigned int len,DWORD flags);
  27. extern ZRESULT CloseZipU(HZIP hz);
  28. #ifdef _UNICODE
  29. #define ZIPENTRY ZIPENTRYW
  30. #define GetZipItem GetZipItemW
  31. #define FindZipItem FindZipItemW
  32. #else
  33. #define GetZipItem GetZipItemA
  34. #define FindZipItem FindZipItemA
  35. #endif
  36. extern ZRESULT GetZipItemA(HZIP hz, int index, ZIPENTRY *ze);
  37. extern ZRESULT GetZipItemW(HZIP hz, int index, ZIPENTRYW *ze);
  38. extern ZRESULT FindZipItemA(HZIP hz, const TCHAR *name, bool ic, int *index, ZIPENTRY *ze);
  39. extern ZRESULT FindZipItemW(HZIP hz, const TCHAR *name, bool ic, int *index, ZIPENTRYW *ze);
  40. extern ZRESULT UnzipItem(HZIP hz, int index, void *dst, unsigned int len, DWORD flags);
  41. ///////////////////////////////////////////////////////////////////////////////////////
  42. namespace DuiLib
  43. {
  44. CGifAnimUI::CGifAnimUI(void)
  45. {
  46. m_pGifImage = NULL;
  47. m_pPropertyItem = NULL;
  48. m_nFrameCount = 0;
  49. m_nFramePosition = 0;
  50. m_bIsAutoPlay = true;
  51. m_bIsAutoSize = false;
  52. m_bIsPlaying = false;
  53. m_pStream = NULL;
  54. }
  55. CGifAnimUI::~CGifAnimUI(void)
  56. {
  57. DeleteGif();
  58. m_pManager->KillTimer( this, EVENT_TIEM_ID );
  59. }
  60. LPCTSTR CGifAnimUI::GetClass() const
  61. {
  62. return DUI_CTR_GIFANIM;
  63. }
  64. LPVOID CGifAnimUI::GetInterface( LPCTSTR pstrName )
  65. {
  66. if( _tcscmp(pstrName, DUI_CTR_GIFANIM) == 0 ) return static_cast<CGifAnimUI*>(this);
  67. return CControlUI::GetInterface(pstrName);
  68. }
  69. void CGifAnimUI::DoInit()
  70. {
  71. InitGifImage();
  72. }
  73. bool CGifAnimUI::DoPaint(HDC hDC, const RECT& rcPaint, CControlUI* pStopControl)
  74. {
  75. if ( NULL == m_pGifImage )
  76. {
  77. InitGifImage();
  78. }
  79. DrawFrame( hDC );
  80. return true;
  81. }
  82. void CGifAnimUI::DoEvent( TEventUI& event )
  83. {
  84. if( event.Type == UIEVENT_TIMER )
  85. OnTimer( (UINT_PTR)event.wParam );
  86. }
  87. void CGifAnimUI::SetVisible(bool bVisible /* = true */)
  88. {
  89. CControlUI::SetVisible(bVisible);
  90. if (bVisible)
  91. PlayGif();
  92. else
  93. StopGif();
  94. }
  95. void CGifAnimUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
  96. {
  97. if( _tcscmp(pstrName, _T("bkimage")) == 0 ) SetBkImage(pstrValue);
  98. else if( _tcscmp(pstrName, _T("autoplay")) == 0 ) {
  99. SetAutoPlay(_tcscmp(pstrValue, _T("true")) == 0);
  100. }
  101. else if( _tcscmp(pstrName, _T("autosize")) == 0 ) {
  102. SetAutoSize(_tcscmp(pstrValue, _T("true")) == 0);
  103. }
  104. else
  105. CControlUI::SetAttribute(pstrName, pstrValue);
  106. }
  107. void CGifAnimUI::SetBkImage(LPCTSTR pStrImage)
  108. {
  109. if( m_sBkImage == pStrImage || NULL == pStrImage) return;
  110. m_sBkImage = pStrImage;
  111. StopGif();
  112. DeleteGif();
  113. Invalidate();
  114. }
  115. LPCTSTR CGifAnimUI::GetBkImage()
  116. {
  117. return m_sBkImage.GetData();
  118. }
  119. void CGifAnimUI::SetAutoPlay(bool bIsAuto)
  120. {
  121. m_bIsAutoPlay = bIsAuto;
  122. }
  123. bool CGifAnimUI::IsAutoPlay() const
  124. {
  125. return m_bIsAutoPlay;
  126. }
  127. void CGifAnimUI::SetAutoSize(bool bIsAuto)
  128. {
  129. m_bIsAutoSize = bIsAuto;
  130. }
  131. bool CGifAnimUI::IsAutoSize() const
  132. {
  133. return m_bIsAutoSize;
  134. }
  135. void CGifAnimUI::PlayGif()
  136. {
  137. if (m_bIsPlaying || m_pGifImage == NULL)
  138. {
  139. return;
  140. }
  141. long lPause = ((long*) m_pPropertyItem->value)[m_nFramePosition] * 10;
  142. if ( lPause == 0 ) lPause = 100;
  143. m_pManager->SetTimer( this, EVENT_TIEM_ID, lPause );
  144. m_bIsPlaying = true;
  145. }
  146. void CGifAnimUI::PauseGif()
  147. {
  148. if (!m_bIsPlaying || m_pGifImage == NULL)
  149. {
  150. return;
  151. }
  152. m_pManager->KillTimer(this, EVENT_TIEM_ID);
  153. this->Invalidate();
  154. m_bIsPlaying = false;
  155. }
  156. void CGifAnimUI::StopGif()
  157. {
  158. if (!m_bIsPlaying)
  159. {
  160. return;
  161. }
  162. m_pManager->KillTimer(this, EVENT_TIEM_ID);
  163. m_nFramePosition = 0;
  164. this->Invalidate();
  165. m_bIsPlaying = false;
  166. }
  167. void CGifAnimUI::InitGifImage()
  168. {
  169. m_pGifImage = LoadGifFromFile(GetBkImage());
  170. if ( NULL == m_pGifImage ) return;
  171. UINT nCount = 0;
  172. nCount = m_pGifImage->GetFrameDimensionsCount();
  173. GUID* pDimensionIDs = new GUID[ nCount ];
  174. m_pGifImage->GetFrameDimensionsList( pDimensionIDs, nCount );
  175. m_nFrameCount = m_pGifImage->GetFrameCount( &pDimensionIDs[0] );
  176. int nSize = m_pGifImage->GetPropertyItemSize( PropertyTagFrameDelay );
  177. m_pPropertyItem = (Gdiplus::PropertyItem*) malloc( nSize );
  178. m_pGifImage->GetPropertyItem( PropertyTagFrameDelay, nSize, m_pPropertyItem );
  179. delete[] pDimensionIDs;
  180. pDimensionIDs = NULL;
  181. if (m_bIsAutoSize)
  182. {
  183. SetFixedWidth(m_pGifImage->GetWidth());
  184. SetFixedHeight(m_pGifImage->GetHeight());
  185. }
  186. if (m_bIsAutoPlay && nSize > 0)
  187. {
  188. PlayGif();
  189. }
  190. }
  191. void CGifAnimUI::DeleteGif()
  192. {
  193. if (m_pStream != NULL )
  194. {
  195. m_pStream->Release();
  196. m_pStream = NULL;
  197. }
  198. if ( m_pGifImage != NULL )
  199. {
  200. delete m_pGifImage;
  201. m_pGifImage = NULL;
  202. }
  203. if ( m_pPropertyItem != NULL )
  204. {
  205. free( m_pPropertyItem );
  206. m_pPropertyItem = NULL;
  207. }
  208. m_nFrameCount = 0;
  209. m_nFramePosition = 0;
  210. }
  211. void CGifAnimUI::OnTimer( UINT_PTR idEvent )
  212. {
  213. if ( idEvent != EVENT_TIEM_ID )
  214. return;
  215. m_pManager->KillTimer( this, EVENT_TIEM_ID );
  216. this->Invalidate();
  217. m_nFramePosition = (++m_nFramePosition) % m_nFrameCount;
  218. long lPause = ((long*) m_pPropertyItem->value)[m_nFramePosition] * 10;
  219. if ( lPause == 0 ) lPause = 100;
  220. m_pManager->SetTimer( this, EVENT_TIEM_ID, lPause );
  221. }
  222. void CGifAnimUI::DrawFrame( HDC hDC )
  223. {
  224. if ( NULL == hDC || NULL == m_pGifImage ) return;
  225. GUID pageGuid = Gdiplus::FrameDimensionTime;
  226. Gdiplus::Graphics graphics( hDC );
  227. graphics.DrawImage( m_pGifImage, m_rcItem.left, m_rcItem.top, m_rcItem.right-m_rcItem.left, m_rcItem.bottom-m_rcItem.top );
  228. m_pGifImage->SelectActiveFrame( &pageGuid, m_nFramePosition );
  229. }
  230. Gdiplus::Image* CGifAnimUI::LoadGifFromFile(LPCTSTR pstrGifPath)
  231. {
  232. LPBYTE pData = NULL;
  233. DWORD dwSize = 0;
  234. do
  235. {
  236. CDuiString sFile = CPaintManagerUI::GetResourcePath();
  237. if( CPaintManagerUI::GetResourceZip().IsEmpty() ) {
  238. sFile += pstrGifPath;
  239. HANDLE hFile = ::CreateFile(sFile.GetData(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, \
  240. FILE_ATTRIBUTE_NORMAL, NULL);
  241. if( hFile == INVALID_HANDLE_VALUE ) break;
  242. dwSize = ::GetFileSize(hFile, NULL);
  243. if( dwSize == 0 ) break;
  244. DWORD dwRead = 0;
  245. pData = new BYTE[ dwSize ];
  246. ::ReadFile( hFile, pData, dwSize, &dwRead, NULL );
  247. ::CloseHandle( hFile );
  248. if( dwRead != dwSize ) {
  249. delete[] pData;
  250. pData = NULL;
  251. break;
  252. }
  253. }
  254. else {
  255. sFile += CPaintManagerUI::GetResourceZip();
  256. HZIP hz = NULL;
  257. if( CPaintManagerUI::IsCachedResourceZip() ) hz = (HZIP)CPaintManagerUI::GetResourceZipHandle();
  258. else hz = OpenZip((void*)sFile.GetData(), 0, 2);
  259. if( hz == NULL ) break;
  260. ZIPENTRY ze;
  261. int i;
  262. if( FindZipItem(hz, pstrGifPath, true, &i, &ze) != 0 ) break;
  263. dwSize = ze.unc_size;
  264. if( dwSize == 0 ) break;
  265. pData = new BYTE[ dwSize ];
  266. int res = UnzipItem(hz, i, pData, dwSize, 3);
  267. if( res != 0x00000000 && res != 0x00000600) {
  268. delete[] pData;
  269. pData = NULL;
  270. if( !CPaintManagerUI::IsCachedResourceZip() ) CloseZip(hz);
  271. break;
  272. }
  273. if( !CPaintManagerUI::IsCachedResourceZip() ) CloseZip(hz);
  274. }
  275. } while (0);
  276. while (!pData)
  277. {
  278. //读不到图片, 则直接去读取bitmap.m_lpstr指向的路径
  279. HANDLE hFile = ::CreateFile(pstrGifPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, \
  280. FILE_ATTRIBUTE_NORMAL, NULL);
  281. if( hFile == INVALID_HANDLE_VALUE ) break;
  282. dwSize = ::GetFileSize(hFile, NULL);
  283. if( dwSize == 0 ) break;
  284. DWORD dwRead = 0;
  285. pData = new BYTE[ dwSize ];
  286. ::ReadFile( hFile, pData, dwSize, &dwRead, NULL );
  287. ::CloseHandle( hFile );
  288. if( dwRead != dwSize ) {
  289. delete[] pData;
  290. pData = NULL;
  291. }
  292. break;
  293. }
  294. if (!pData)
  295. {
  296. return NULL;
  297. }
  298. Gdiplus::Image* pImage = LoadGifFromMemory(pData, dwSize);
  299. delete[] pData;
  300. return pImage;
  301. }
  302. Gdiplus::Image* CGifAnimUI::LoadGifFromMemory( LPVOID pBuf,size_t dwSize )
  303. {
  304. HGLOBAL hMem = ::GlobalAlloc(GMEM_MOVEABLE, dwSize);
  305. BYTE* pMem = (BYTE*)::GlobalLock(hMem);
  306. memcpy(pMem, pBuf, dwSize);
  307. ::GlobalUnlock(hMem);
  308. ::CreateStreamOnHGlobal(hMem, TRUE, &m_pStream);
  309. Gdiplus::Image *pImg = Gdiplus::Image::FromStream(m_pStream);
  310. if(!pImg || pImg->GetLastStatus() != Gdiplus::Ok)
  311. {
  312. m_pStream->Release();
  313. m_pStream = NULL;
  314. return 0;
  315. }
  316. return pImg;
  317. }
  318. }