UIGifAnim.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include "StdAfx.h"
  2. #include "UIGifAnim.h"
  3. ///////////////////////////////////////////////////////////////////////////////////////
  4. namespace DuiLib
  5. {
  6. IMPLEMENT_DUICONTROL(CGifAnimUI)
  7. CGifAnimUI::CGifAnimUI(void)
  8. {
  9. m_pGifImage = NULL;
  10. m_pPropertyItem = NULL;
  11. m_nFrameCount = 0;
  12. m_nFramePosition = 0;
  13. m_bIsAutoPlay = true;
  14. m_bIsAutoSize = false;
  15. m_bIsPlaying = false;
  16. }
  17. CGifAnimUI::~CGifAnimUI(void)
  18. {
  19. DeleteGif();
  20. m_pManager->KillTimer( this, EVENT_TIEM_ID );
  21. }
  22. LPCTSTR CGifAnimUI::GetClass() const
  23. {
  24. return _T("GifAnimUI");
  25. }
  26. LPVOID CGifAnimUI::GetInterface( LPCTSTR pstrName )
  27. {
  28. if( _tcsicmp(pstrName, DUI_CTR_GIFANIM) == 0 ) return static_cast<CGifAnimUI*>(this);
  29. return CControlUI::GetInterface(pstrName);
  30. }
  31. void CGifAnimUI::DoInit()
  32. {
  33. InitGifImage();
  34. }
  35. bool CGifAnimUI::DoPaint(HDC hDC, const RECT& rcPaint, CControlUI* pStopControl)
  36. {
  37. if( !::IntersectRect( &m_rcPaint, &rcPaint, &m_rcItem ) ) return true;
  38. if ( NULL == m_pGifImage )
  39. {
  40. InitGifImage();
  41. }
  42. DrawFrame( hDC );
  43. return true;
  44. }
  45. void CGifAnimUI::DoEvent( TEventUI& event )
  46. {
  47. if( event.Type == UIEVENT_TIMER )
  48. OnTimer( (UINT_PTR)event.wParam );
  49. }
  50. void CGifAnimUI::SetVisible(bool bVisible /* = true */)
  51. {
  52. CControlUI::SetVisible(bVisible);
  53. if (bVisible)
  54. PlayGif();
  55. else
  56. StopGif();
  57. }
  58. void CGifAnimUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
  59. {
  60. if( _tcsicmp(pstrName, _T("bkimage")) == 0 ) SetBkImage(pstrValue);
  61. else if( _tcsicmp(pstrName, _T("autoplay")) == 0 ) {
  62. SetAutoPlay(_tcsicmp(pstrValue, _T("true")) == 0);
  63. }
  64. else if( _tcsicmp(pstrName, _T("autosize")) == 0 ) {
  65. SetAutoSize(_tcsicmp(pstrValue, _T("true")) == 0);
  66. }
  67. else
  68. CControlUI::SetAttribute(pstrName, pstrValue);
  69. }
  70. void CGifAnimUI::SetBkImage(LPCTSTR pStrImage)
  71. {
  72. if( m_sBkImage == pStrImage || NULL == pStrImage) return;
  73. m_sBkImage = pStrImage;
  74. StopGif();
  75. DeleteGif();
  76. Invalidate();
  77. }
  78. LPCTSTR CGifAnimUI::GetBkImage()
  79. {
  80. return m_sBkImage.GetData();
  81. }
  82. void CGifAnimUI::SetAutoPlay(bool bIsAuto)
  83. {
  84. m_bIsAutoPlay = bIsAuto;
  85. }
  86. bool CGifAnimUI::IsAutoPlay() const
  87. {
  88. return m_bIsAutoPlay;
  89. }
  90. void CGifAnimUI::SetAutoSize(bool bIsAuto)
  91. {
  92. m_bIsAutoSize = bIsAuto;
  93. }
  94. bool CGifAnimUI::IsAutoSize() const
  95. {
  96. return m_bIsAutoSize;
  97. }
  98. void CGifAnimUI::PlayGif()
  99. {
  100. if (m_bIsPlaying || m_pGifImage == NULL || m_nFrameCount <= 1)
  101. {
  102. return;
  103. }
  104. long lPause = ((long*) m_pPropertyItem->value)[m_nFramePosition] * 10;
  105. if ( lPause == 0 ) lPause = 100;
  106. m_pManager->SetTimer( this, EVENT_TIEM_ID, lPause );
  107. m_bIsPlaying = true;
  108. }
  109. void CGifAnimUI::PauseGif()
  110. {
  111. if (!m_bIsPlaying || m_pGifImage == NULL)
  112. {
  113. return;
  114. }
  115. m_pManager->KillTimer(this, EVENT_TIEM_ID);
  116. this->Invalidate();
  117. m_bIsPlaying = false;
  118. }
  119. void CGifAnimUI::StopGif()
  120. {
  121. if (!m_bIsPlaying)
  122. {
  123. return;
  124. }
  125. m_pManager->KillTimer(this, EVENT_TIEM_ID);
  126. m_nFramePosition = 0;
  127. this->Invalidate();
  128. m_bIsPlaying = false;
  129. }
  130. void CGifAnimUI::InitGifImage()
  131. {
  132. TImageInfo* pImageInfo = CRenderEngine::GdiplusLoadImage(GetBkImage());
  133. if(pImageInfo == NULL) return;
  134. m_pGifImage = pImageInfo->pImage;
  135. if ( NULL == m_pGifImage ) return;
  136. UINT nCount = 0;
  137. nCount = m_pGifImage->GetFrameDimensionsCount();
  138. GUID* pDimensionIDs = new GUID[ nCount ];
  139. m_pGifImage->GetFrameDimensionsList( pDimensionIDs, nCount );
  140. m_nFrameCount = m_pGifImage->GetFrameCount( &pDimensionIDs[0] );
  141. if (m_nFrameCount > 1)
  142. {
  143. int nSize = m_pGifImage->GetPropertyItemSize(PropertyTagFrameDelay);
  144. m_pPropertyItem = (Gdiplus::PropertyItem*) malloc(nSize);
  145. m_pGifImage->GetPropertyItem(PropertyTagFrameDelay, nSize, m_pPropertyItem);
  146. }
  147. delete[] pDimensionIDs;
  148. pDimensionIDs = NULL;
  149. if (m_bIsAutoSize)
  150. {
  151. SetFixedWidth(m_pGifImage->GetWidth());
  152. SetFixedHeight(m_pGifImage->GetHeight());
  153. }
  154. if (m_bIsAutoPlay)
  155. {
  156. PlayGif();
  157. }
  158. }
  159. void CGifAnimUI::DeleteGif()
  160. {
  161. if ( m_pGifImage != NULL )
  162. {
  163. delete m_pGifImage;
  164. m_pGifImage = NULL;
  165. }
  166. if ( m_pPropertyItem != NULL )
  167. {
  168. free( m_pPropertyItem );
  169. m_pPropertyItem = NULL;
  170. }
  171. m_nFrameCount = 0;
  172. m_nFramePosition = 0;
  173. }
  174. void CGifAnimUI::OnTimer( UINT_PTR idEvent )
  175. {
  176. if ( idEvent != EVENT_TIEM_ID )
  177. return;
  178. m_pManager->KillTimer( this, EVENT_TIEM_ID );
  179. this->Invalidate();
  180. m_nFramePosition = (++m_nFramePosition) % m_nFrameCount;
  181. long lPause = ((long*) m_pPropertyItem->value)[m_nFramePosition] * 10;
  182. if ( lPause == 0 ) lPause = 100;
  183. m_pManager->SetTimer( this, EVENT_TIEM_ID, lPause );
  184. }
  185. void CGifAnimUI::DrawFrame( HDC hDC )
  186. {
  187. if ( NULL == hDC || NULL == m_pGifImage ) return;
  188. GUID pageGuid = Gdiplus::FrameDimensionTime;
  189. Gdiplus::Graphics graphics( hDC );
  190. graphics.DrawImage( m_pGifImage, m_rcItem.left, m_rcItem.top, m_rcItem.right-m_rcItem.left, m_rcItem.bottom-m_rcItem.top );
  191. m_pGifImage->SelectActiveFrame( &pageGuid, m_nFramePosition );
  192. }
  193. }