UIRollText.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "stdafx.h"
  2. #include "UIRollText.h"
  3. namespace DuiLib
  4. {
  5. IMPLEMENT_DUICONTROL(CRollTextUI)
  6. CRollTextUI::CRollTextUI(void)
  7. {
  8. m_nScrollPos = 0;
  9. m_nText_W_H = 0;
  10. m_nStep = 5;
  11. m_bUseRoll = FALSE;
  12. m_nRollDirection = ROLLTEXT_LEFT;
  13. }
  14. CRollTextUI::~CRollTextUI(void)
  15. {
  16. m_pManager->KillTimer(this, ROLLTEXT_ROLL_END);
  17. m_pManager->KillTimer(this, ROLLTEXT_TIMERID);
  18. }
  19. LPCTSTR CRollTextUI::GetClass() const
  20. {
  21. return _T("RollTextUI");
  22. }
  23. LPVOID CRollTextUI::GetInterface(LPCTSTR pstrName)
  24. {
  25. if( _tcsicmp(pstrName, _T("RollText")) == 0 ) return static_cast<CRollTextUI*>(this);
  26. return CLabelUI::GetInterface(pstrName);
  27. }
  28. void CRollTextUI::BeginRoll(int nDirect, LONG lTimeSpan, LONG lMaxTimeLimited)
  29. {
  30. m_nRollDirection = nDirect;
  31. if (m_bUseRoll)
  32. {
  33. EndRoll();
  34. }
  35. m_nText_W_H = 0;
  36. m_pManager->KillTimer(this, ROLLTEXT_TIMERID);
  37. m_pManager->SetTimer(this, ROLLTEXT_TIMERID, lTimeSpan);
  38. m_pManager->KillTimer(this, ROLLTEXT_ROLL_END);
  39. m_pManager->SetTimer(this, ROLLTEXT_ROLL_END, lMaxTimeLimited*1000);
  40. m_bUseRoll = TRUE;
  41. }
  42. void CRollTextUI::EndRoll()
  43. {
  44. if (!m_bUseRoll) return;
  45. m_pManager->KillTimer(this, ROLLTEXT_ROLL_END);
  46. m_pManager->KillTimer(this, ROLLTEXT_TIMERID);
  47. m_bUseRoll = FALSE;
  48. }
  49. void CRollTextUI::SetPos(RECT rc)
  50. {
  51. CLabelUI::SetPos(rc);
  52. m_nText_W_H = 0; //布局变化重新计算
  53. }
  54. void CRollTextUI::SetText( LPCTSTR pstrText )
  55. {
  56. CLabelUI::SetText(pstrText);
  57. m_nText_W_H = 0; //文本变化重新计算
  58. }
  59. void CRollTextUI::DoEvent(TEventUI& event)
  60. {
  61. if (event.Type == UIEVENT_TIMER && event.wParam == ROLLTEXT_ROLL_END)
  62. {
  63. m_pManager->KillTimer(this, ROLLTEXT_ROLL_END);
  64. m_pManager->SendNotify(this, DUI_MSGTYPE_TEXTROLLEND);
  65. }
  66. else if( event.Type == UIEVENT_TIMER && event.wParam == ROLLTEXT_TIMERID )
  67. {
  68. Invalidate();
  69. return;
  70. }
  71. CLabelUI::DoEvent(event);
  72. }
  73. void CRollTextUI::PaintText(HDC hDC)
  74. {
  75. if( m_dwTextColor == 0 ) m_dwTextColor = m_pManager->GetDefaultFontColor();
  76. if( m_dwDisabledTextColor == 0 ) m_dwDisabledTextColor = m_pManager->GetDefaultDisabledColor();
  77. DWORD dwTextColor = IsEnabled() ? m_dwTextColor : m_dwDisabledTextColor;
  78. CDuiString sText = GetText();
  79. if( sText.IsEmpty() ) return;
  80. RECT rcTextPadding = GetTextPadding();
  81. CDuiRect rcClient;
  82. rcClient = m_rcItem;
  83. rcClient.left += rcTextPadding.left;
  84. rcClient.right -= rcTextPadding.right;
  85. rcClient.top += rcTextPadding.top;
  86. rcClient.bottom -= rcTextPadding.bottom;
  87. if(m_nText_W_H > 0)
  88. {
  89. int nScrollRange = 0;
  90. if (m_nRollDirection == ROLLTEXT_LEFT || m_nRollDirection == ROLLTEXT_RIGHT) { //左面移动
  91. nScrollRange = m_nText_W_H + rcClient.GetWidth();
  92. rcClient.Offset((m_nRollDirection == ROLLTEXT_LEFT?rcClient.GetWidth():-rcClient.GetWidth()), 0);
  93. rcClient.Offset((m_nRollDirection == ROLLTEXT_LEFT?-m_nScrollPos:m_nScrollPos), 0);
  94. rcClient.right += (m_nText_W_H - rcClient.GetWidth());
  95. }
  96. else { //上下移动
  97. nScrollRange = m_nText_W_H + rcClient.GetHeight();
  98. rcClient.Offset(0, (m_nRollDirection == ROLLTEXT_UP?rcClient.GetHeight():-rcClient.GetHeight()));
  99. rcClient.Offset(0, (m_nRollDirection == ROLLTEXT_UP?-m_nScrollPos:m_nScrollPos));
  100. rcClient.bottom += (m_nText_W_H - rcClient.GetHeight());
  101. }
  102. m_nScrollPos += m_nStep;
  103. if (m_nScrollPos > nScrollRange) {
  104. m_nScrollPos = 0;
  105. }
  106. }
  107. RECT rc = rcClient;
  108. UINT uTextStyle = DT_WORDBREAK | DT_EDITCONTROL;
  109. if(m_nText_W_H == 0) {
  110. uTextStyle |= DT_CALCRECT; //第一次计算文本宽度或高度
  111. if (m_nRollDirection == ROLLTEXT_LEFT || m_nRollDirection == ROLLTEXT_RIGHT) { //左面移动
  112. rc.right += 10000;
  113. }
  114. else { //上下移动
  115. rc.bottom += 10000;
  116. }
  117. }
  118. if( m_bShowHtml ) {
  119. int nLinks = 0;
  120. CRenderEngine::DrawHtmlText(hDC, m_pManager, rc, sText, dwTextColor, NULL, NULL, nLinks, m_iFont, uTextStyle);
  121. }
  122. else {
  123. CRenderEngine::DrawText(hDC, m_pManager, rc, sText, dwTextColor, m_iFont, uTextStyle);
  124. }
  125. if(m_nText_W_H == 0) {
  126. m_nText_W_H = (m_nRollDirection == ROLLTEXT_LEFT || m_nRollDirection == ROLLTEXT_RIGHT)?(rc.right - rc.left):(rc.bottom - rc.top); //计算文本宽度或高度
  127. }
  128. }
  129. }