UIGroupBox.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include "StdAfx.h"
  2. #include "UIGroupBox.h"
  3. namespace DuiLib
  4. {
  5. IMPLEMENT_DUICONTROL(CGroupBoxUI)
  6. //////////////////////////////////////////////////////////////////////////
  7. //
  8. CGroupBoxUI::CGroupBoxUI(): m_uTextStyle(DT_SINGLELINE | DT_VCENTER | DT_CENTER), m_dwTextColor(0),
  9. m_dwDisabledTextColor(0), m_iFont(-1)
  10. {
  11. SetInset(CDuiRect(20, 25, 20, 20));
  12. }
  13. CGroupBoxUI::~CGroupBoxUI()
  14. {
  15. }
  16. LPCTSTR CGroupBoxUI::GetClass() const
  17. {
  18. return _T("GroupBoxUI");
  19. }
  20. LPVOID CGroupBoxUI::GetInterface(LPCTSTR pstrName)
  21. {
  22. if( _tcsicmp(pstrName, _T("GroupBox")) == 0 ) return static_cast<CGroupBoxUI*>(this);
  23. return CVerticalLayoutUI::GetInterface(pstrName);
  24. }
  25. void CGroupBoxUI::SetTextColor(DWORD dwTextColor)
  26. {
  27. m_dwTextColor = dwTextColor;
  28. Invalidate();
  29. }
  30. DWORD CGroupBoxUI::GetTextColor() const
  31. {
  32. return m_dwTextColor;
  33. }
  34. void CGroupBoxUI::SetDisabledTextColor(DWORD dwTextColor)
  35. {
  36. m_dwDisabledTextColor = dwTextColor;
  37. Invalidate();
  38. }
  39. DWORD CGroupBoxUI::GetDisabledTextColor() const
  40. {
  41. return m_dwDisabledTextColor;
  42. }
  43. void CGroupBoxUI::SetFont(int index)
  44. {
  45. m_iFont = index;
  46. Invalidate();
  47. }
  48. int CGroupBoxUI::GetFont() const
  49. {
  50. return m_iFont;
  51. }
  52. void CGroupBoxUI::PaintText(HDC hDC)
  53. {
  54. CDuiString sText = GetText();
  55. if( sText.IsEmpty() ) {
  56. return;
  57. }
  58. if( m_dwTextColor == 0 ) m_dwTextColor = m_pManager->GetDefaultFontColor();
  59. if( m_dwDisabledTextColor == 0 ) m_dwDisabledTextColor = m_pManager->GetDefaultDisabledColor();
  60. if( sText.IsEmpty() ) return;
  61. CDuiRect rcText = m_rcItem;
  62. rcText.Deflate(5,5);
  63. SIZE szAvailable = { rcText.right - rcText.left, rcText.bottom - rcText.top };
  64. SIZE sz = CalcrectSize(szAvailable);
  65. //计算文字区域
  66. rcText.left = rcText.left + 15;
  67. rcText.top = rcText.top - 5;
  68. rcText.right = rcText.left + sz.cx;
  69. rcText.bottom = rcText.top + sz.cy;
  70. DWORD dwTextColor = m_dwTextColor;
  71. if(!IsEnabled()) dwTextColor = m_dwDisabledTextColor;
  72. CRenderEngine::DrawText(hDC, m_pManager, rcText, sText, dwTextColor, m_iFont, m_uTextStyle, GetAdjustColor(m_dwBackColor));
  73. }
  74. void CGroupBoxUI::PaintBorder(HDC hDC)
  75. {
  76. int nBorderSize;
  77. SIZE cxyBorderRound;
  78. RECT rcBorderSize;
  79. if (m_pManager) {
  80. nBorderSize = GetManager()->GetDPIObj()->Scale(m_nBorderSize);
  81. cxyBorderRound = GetManager()->GetDPIObj()->Scale(m_cxyBorderRound);
  82. rcBorderSize = GetManager()->GetDPIObj()->Scale(m_rcBorderSize);
  83. }
  84. else {
  85. nBorderSize = m_nBorderSize;
  86. cxyBorderRound = m_cxyBorderRound;
  87. rcBorderSize = m_rcBorderSize;
  88. }
  89. if( nBorderSize > 0 )
  90. {
  91. CDuiRect rcItem = m_rcItem;
  92. rcItem.Deflate(5, 5);
  93. if( cxyBorderRound.cx > 0 || cxyBorderRound.cy > 0 )//画圆角边框
  94. {
  95. if (IsFocused() && m_dwFocusBorderColor != 0)
  96. CRenderEngine::DrawRoundRect(hDC, rcItem, nBorderSize, cxyBorderRound.cx, cxyBorderRound.cy, GetAdjustColor(m_dwFocusBorderColor));
  97. else
  98. CRenderEngine::DrawRoundRect(hDC, rcItem, nBorderSize, cxyBorderRound.cx, cxyBorderRound.cy, GetAdjustColor(m_dwBorderColor));
  99. }
  100. else
  101. {
  102. if (IsFocused() && m_dwFocusBorderColor != 0)
  103. CRenderEngine::DrawRect(hDC, rcItem, nBorderSize, GetAdjustColor(m_dwFocusBorderColor));
  104. else
  105. CRenderEngine::DrawRect(hDC, rcItem, nBorderSize, GetAdjustColor(m_dwBorderColor));
  106. }
  107. }
  108. PaintText(hDC);
  109. }
  110. SIZE CGroupBoxUI::CalcrectSize(SIZE szAvailable)
  111. {
  112. SIZE cxyFixed = GetFixedXY();
  113. RECT rcText = { 0, 0, MAX(szAvailable.cx, cxyFixed.cx), 20 };
  114. CDuiString sText = GetText();
  115. CRenderEngine::DrawText(m_pManager->GetPaintDC(), m_pManager, rcText, sText, m_dwTextColor, m_iFont, DT_CALCRECT | m_uTextStyle);
  116. SIZE cXY = {rcText.right - rcText.left, rcText.bottom - rcText.top};
  117. return cXY;
  118. }
  119. void CGroupBoxUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
  120. {
  121. if( _tcsicmp(pstrName, _T("textcolor")) == 0 )
  122. {
  123. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  124. LPTSTR pstr = NULL;
  125. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  126. SetTextColor(clrColor);
  127. }
  128. else if( _tcsicmp(pstrName, _T("disabledtextcolor")) == 0 )
  129. {
  130. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  131. LPTSTR pstr = NULL;
  132. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  133. SetDisabledTextColor(clrColor);
  134. }
  135. else if( _tcsicmp(pstrName, _T("font")) == 0 )
  136. {
  137. SetFont(_ttoi(pstrValue));
  138. }
  139. CVerticalLayoutUI::SetAttribute(pstrName, pstrValue);
  140. }
  141. }