UIProgress.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include "StdAfx.h"
  2. #include "UIProgress.h"
  3. namespace DuiLib
  4. {
  5. IMPLEMENT_DUICONTROL(CProgressUI)
  6. CProgressUI::CProgressUI() : m_bShowText(false), m_bHorizontal(true), m_nMin(0), m_nMax(100), m_nValue(0), m_bStretchForeImage(true)
  7. {
  8. m_uTextStyle = DT_SINGLELINE | DT_CENTER;
  9. SetFixedHeight(12);
  10. }
  11. LPCTSTR CProgressUI::GetClass() const
  12. {
  13. return _T("ProgressUI");
  14. }
  15. LPVOID CProgressUI::GetInterface(LPCTSTR pstrName)
  16. {
  17. if( _tcsicmp(pstrName, DUI_CTR_PROGRESS) == 0 ) return static_cast<CProgressUI*>(this);
  18. return CLabelUI::GetInterface(pstrName);
  19. }
  20. bool CProgressUI::IsShowText()
  21. {
  22. return m_bShowText;
  23. }
  24. void CProgressUI::SetShowText(bool bShowText)
  25. {
  26. if( m_bShowText == bShowText ) return;
  27. m_bShowText = bShowText;
  28. if(!m_bShowText) SetText(_T(""));
  29. }
  30. bool CProgressUI::IsHorizontal()
  31. {
  32. return m_bHorizontal;
  33. }
  34. void CProgressUI::SetHorizontal(bool bHorizontal)
  35. {
  36. if( m_bHorizontal == bHorizontal ) return;
  37. m_bHorizontal = bHorizontal;
  38. Invalidate();
  39. }
  40. int CProgressUI::GetMinValue() const
  41. {
  42. return m_nMin;
  43. }
  44. void CProgressUI::SetMinValue(int nMin)
  45. {
  46. m_nMin = nMin;
  47. Invalidate();
  48. }
  49. int CProgressUI::GetMaxValue() const
  50. {
  51. return m_nMax;
  52. }
  53. void CProgressUI::SetMaxValue(int nMax)
  54. {
  55. m_nMax = nMax;
  56. Invalidate();
  57. }
  58. int CProgressUI::GetValue() const
  59. {
  60. return m_nValue;
  61. }
  62. void CProgressUI::SetValue(int nValue)
  63. {
  64. if(nValue == m_nValue || nValue<m_nMin || nValue > m_nMax) {
  65. return;
  66. }
  67. m_nValue = nValue;
  68. Invalidate();
  69. UpdateText();
  70. }
  71. void CProgressUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
  72. {
  73. if( _tcsicmp(pstrName, _T("hor")) == 0 ) SetHorizontal(_tcsicmp(pstrValue, _T("true")) == 0);
  74. else if( _tcsicmp(pstrName, _T("min")) == 0 ) SetMinValue(_ttoi(pstrValue));
  75. else if( _tcsicmp(pstrName, _T("max")) == 0 ) SetMaxValue(_ttoi(pstrValue));
  76. else if( _tcsicmp(pstrName, _T("value")) == 0 ) SetValue(_ttoi(pstrValue));
  77. else if( _tcsicmp(pstrName, _T("isstretchfore"))==0) SetStretchForeImage(_tcsicmp(pstrValue, _T("true")) == 0? true : false);
  78. else if( _tcsicmp(pstrName, _T("showtext"))==0) SetShowText(_tcsicmp(pstrValue, _T("true")) == 0? true : false);
  79. else CLabelUI::SetAttribute(pstrName, pstrValue);
  80. }
  81. void CProgressUI::PaintForeColor(HDC hDC)
  82. {
  83. if(m_dwForeColor == 0) return;
  84. if( m_nMax <= m_nMin ) m_nMax = m_nMin + 1;
  85. if( m_nValue > m_nMax ) m_nValue = m_nMax;
  86. if( m_nValue < m_nMin ) m_nValue = m_nMin;
  87. RECT rc = m_rcItem;
  88. if( m_bHorizontal ) {
  89. rc.right = m_rcItem.left + (m_nValue - m_nMin) * (m_rcItem.right - m_rcItem.left) / (m_nMax - m_nMin);
  90. }
  91. else {
  92. rc.bottom = m_rcItem.top + (m_rcItem.bottom - m_rcItem.top) * (m_nMax - m_nValue) / (m_nMax - m_nMin);
  93. }
  94. CRenderEngine::DrawColor(hDC, rc, GetAdjustColor(m_dwForeColor));
  95. }
  96. void CProgressUI::PaintForeImage(HDC hDC)
  97. {
  98. if( m_nMax <= m_nMin ) m_nMax = m_nMin + 1;
  99. if( m_nValue > m_nMax ) m_nValue = m_nMax;
  100. if( m_nValue < m_nMin ) m_nValue = m_nMin;
  101. RECT rc = {0};
  102. if( m_bHorizontal ) {
  103. rc.right = (m_nValue - m_nMin) * (m_rcItem.right - m_rcItem.left) * 1.0f / (m_nMax - m_nMin);
  104. rc.bottom = m_rcItem.bottom - m_rcItem.top;
  105. }
  106. else {
  107. rc.top = (m_rcItem.bottom - m_rcItem.top) * (m_nMax - m_nValue) * 1.0f / (m_nMax - m_nMin);
  108. rc.right = m_rcItem.right - m_rcItem.left;
  109. rc.bottom = m_rcItem.bottom - m_rcItem.top;
  110. }
  111. if( !m_sForeImage.IsEmpty() ) {
  112. m_sForeImageModify.Empty();
  113. int sw = MulDiv(rc.right - rc.left, 100, GetManager()->GetDPIObj()->GetScale());
  114. int sh = MulDiv(rc.bottom - rc.top, 100, GetManager()->GetDPIObj()->GetScale());
  115. rc.left = MulDiv(rc.left, 100, GetManager()->GetDPIObj()->GetScale());
  116. rc.top = MulDiv(rc.top, 100, GetManager()->GetDPIObj()->GetScale());
  117. rc.right = rc.left + sw;
  118. rc.bottom = rc.top + sh;
  119. if (m_bStretchForeImage) {
  120. m_sForeImageModify.SmallFormat(_T("dest='%d,%d,%d,%d'"), rc.left, rc.top, rc.right, rc.bottom);
  121. }
  122. else {
  123. m_sForeImageModify.SmallFormat(_T("dest='%d,%d,%d,%d' source='%d,%d,%d,%d'"), rc.left, rc.top, rc.right, rc.bottom, rc.left, rc.top, rc.right, rc.bottom);
  124. }
  125. if( !DrawImage(hDC, (LPCTSTR)m_sForeImage, (LPCTSTR)m_sForeImageModify) ) {}
  126. else return;
  127. }
  128. }
  129. bool CProgressUI::IsStretchForeImage()
  130. {
  131. return m_bStretchForeImage;
  132. }
  133. void CProgressUI::SetStretchForeImage( bool bStretchForeImage /*= true*/ )
  134. {
  135. if (m_bStretchForeImage==bStretchForeImage) return;
  136. m_bStretchForeImage=bStretchForeImage;
  137. Invalidate();
  138. }
  139. void CProgressUI::UpdateText()
  140. {
  141. if(m_bShowText) {
  142. CDuiString sText;
  143. sText.Format(_T("%.0f%%"), (m_nValue - m_nMin) * 100.0f / (m_nMax - m_nMin));
  144. SetText(sText);
  145. }
  146. }
  147. }