UILoading.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #include "stdafx.h"
  2. #include "UILoading.h"
  3. #include <Gdiplus.h>
  4. using namespace DuiLib;
  5. using namespace Gdiplus;
  6. namespace
  7. {
  8. void DrawLine(Graphics* _objGraphics, PointF _objPointOne, PointF _objPointTwo, Color _objColor, int _intLineThickness)
  9. {
  10. SolidBrush brush(_objColor);
  11. Pen objPen(&brush, (Gdiplus::REAL)_intLineThickness);
  12. {
  13. objPen.SetStartCap(LineCap::LineCapRound);
  14. objPen.SetEndCap(LineCap::LineCapRound);
  15. _objGraphics->DrawLine(&objPen, _objPointOne, _objPointTwo);
  16. }
  17. }
  18. PointF GetCoordinate(PointF _objCircleCenter, int _intRadius, double _dblAngle)
  19. {
  20. double dblAngle = 3.14 * _dblAngle / 180;
  21. PointF pf(_objCircleCenter.X + _intRadius * (float)cos(dblAngle), _objCircleCenter.Y + _intRadius * (float)sin(dblAngle));
  22. return pf;
  23. }
  24. Color Darken(Color _objColor, int _intPercent)
  25. {
  26. int intRed = _objColor.GetR();
  27. int intGreen = _objColor.GetG();
  28. int intBlue = _objColor.GetB();
  29. Color color(_intPercent, min(intRed, 255), min(intGreen, 255), min(intBlue, 255));
  30. return color;
  31. }
  32. double* GetSpokesAngles(int _intNumberSpoke)
  33. {
  34. double* Angles = new double[_intNumberSpoke];
  35. double dblAngle = (double)360 / _intNumberSpoke;
  36. for (int shtCounter = 0; shtCounter < _intNumberSpoke; shtCounter++)
  37. {
  38. Angles[shtCounter] = (shtCounter == 0 ? dblAngle : Angles[shtCounter - 1] + dblAngle);
  39. }
  40. return Angles;
  41. }
  42. }
  43. IMPLEMENT_DUICONTROL(CLoadingUI)
  44. CLoadingUI::CLoadingUI()
  45. : m_bStop(true)
  46. , m_nTime(100)
  47. , m_nNumber(0)
  48. , m_Angles(nullptr)
  49. , m_Colors(nullptr)
  50. , m_NumberOfSpoke(10)
  51. , m_SpokeThickness(4)
  52. , m_OuterCircleRadius( 10)
  53. , m_InnerCircleRadius (8)
  54. {
  55. }
  56. CLoadingUI::~CLoadingUI()
  57. {
  58. Stop();
  59. if(m_Angles) delete m_Angles;
  60. if(m_Colors) delete m_Colors;
  61. }
  62. LPCTSTR CLoadingUI::GetClass() const
  63. {
  64. return DUI_CTR_LOADINGCIRCLE;
  65. }
  66. LPVOID CLoadingUI::GetInterface(LPCTSTR pstrName)
  67. {
  68. if (_tcscmp(pstrName, DUI_CTR_LOADINGCIRCLE) == 0) return static_cast<CLoadingUI*>(this);
  69. return CControlUI::GetInterface(pstrName);
  70. }
  71. void CLoadingUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
  72. {
  73. if (_tcsicmp(pstrName, _T("style") ) == 0)
  74. {
  75. LPTSTR pstr = NULL;
  76. m_nNumber = _tcstol(pstrValue, &pstr, 10);
  77. }
  78. else if (_tcsicmp(pstrName, _T("time") ) == 0)
  79. {
  80. LPTSTR pstr = NULL;
  81. m_nTime = _tcstol(pstrValue, &pstr, 10);
  82. }
  83. else if (_tcsicmp(pstrName, _T("spoke") ) == 0)
  84. {
  85. LPTSTR pstr = NULL;
  86. m_NumberOfSpoke = _tcstol(pstrValue, &pstr, 0);
  87. }
  88. else if (_tcsicmp(pstrName, _T("thickness")) == 0)
  89. {
  90. LPTSTR pstr = NULL;
  91. m_SpokeThickness = _tcstol(pstrValue, &pstr, 0);
  92. }
  93. else if (_tcsicmp(pstrName, _T("outradius")) == 0)
  94. {
  95. LPTSTR pstr = NULL;
  96. m_OuterCircleRadius = _tcstol(pstrValue, &pstr, 0);
  97. }
  98. else if (_tcsicmp(pstrName, _T("innerradius")) == 0)
  99. {
  100. LPTSTR pstr = NULL;
  101. m_InnerCircleRadius = _tcstol(pstrValue, &pstr, 0);
  102. }
  103. else if (_tcsicmp(pstrName, _T("color")) == 0)
  104. {
  105. while (*pstrValue > _T('\0') && *pstrValue <= _T(' ')) pstrValue = ::CharNext(pstrValue);
  106. if (*pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  107. LPTSTR pstr = NULL;
  108. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  109. m_Color.SetValue(clrColor);
  110. }
  111. else
  112. {
  113. __super::SetAttribute(pstrName, pstrValue);
  114. }
  115. }
  116. void CLoadingUI::PaintBkImage(HDC hDC)
  117. {
  118. m_CenterPoint.X = (Gdiplus::REAL)this->GetWidth() / 2;
  119. m_CenterPoint.Y = (Gdiplus::REAL)this->GetHeight() / 2;
  120. if (m_NumberOfSpoke > 0)
  121. {
  122. Image* img = new Bitmap(this->GetWidth(), this->GetHeight());
  123. Graphics g(img);
  124. g.SetSmoothingMode(SmoothingMode::SmoothingModeHighQuality);
  125. int intPosition = m_ProgressValue;
  126. for (int intCounter = 0; intCounter < m_NumberOfSpoke; intCounter++)
  127. {
  128. intPosition = intPosition % m_NumberOfSpoke;
  129. DrawLine(&g,
  130. GetCoordinate(m_CenterPoint, m_InnerCircleRadius, m_Angles[intPosition]),
  131. GetCoordinate(m_CenterPoint, m_OuterCircleRadius, m_Angles[intPosition]),
  132. m_Colors[intCounter], m_SpokeThickness);
  133. intPosition++;
  134. }
  135. // 获得窗口的Graphics对象
  136. Graphics gh(hDC);
  137. // 将描画好的CacheImage画到窗口上
  138. gh.DrawImage(img, m_rcItem.left, m_rcItem.top);
  139. delete img;
  140. }
  141. }
  142. Color* CLoadingUI::GenerateColorsPallet(Color _objColor, bool _blnShadeColor, int _intNbSpoke)
  143. {
  144. Color* objColors = new Color[m_NumberOfSpoke];
  145. byte bytIncrement = (byte)(255 / m_NumberOfSpoke);
  146. byte PERCENTAGE_OF_DARKEN = 0;
  147. for (int intCursor = 0; intCursor < m_NumberOfSpoke; intCursor++)
  148. {
  149. if (_blnShadeColor)
  150. {
  151. if (intCursor == 0 || intCursor < m_NumberOfSpoke - _intNbSpoke)
  152. {
  153. objColors[intCursor] = _objColor;
  154. }
  155. else
  156. {
  157. PERCENTAGE_OF_DARKEN += bytIncrement;
  158. if (PERCENTAGE_OF_DARKEN > 255)
  159. {
  160. PERCENTAGE_OF_DARKEN = 255;
  161. }
  162. objColors[intCursor] = Darken(_objColor, PERCENTAGE_OF_DARKEN);
  163. }
  164. }
  165. else
  166. {
  167. objColors[intCursor] = _objColor;
  168. }
  169. }
  170. return objColors;
  171. }
  172. void CLoadingUI::Start()
  173. {
  174. if (m_nTime > 0 && m_pManager && m_bStop == true)
  175. {
  176. m_pManager->SetTimer(this, kTimerLoadingId, m_nTime);
  177. }
  178. m_bStop = false;
  179. }
  180. void CLoadingUI::Stop()
  181. {
  182. m_bStop = true;
  183. if (m_pManager)
  184. {
  185. m_pManager->KillTimer(this, kTimerLoadingId);
  186. }
  187. }
  188. void CLoadingUI::Init()
  189. {
  190. CControlUI::Init();
  191. m_Angles = GetSpokesAngles(m_NumberOfSpoke);
  192. m_Colors = GenerateColorsPallet(m_Color, true, m_NumberOfSpoke);
  193. Start();
  194. }
  195. void DuiLib::CLoadingUI::DoEvent(TEventUI& event)
  196. {
  197. if (event.Type == UIEVENT_TIMER && event.wParam == kTimerLoadingId)
  198. {
  199. m_ProgressValue = ++m_ProgressValue % m_NumberOfSpoke;
  200. Invalidate();
  201. }
  202. else
  203. {
  204. CControlUI::DoEvent(event);
  205. }
  206. }
  207. CControlUI* CreateLoadingControl(LPCTSTR pstrType)
  208. {
  209. if (_tcsicmp(pstrType, DUI_CTR_LOADINGCIRCLE) == 0)
  210. {
  211. return new CLoadingUI();
  212. }
  213. return NULL;
  214. }