UIProgress.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "stdafx.h"
  2. #include "UIProgress.h"
  3. namespace DuiLib
  4. {
  5. CProgressUI::CProgressUI() : m_bHorizontal(true), m_nMin(0), m_nMax(100), m_nValue(0)
  6. {
  7. m_uTextStyle = DT_SINGLELINE | DT_CENTER;
  8. SetFixedHeight(12);
  9. }
  10. LPCTSTR CProgressUI::GetClass() const
  11. {
  12. return DUI_CTR_PROGRESS;
  13. }
  14. LPVOID CProgressUI::GetInterface(LPCTSTR pstrName)
  15. {
  16. if( _tcscmp(pstrName, DUI_CTR_PROGRESS) == 0 ) return static_cast<CProgressUI*>(this);
  17. return CLabelUI::GetInterface(pstrName);
  18. }
  19. bool CProgressUI::IsHorizontal()
  20. {
  21. return m_bHorizontal;
  22. }
  23. void CProgressUI::SetHorizontal(bool bHorizontal)
  24. {
  25. if( m_bHorizontal == bHorizontal ) return;
  26. m_bHorizontal = bHorizontal;
  27. Invalidate();
  28. }
  29. int CProgressUI::GetMinValue() const
  30. {
  31. return m_nMin;
  32. }
  33. void CProgressUI::SetMinValue(int nMin)
  34. {
  35. m_nMin = nMin;
  36. Invalidate();
  37. }
  38. int CProgressUI::GetMaxValue() const
  39. {
  40. return m_nMax;
  41. }
  42. void CProgressUI::SetMaxValue(int nMax)
  43. {
  44. m_nMax = nMax;
  45. Invalidate();
  46. }
  47. int CProgressUI::GetValue() const
  48. {
  49. return m_nValue;
  50. }
  51. void CProgressUI::SetValue(int nValue)
  52. {
  53. m_nValue = nValue;
  54. if (m_nValue > m_nMax) m_nValue = m_nMax;
  55. if (m_nValue < m_nMin) m_nValue = m_nMin;
  56. Invalidate();
  57. }
  58. LPCTSTR CProgressUI::GetForeImage() const
  59. {
  60. return m_diFore.sDrawString;
  61. }
  62. void CProgressUI::SetForeImage(LPCTSTR pStrImage)
  63. {
  64. if( m_diFore.sDrawString == pStrImage && m_diFore.pImageInfo != NULL ) return;
  65. m_diFore.Clear();
  66. m_diFore.sDrawString = pStrImage;
  67. Invalidate();
  68. }
  69. void CProgressUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
  70. {
  71. if( _tcscmp(pstrName, _T("foreimage")) == 0 ) SetForeImage(pstrValue);
  72. else if( _tcscmp(pstrName, _T("hor")) == 0 ) SetHorizontal(_tcscmp(pstrValue, _T("true")) == 0);
  73. else if( _tcscmp(pstrName, _T("min")) == 0 ) SetMinValue(_ttoi(pstrValue));
  74. else if( _tcscmp(pstrName, _T("max")) == 0 ) SetMaxValue(_ttoi(pstrValue));
  75. else if( _tcscmp(pstrName, _T("value")) == 0 ) SetValue(_ttoi(pstrValue));
  76. else CLabelUI::SetAttribute(pstrName, pstrValue);
  77. }
  78. void CProgressUI::PaintStatusImage(HDC hDC)
  79. {
  80. if( m_nMax <= m_nMin ) m_nMax = m_nMin + 1;
  81. if( m_nValue > m_nMax ) m_nValue = m_nMax;
  82. if( m_nValue < m_nMin ) m_nValue = m_nMin;
  83. RECT rc = {0};
  84. if( m_bHorizontal ) {
  85. rc.right = (m_nValue - m_nMin) * (m_rcItem.right - m_rcItem.left) / (m_nMax - m_nMin);
  86. rc.bottom = m_rcItem.bottom - m_rcItem.top;
  87. }
  88. else {
  89. rc.top = (m_rcItem.bottom - m_rcItem.top) * (m_nMax - m_nValue) / (m_nMax - m_nMin);
  90. rc.right = m_rcItem.right - m_rcItem.left;
  91. rc.bottom = m_rcItem.bottom - m_rcItem.top;
  92. }
  93. m_diFore.rcDestOffset = rc;
  94. if( DrawImage(hDC, m_diFore) ) return;
  95. }
  96. }