当前位置:   article > 正文

DuiLib::CLabelUI 的继承类_duilib lableui

duilib lableui

需求 :  要弹出一个文本提示对话框,替代 ::MessageBox

思路 :  用DuiDesigner画一个对话框,带上一张背景图, 放上按钮和Label, Label上显示文本信息.

效果:  发现文本不能换行, 只能显示一行.

纠正 :

  去DuiLib中看到,实际最后画文本的时候,都加了单行类型, 就是说, 最后只能以单行方式画文本.

  去网上看了下,cppblog的一个人,直接在CLabelUI中改了人家代码. 虽然简单,但是这库,别人就没法用了.

  我做了最小化修改, 从CLabelUI继承了一个类CLabelUIEx,重载 PaintText,

  这样,我自己用时, 从CLabelUIEx继承控件.

  别人用时, 还从CLabelUI继承, 和原来的库一样效果.


  因为 CDialogBuilder 负责创建控件, 修改了CDialogBuilder, 使其生成新的控件类型 LabelEx.

  最后能换行了, 但是只能从给定矩形的左上角开始, 就不修改了.

  修改后的类,用于显示一些活动的文本。比较固定的文本,用图片直接显示.

  毕竟由专业美工作出的图,比自己写的字,效果要好的多.


 如果是固定的字符串比较多,需要加很多图片的话,也可以考虑用此类显示提示信息.

  权衡显示效果和程序的体积.

 

  只修改了DuiLib.

  DuiDesigner没改, 在编辑xml时,将LabelEx改成Label, 修改完后, 再改回来, 打成skin.zip包.

 

  本来不该改界面库的, 被迫的.

 

  1. ///
  2. //END消息映射宏定义
  3. //BEGIN控件名称宏定义//
  4. ///
  5. #define DUI_CTR_EDIT (_T("Edit"))
  6. #define DUI_CTR_LIST (_T("List"))
  7. #define DUI_CTR_TEXT (_T("Text"))
  8. #define DUI_CTR_COMBO (_T("Combo"))
  9. #define DUI_CTR_LABEL (_T("Label"))
  10. #define DUI_CTR_LABEL_EX (_T("LabelEx")) ///< @todo
  11. #define DUI_CTR_FLASH (_T("Flash"))

  1. CControlUI* CDialogBuilder::_Parse(CMarkupNode* pRoot, CControlUI* pParent, CPaintManagerUI* pManager)
  2. {
  3. IContainerUI* pContainer = NULL;
  4. CControlUI* pReturn = NULL;

...

  1. case 6:
  2. if( _tcscmp(pstrClass, DUI_CTR_BUTTON) == 0 ) pControl = new CButtonUI;
  3. else if( _tcscmp(pstrClass, DUI_CTR_OPTION) == 0 ) pControl = new COptionUI;
  4. else if( _tcscmp(pstrClass, DUI_CTR_SLIDER) == 0 ) pControl = new CSliderUI;
  5. break;
  6. case 7:
  7. if( _tcscmp(pstrClass, DUI_CTR_CONTROL) == 0 ) pControl = new CControlUI;
  8. else if( _tcscmp(pstrClass, DUI_CTR_ACTIVEX) == 0 ) pControl = new CActiveXUI;
  9. else if (0 == _tcscmp(pstrClass, DUI_CTR_LABEL_EX)) ///< @todo
  10. {
  11. pControl = new CLabelUIEx;
  12. }
  13. break;
  14. case 8:

  1. /// @file UILabelEx.h
  2. /// @brief 支持自动换行的LabelUI,
  3. /// 修改了 DuiLib::CLabelUI::PaintText 为虚函数, LabelUIEx继承自CLabelUI
  4. /// 重载了 virtual void PaintText(HDC hDC);
  5. #ifndef __UI_LABEL_EX_H__
  6. #define __UI_LABEL_EX_H__
  7. namespace DuiLib
  8. {
  9. class UILIB_API CLabelUIEx :
  10. public DuiLib::CLabelUI
  11. {
  12. public:
  13. CLabelUIEx(void);
  14. virtual ~CLabelUIEx(void);
  15. virtual void PaintText(HDC hDC);
  16. virtual void SetPos(RECT rc);
  17. private:
  18. Color _MakeRGB(int a, Color cl);
  19. Color _MakeRGB(int r, int g, int b);
  20. };
  21. }
  22. #endif

  1. #include "StdAfx.h"
  2. #include "UILabelEx.h"
  3. namespace DuiLib
  4. {
  5. CLabelUIEx::CLabelUIEx(void)
  6. {
  7. }
  8. CLabelUIEx::~CLabelUIEx(void)
  9. {
  10. }
  11. Color CLabelUIEx::_MakeRGB(int a, Color cl)
  12. {
  13. return Color(a, cl.GetR(), cl.GetG(), cl.GetB());
  14. }
  15. Color CLabelUIEx::_MakeRGB(int r, int g, int b)
  16. {
  17. return Color(255, r, g, b);
  18. }
  19. void CLabelUIEx::SetPos(RECT rc)
  20. {
  21. __super::SetPos(rc);
  22. }
  23. void CLabelUIEx::PaintText(HDC hDC)
  24. {
  25. UINT uStyle = GetTextStyle();
  26. if( m_dwTextColor == 0 ) m_dwTextColor = m_pManager->GetDefaultFontColor();
  27. if( m_dwDisabledTextColor == 0 ) m_dwDisabledTextColor = m_pManager->GetDefaultDisabledColor();
  28. RECT rc = m_rcItem;
  29. rc.left += m_rcTextPadding.left;
  30. rc.right -= m_rcTextPadding.right;
  31. rc.top += m_rcTextPadding.top;
  32. rc.bottom -= m_rcTextPadding.bottom;
  33. if(!GetEnabledEffect())
  34. {
  35. if( m_sText.IsEmpty() ) return;
  36. int nLinks = 0;
  37. if( IsEnabled() ) {
  38. if( m_bShowHtml )
  39. CRenderEngine::DrawHtmlText(hDC, m_pManager, rc, m_sText, m_dwTextColor, \
  40. NULL, NULL, nLinks, DT_SINGLELINE | m_uTextStyle);
  41. else
  42. CRenderEngine::DrawText(hDC, m_pManager, rc, m_sText, m_dwTextColor, \
  43. m_iFont, /* DT_SINGLELINE | */m_uTextStyle); ///< @todo
  44. }
  45. else {
  46. if( m_bShowHtml )
  47. CRenderEngine::DrawHtmlText(hDC, m_pManager, rc, m_sText, m_dwDisabledTextColor, \
  48. NULL, NULL, nLinks, DT_SINGLELINE | m_uTextStyle);
  49. else
  50. CRenderEngine::DrawText(hDC, m_pManager, rc, m_sText, m_dwDisabledTextColor, \
  51. m_iFont, /* DT_SINGLELINE | */m_uTextStyle); ///< @todo
  52. }
  53. }
  54. else
  55. {
  56. Font nFont(hDC,m_pManager->GetFont(GetFont()));
  57. Graphics nGraphics(hDC);
  58. nGraphics.SetTextRenderingHint(m_TextRenderingHintAntiAlias);
  59. StringFormat format;
  60. format.SetAlignment((StringAlignment)m_hAlign);
  61. format.SetLineAlignment((StringAlignment)m_vAlign);
  62. RectF nRc((float)rc.left,(float)rc.top,(float)rc.right-rc.left,(float)rc.bottom-rc.top);
  63. RectF nShadowRc = nRc;
  64. nShadowRc.X += m_ShadowOffset.X;
  65. nShadowRc.Y += m_ShadowOffset.Y;
  66. int nGradientLength = GetGradientLength();
  67. if(nGradientLength == 0)
  68. nGradientLength = (rc.bottom-rc.top);
  69. LinearGradientBrush nLineGrBrushA(Point(GetGradientAngle(), 0),Point(0,nGradientLength),_MakeRGB(GetTransShadow(),GetTextShadowColorA()),_MakeRGB(GetTransShadow1(),GetTextShadowColorB() == -1?GetTextShadowColorA():GetTextShadowColorB()));
  70. LinearGradientBrush nLineGrBrushB(Point(GetGradientAngle(), 0),Point(0,nGradientLength),_MakeRGB(GetTransText(),GetTextColor()),_MakeRGB(GetTransText1(),GetTextColor1() == -1?GetTextColor():GetTextColor1()));
  71. if(GetEnabledStroke() && GetStrokeColor() > 0)
  72. {
  73. LinearGradientBrush nLineGrBrushStroke(Point(GetGradientAngle(),0),Point(0,rc.bottom-rc.top+2),_MakeRGB(GetTransStroke(),GetStrokeColor()),_MakeRGB(GetTransStroke(),GetStrokeColor()));
  74. #ifdef _UNICODE
  75. nRc.Offset(-1,0);
  76. nGraphics.DrawString(m_TextValue,m_TextValue.GetLength(),&nFont,nRc,&format,&nLineGrBrushStroke);
  77. nRc.Offset(2,0);
  78. nGraphics.DrawString(m_TextValue,m_TextValue.GetLength(),&nFont,nRc,&format,&nLineGrBrushStroke);
  79. nRc.Offset(-1,-1);
  80. nGraphics.DrawString(m_TextValue,m_TextValue.GetLength(),&nFont,nRc,&format,&nLineGrBrushStroke);
  81. nRc.Offset(0,2);
  82. nGraphics.DrawString(m_TextValue,m_TextValue.GetLength(),&nFont,nRc,&format,&nLineGrBrushStroke);
  83. nRc.Offset(0,-1);
  84. #else
  85. USES_CONVERSION;
  86. wstring mTextValue = A2W(m_TextValue.GetData());
  87. nRc.Offset(-1,0);
  88. nGraphics.DrawString(mTextValue.c_str(),mTextValue.length(),&nFont,nRc,&format,&nLineGrBrushStroke);
  89. nRc.Offset(2,0);
  90. nGraphics.DrawString(mTextValue.c_str(),mTextValue.length(),&nFont,nRc,&format,&nLineGrBrushStroke);
  91. nRc.Offset(-1,-1);
  92. nGraphics.DrawString(mTextValue.c_str(),mTextValue.length(),&nFont,nRc,&format,&nLineGrBrushStroke);
  93. nRc.Offset(0,2);
  94. nGraphics.DrawString(mTextValue.c_str(),mTextValue.length(),&nFont,nRc,&format,&nLineGrBrushStroke);
  95. nRc.Offset(0,-1);
  96. #endif
  97. }
  98. #ifdef _UNICODE
  99. if(GetEnabledShadow() && (GetTextShadowColorA() > 0 || GetTextShadowColorB() > 0))
  100. nGraphics.DrawString(m_TextValue,m_TextValue.GetLength(),&nFont,nShadowRc,&format,&nLineGrBrushA);
  101. nGraphics.DrawString(m_TextValue,m_TextValue.GetLength(),&nFont,nRc,&format,&nLineGrBrushB);
  102. #else
  103. USES_CONVERSION;
  104. wstring mTextValue = A2W(m_TextValue.GetData());
  105. if(GetEnabledShadow() && (GetTextShadowColorA() > 0 || GetTextShadowColorB() > 0))
  106. nGraphics.DrawString(mTextValue.c_str(),mTextValue.length(),&nFont,nShadowRc,&format,&nLineGrBrushA);
  107. nGraphics.DrawString(mTextValue.c_str(),mTextValue.length(),&nFont,nRc,&format,&nLineGrBrushB);
  108. #endif
  109. }
  110. }
  111. }

调用示例

  1. void CTextMsgDlg::UiInit()
  2. {
  3. UINT uStyle = 0;
  4. m_pLabelMsg = static_cast<CLabelUIEx *>(m_PaintManager.FindControl(ELEMENT_TEXT_DLG_LABEL_MSG));
  5. if (NULL != m_pLabelMsg)
  6. {
  7. m_pLabelMsg->SetText(m_strMsg.c_str());
  8. uStyle = m_pLabelMsg->GetTextStyle();
  9. uStyle |= DT_WORDBREAK;
  10. uStyle &= (~DT_CENTER);
  11. m_pLabelMsg->SetTextStyle(uStyle /*DT_CENTER | DT_VCENTER | DT_WORDBREAK | DT_WORD_ELLIPSIS */);
  12. }
  13. }

2017-0121

yiluanlie9944@csdn问起有没有这个类的实现工程,我上传的Demo中应该都有吧.
找了一个已经上传的Demo, 里面有这个类的实现.
http://download.csdn.net/download/lostspeed/9042237
<<srcbk_2015_0824_1711_prj_dlg_show_list_ui_use_massive_amounts_of_data.zip>>


PS: 找资料的方法, 如果人家在这篇文章之后,写的同系列的Demo, 很有可能有这篇文章的内容.
先下载作者在这篇文章之后的Demo翻翻,看看有没有这篇文章描述的内容:)


 







 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/348767
推荐阅读
相关标签
  

闽ICP备14008679号