UIGifAnim.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. }
  54. CGifAnimUI::~CGifAnimUI(void)
  55. {
  56. DeleteGif();
  57. m_pManager->KillTimer( this, EVENT_TIEM_ID );
  58. }
  59. LPCTSTR CGifAnimUI::GetClass() const
  60. {
  61. return _T("GifAnimUI");
  62. }
  63. LPVOID CGifAnimUI::GetInterface( LPCTSTR pstrName )
  64. {
  65. if( _tcscmp(pstrName, DUI_CTR_GIFANIM) == 0 ) return static_cast<CGifAnimUI*>(this);
  66. return CControlUI::GetInterface(pstrName);
  67. }
  68. void CGifAnimUI::DoInit()
  69. {
  70. InitGifImage();
  71. }
  72. void CGifAnimUI::DoPaint( HDC hDC, const RECT& rcPaint )
  73. {
  74. if( !::IntersectRect( &m_rcPaint, &rcPaint, &m_rcItem ) ) return;
  75. if ( NULL == m_pGifImage )
  76. {
  77. InitGifImage();
  78. }
  79. DrawFrame( hDC );
  80. }
  81. void CGifAnimUI::DoEvent( TEventUI& event )
  82. {
  83. if( event.Type == UIEVENT_TIMER )
  84. OnTimer( (UINT_PTR)event.wParam );
  85. }
  86. void CGifAnimUI::SetVisible(bool bVisible /* = true */)
  87. {
  88. CControlUI::SetVisible(bVisible);
  89. if (bVisible)
  90. PlayGif();
  91. else
  92. StopGif();
  93. }
  94. void CGifAnimUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
  95. {
  96. if( _tcscmp(pstrName, _T("bkimage")) == 0 ) SetBkImage(pstrValue);
  97. else if( _tcscmp(pstrName, _T("autoplay")) == 0 ) {
  98. SetAutoPlay(_tcscmp(pstrValue, _T("true")) == 0);
  99. }
  100. else if( _tcscmp(pstrName, _T("autosize")) == 0 ) {
  101. SetAutoSize(_tcscmp(pstrValue, _T("true")) == 0);
  102. }
  103. else
  104. CControlUI::SetAttribute(pstrName, pstrValue);
  105. }
  106. void CGifAnimUI::SetBkImage(LPCTSTR pStrImage)
  107. {
  108. if( m_sBkImage == pStrImage || NULL == pStrImage) return;
  109. m_sBkImage = pStrImage;
  110. StopGif();
  111. DeleteGif();
  112. Invalidate();
  113. }
  114. LPCTSTR CGifAnimUI::GetBkImage()
  115. {
  116. return m_sBkImage.GetData();
  117. }
  118. void CGifAnimUI::SetAutoPlay(bool bIsAuto)
  119. {
  120. m_bIsAutoPlay = bIsAuto;
  121. }
  122. bool CGifAnimUI::IsAutoPlay() const
  123. {
  124. return m_bIsAutoPlay;
  125. }
  126. void CGifAnimUI::SetAutoSize(bool bIsAuto)
  127. {
  128. m_bIsAutoSize = bIsAuto;
  129. }
  130. bool CGifAnimUI::IsAutoSize() const
  131. {
  132. return m_bIsAutoSize;
  133. }
  134. void CGifAnimUI::PlayGif()
  135. {
  136. if (m_bIsPlaying || m_pGifImage == NULL)
  137. {
  138. return;
  139. }
  140. long lPause = ((long*) m_pPropertyItem->value)[m_nFramePosition] * 10;
  141. if ( lPause == 0 ) lPause = 100;
  142. m_pManager->SetTimer( this, EVENT_TIEM_ID, lPause );
  143. m_bIsPlaying = true;
  144. }
  145. void CGifAnimUI::PauseGif()
  146. {
  147. if (!m_bIsPlaying || m_pGifImage == NULL)
  148. {
  149. return;
  150. }
  151. m_pManager->KillTimer(this, EVENT_TIEM_ID);
  152. this->Invalidate();
  153. m_bIsPlaying = false;
  154. }
  155. void CGifAnimUI::StopGif()
  156. {
  157. if (!m_bIsPlaying)
  158. {
  159. return;
  160. }
  161. m_pManager->KillTimer(this, EVENT_TIEM_ID);
  162. m_nFramePosition = 0;
  163. this->Invalidate();
  164. m_bIsPlaying = false;
  165. }
  166. void CGifAnimUI::InitGifImage()
  167. {
  168. m_pGifImage = LoadGifFromFile(GetBkImage());
  169. if ( NULL == m_pGifImage ) return;
  170. UINT nCount = 0;
  171. nCount = m_pGifImage->GetFrameDimensionsCount();
  172. GUID* pDimensionIDs = new GUID[ nCount ];
  173. m_pGifImage->GetFrameDimensionsList( pDimensionIDs, nCount );
  174. m_nFrameCount = m_pGifImage->GetFrameCount( &pDimensionIDs[0] );
  175. int nSize = m_pGifImage->GetPropertyItemSize( PropertyTagFrameDelay );
  176. m_pPropertyItem = (Gdiplus::PropertyItem*) malloc( nSize );
  177. m_pGifImage->GetPropertyItem( PropertyTagFrameDelay, nSize, m_pPropertyItem );
  178. delete pDimensionIDs;
  179. pDimensionIDs = NULL;
  180. if (m_bIsAutoSize)
  181. {
  182. SetFixedWidth(m_pGifImage->GetWidth());
  183. SetFixedHeight(m_pGifImage->GetHeight());
  184. }
  185. if (m_bIsAutoPlay)
  186. {
  187. PlayGif();
  188. }
  189. }
  190. void CGifAnimUI::DeleteGif()
  191. {
  192. if ( m_pGifImage != NULL )
  193. {
  194. delete m_pGifImage;
  195. m_pGifImage = NULL;
  196. }
  197. if ( m_pPropertyItem != NULL )
  198. {
  199. free( m_pPropertyItem );
  200. m_pPropertyItem = NULL;
  201. }
  202. m_nFrameCount = 0;
  203. m_nFramePosition = 0;
  204. }
  205. void CGifAnimUI::OnTimer( UINT_PTR idEvent )
  206. {
  207. if ( idEvent != EVENT_TIEM_ID )
  208. return;
  209. m_pManager->KillTimer( this, EVENT_TIEM_ID );
  210. this->Invalidate();
  211. m_nFramePosition = (++m_nFramePosition) % m_nFrameCount;
  212. long lPause = ((long*) m_pPropertyItem->value)[m_nFramePosition] * 10;
  213. if ( lPause == 0 ) lPause = 100;
  214. m_pManager->SetTimer( this, EVENT_TIEM_ID, lPause );
  215. }
  216. void CGifAnimUI::DrawFrame( HDC hDC )
  217. {
  218. if ( NULL == hDC || NULL == m_pGifImage ) return;
  219. GUID pageGuid = Gdiplus::FrameDimensionTime;
  220. Gdiplus::Graphics graphics( hDC );
  221. graphics.DrawImage( m_pGifImage, m_rcItem.left, m_rcItem.top, m_rcItem.right-m_rcItem.left, m_rcItem.bottom-m_rcItem.top );
  222. m_pGifImage->SelectActiveFrame( &pageGuid, m_nFramePosition );
  223. }
  224. Gdiplus::Image* CGifAnimUI::LoadGifFromFile(LPCTSTR pstrGifPath)
  225. {
  226. LPBYTE pData = NULL;
  227. DWORD dwSize = 0;
  228. do
  229. {
  230. CDuiString sFile = CPaintManagerUI::GetResourcePath();
  231. if( CPaintManagerUI::GetResourceZip().IsEmpty() ) {
  232. sFile += pstrGifPath;
  233. HANDLE hFile = ::CreateFile(sFile.GetData(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, \
  234. FILE_ATTRIBUTE_NORMAL, NULL);
  235. if( hFile == INVALID_HANDLE_VALUE ) break;
  236. dwSize = ::GetFileSize(hFile, NULL);
  237. if( dwSize == 0 ) break;
  238. DWORD dwRead = 0;
  239. pData = new BYTE[ dwSize ];
  240. ::ReadFile( hFile, pData, dwSize, &dwRead, NULL );
  241. ::CloseHandle( hFile );
  242. if( dwRead != dwSize ) {
  243. delete[] pData;
  244. pData = NULL;
  245. break;
  246. }
  247. }
  248. else {
  249. sFile += CPaintManagerUI::GetResourceZip();
  250. HZIP hz = NULL;
  251. if( CPaintManagerUI::IsCachedResourceZip() ) hz = (HZIP)CPaintManagerUI::GetResourceZipHandle();
  252. else hz = OpenZip((void*)sFile.GetData(), 0, 2);
  253. if( hz == NULL ) break;
  254. ZIPENTRY ze;
  255. int i;
  256. if( FindZipItem(hz, pstrGifPath, true, &i, &ze) != 0 ) break;
  257. dwSize = ze.unc_size;
  258. if( dwSize == 0 ) break;
  259. pData = new BYTE[ dwSize ];
  260. int res = UnzipItem(hz, i, pData, dwSize, 3);
  261. if( res != 0x00000000 && res != 0x00000600) {
  262. delete[] pData;
  263. pData = NULL;
  264. if( !CPaintManagerUI::IsCachedResourceZip() ) CloseZip(hz);
  265. break;
  266. }
  267. if( !CPaintManagerUI::IsCachedResourceZip() ) CloseZip(hz);
  268. }
  269. } while (0);
  270. while (!pData)
  271. {
  272. //读不到图片, 则直接去读取bitmap.m_lpstr指向的路径
  273. HANDLE hFile = ::CreateFile(pstrGifPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, \
  274. FILE_ATTRIBUTE_NORMAL, NULL);
  275. if( hFile == INVALID_HANDLE_VALUE ) break;
  276. dwSize = ::GetFileSize(hFile, NULL);
  277. if( dwSize == 0 ) break;
  278. DWORD dwRead = 0;
  279. pData = new BYTE[ dwSize ];
  280. ::ReadFile( hFile, pData, dwSize, &dwRead, NULL );
  281. ::CloseHandle( hFile );
  282. if( dwRead != dwSize ) {
  283. delete[] pData;
  284. pData = NULL;
  285. }
  286. break;
  287. }
  288. if (!pData)
  289. {
  290. return NULL;
  291. }
  292. Gdiplus::Image* pImage = LoadGifFromMemory(pData, dwSize);
  293. delete pData;
  294. return pImage;
  295. }
  296. Gdiplus::Image* CGifAnimUI::LoadGifFromMemory( LPVOID pBuf,size_t dwSize )
  297. {
  298. HGLOBAL hMem = ::GlobalAlloc(GMEM_FIXED, dwSize);
  299. BYTE* pMem = (BYTE*)::GlobalLock(hMem);
  300. memcpy(pMem, pBuf, dwSize);
  301. IStream* pStm = NULL;
  302. ::CreateStreamOnHGlobal(hMem, TRUE, &pStm);
  303. Gdiplus::Image *pImg = Gdiplus::Image::FromStream(pStm);
  304. if(!pImg || pImg->GetLastStatus() != Gdiplus::Ok)
  305. {
  306. pStm->Release();
  307. ::GlobalUnlock(hMem);
  308. return 0;
  309. }
  310. return pImg;
  311. }
  312. }