需求 : 要弹出一个文本提示对话框,替代 ::MessageBox
思路 : 用DuiDesigner画一个对话框,带上一张背景图, 放上按钮和Label, Label上显示文本信息.
效果: 发现文本不能换行, 只能显示一行.
纠正 :
去DuiLib中看到,实际最后画文本的时候,都加了单行类型, 就是说, 最后只能以单行方式画文本.
去网上看了下,cppblog的一个人,直接在CLabelUI中改了人家代码. 虽然简单,但是这库,别人就没法用了.
我做了最小化修改, 从CLabelUI继承了一个类CLabelUIEx,重载 PaintText,
这样,我自己用时, 从CLabelUIEx继承控件.
别人用时, 还从CLabelUI继承, 和原来的库一样效果.
因为 CDialogBuilder 负责创建控件, 修改了CDialogBuilder, 使其生成新的控件类型 LabelEx.
最后能换行了, 但是只能从给定矩形的左上角开始, 就不修改了.
修改后的类,用于显示一些活动的文本。比较固定的文本,用图片直接显示.
毕竟由专业美工作出的图,比自己写的字,效果要好的多.
如果是固定的字符串比较多,需要加很多图片的话,也可以考虑用此类显示提示信息.
权衡显示效果和程序的体积.
只修改了DuiLib.
DuiDesigner没改, 在编辑xml时,将LabelEx改成Label, 修改完后, 再改回来, 打成skin.zip包.
本来不该改界面库的, 被迫的.
///
//END消息映射宏定义
//BEGIN控件名称宏定义//
///
#define DUI_CTR_EDIT (_T("Edit"))
#define DUI_CTR_LIST (_T("List"))
#define DUI_CTR_TEXT (_T("Text"))
#define DUI_CTR_COMBO (_T("Combo"))
#define DUI_CTR_LABEL (_T("Label"))
#define DUI_CTR_LABEL_EX (_T("LabelEx")) ///< @todo
#define DUI_CTR_FLASH (_T("Flash"))
CControlUI* CDialogBuilder::_Parse(CMarkupNode* pRoot, CControlUI* pParent, CPaintManagerUI* pManager)
{
IContainerUI* pContainer = NULL;
CControlUI* pReturn = NULL;
...
case 6:
if( _tcscmp(pstrClass, DUI_CTR_BUTTON) == 0 ) pControl = new CButtonUI;
else if( _tcscmp(pstrClass, DUI_CTR_OPTION) == 0 ) pControl = new COptionUI;
else if( _tcscmp(pstrClass, DUI_CTR_SLIDER) == 0 ) pControl = new CSliderUI;
break;
case 7:
if( _tcscmp(pstrClass, DUI_CTR_CONTROL) == 0 ) pControl = new CControlUI;
else if( _tcscmp(pstrClass, DUI_CTR_ACTIVEX) == 0 ) pControl = new CActiveXUI;
else if (0 == _tcscmp(pstrClass, DUI_CTR_LABEL_EX)) ///< @todo
{
pControl = new CLabelUIEx;
}
break;
case 8:
/// @file UILabelEx.h
/// @brief 支持自动换行的LabelUI,
/// 修改了 DuiLib::CLabelUI::PaintText 为虚函数, LabelUIEx继承自CLabelUI
/// 重载了 virtual void PaintText(HDC hDC);
#ifndef __UI_LABEL_EX_H__
#define __UI_LABEL_EX_H__
namespace DuiLib
{
class UILIB_API CLabelUIEx :
public DuiLib::CLabelUI
{
public:
CLabelUIEx(void);
virtual ~CLabelUIEx(void);
virtual void PaintText(HDC hDC);
virtual void SetPos(RECT rc);
private:
Color _MakeRGB(int a, Color cl);
Color _MakeRGB(int r, int g, int b);
};
}
#endif
#include "StdAfx.h"
#include "UILabelEx.h"
namespace DuiLib
{
CLabelUIEx::CLabelUIEx(void)
{
}
CLabelUIEx::~CLabelUIEx(void)
{
}
Color CLabelUIEx::_MakeRGB(int a, Color cl)
{
return Color(a, cl.GetR(), cl.GetG(), cl.GetB());
}
Color CLabelUIEx::_MakeRGB(int r, int g, int b)
{
return Color(255, r, g, b);
}
void CLabelUIEx::SetPos(RECT rc)
{
__super::SetPos(rc);
}
void CLabelUIEx::PaintText(HDC hDC)
{
UINT uStyle = GetTextStyle();
if( m_dwTextColor == 0 ) m_dwTextColor = m_pManager->GetDefaultFontColor();
if( m_dwDisabledTextColor == 0 ) m_dwDisabledTextColor = m_pManager->GetDefaultDisabledColor();
RECT rc = m_rcItem;
rc.left += m_rcTextPadding.left;
rc.right -= m_rcTextPadding.right;
rc.top += m_rcTextPadding.top;
rc.bottom -= m_rcTextPadding.bottom;
if(!GetEnabledEffect())
{
if( m_sText.IsEmpty() ) return;
int nLinks = 0;
if( IsEnabled() ) {
if( m_bShowHtml )
CRenderEngine::DrawHtmlText(hDC, m_pManager, rc, m_sText, m_dwTextColor, \
NULL, NULL, nLinks, DT_SINGLELINE | m_uTextStyle);
else
CRenderEngine::DrawText(hDC, m_pManager, rc, m_sText, m_dwTextColor, \
m_iFont, /* DT_SINGLELINE | */m_uTextStyle); ///< @todo
}
else {
if( m_bShowHtml )
CRenderEngine::DrawHtmlText(hDC, m_pManager, rc, m_sText, m_dwDisabledTextColor, \
NULL, NULL, nLinks, DT_SINGLELINE | m_uTextStyle);
else
CRenderEngine::DrawText(hDC, m_pManager, rc, m_sText, m_dwDisabledTextColor, \
m_iFont, /* DT_SINGLELINE | */m_uTextStyle); ///< @todo
}
}
else
{
Font nFont(hDC,m_pManager->GetFont(GetFont()));
Graphics nGraphics(hDC);
nGraphics.SetTextRenderingHint(m_TextRenderingHintAntiAlias);
StringFormat format;
format.SetAlignment((StringAlignment)m_hAlign);
format.SetLineAlignment((StringAlignment)m_vAlign);
RectF nRc((float)rc.left,(float)rc.top,(float)rc.right-rc.left,(float)rc.bottom-rc.top);
RectF nShadowRc = nRc;
nShadowRc.X += m_ShadowOffset.X;
nShadowRc.Y += m_ShadowOffset.Y;
int nGradientLength = GetGradientLength();
if(nGradientLength == 0)
nGradientLength = (rc.bottom-rc.top);
LinearGradientBrush nLineGrBrushA(Point(GetGradientAngle(), 0),Point(0,nGradientLength),_MakeRGB(GetTransShadow(),GetTextShadowColorA()),_MakeRGB(GetTransShadow1(),GetTextShadowColorB() == -1?GetTextShadowColorA():GetTextShadowColorB()));
LinearGradientBrush nLineGrBrushB(Point(GetGradientAngle(), 0),Point(0,nGradientLength),_MakeRGB(GetTransText(),GetTextColor()),_MakeRGB(GetTransText1(),GetTextColor1() == -1?GetTextColor():GetTextColor1()));
if(GetEnabledStroke() && GetStrokeColor() > 0)
{
LinearGradientBrush nLineGrBrushStroke(Point(GetGradientAngle(),0),Point(0,rc.bottom-rc.top+2),_MakeRGB(GetTransStroke(),GetStrokeColor()),_MakeRGB(GetTransStroke(),GetStrokeColor()));
#ifdef _UNICODE
nRc.Offset(-1,0);
nGraphics.DrawString(m_TextValue,m_TextValue.GetLength(),&nFont,nRc,&format,&nLineGrBrushStroke);
nRc.Offset(2,0);
nGraphics.DrawString(m_TextValue,m_TextValue.GetLength(),&nFont,nRc,&format,&nLineGrBrushStroke);
nRc.Offset(-1,-1);
nGraphics.DrawString(m_TextValue,m_TextValue.GetLength(),&nFont,nRc,&format,&nLineGrBrushStroke);
nRc.Offset(0,2);
nGraphics.DrawString(m_TextValue,m_TextValue.GetLength(),&nFont,nRc,&format,&nLineGrBrushStroke);
nRc.Offset(0,-1);
#else
USES_CONVERSION;
wstring mTextValue = A2W(m_TextValue.GetData());
nRc.Offset(-1,0);
nGraphics.DrawString(mTextValue.c_str(),mTextValue.length(),&nFont,nRc,&format,&nLineGrBrushStroke);
nRc.Offset(2,0);
nGraphics.DrawString(mTextValue.c_str(),mTextValue.length(),&nFont,nRc,&format,&nLineGrBrushStroke);
nRc.Offset(-1,-1);
nGraphics.DrawString(mTextValue.c_str(),mTextValue.length(),&nFont,nRc,&format,&nLineGrBrushStroke);
nRc.Offset(0,2);
nGraphics.DrawString(mTextValue.c_str(),mTextValue.length(),&nFont,nRc,&format,&nLineGrBrushStroke);
nRc.Offset(0,-1);
#endif
}
#ifdef _UNICODE
if(GetEnabledShadow() && (GetTextShadowColorA() > 0 || GetTextShadowColorB() > 0))
nGraphics.DrawString(m_TextValue,m_TextValue.GetLength(),&nFont,nShadowRc,&format,&nLineGrBrushA);
nGraphics.DrawString(m_TextValue,m_TextValue.GetLength(),&nFont,nRc,&format,&nLineGrBrushB);
#else
USES_CONVERSION;
wstring mTextValue = A2W(m_TextValue.GetData());
if(GetEnabledShadow() && (GetTextShadowColorA() > 0 || GetTextShadowColorB() > 0))
nGraphics.DrawString(mTextValue.c_str(),mTextValue.length(),&nFont,nShadowRc,&format,&nLineGrBrushA);
nGraphics.DrawString(mTextValue.c_str(),mTextValue.length(),&nFont,nRc,&format,&nLineGrBrushB);
#endif
}
}
}
调用示例
void CTextMsgDlg::UiInit()
{
UINT uStyle = 0;
m_pLabelMsg = static_cast<CLabelUIEx *>(m_PaintManager.FindControl(ELEMENT_TEXT_DLG_LABEL_MSG));
if (NULL != m_pLabelMsg)
{
m_pLabelMsg->SetText(m_strMsg.c_str());
uStyle = m_pLabelMsg->GetTextStyle();
uStyle |= DT_WORDBREAK;
uStyle &= (~DT_CENTER);
m_pLabelMsg->SetTextStyle(uStyle /*DT_CENTER | DT_VCENTER | DT_WORDBREAK | DT_WORD_ELLIPSIS */);
}
}
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翻翻,看看有没有这篇文章描述的内容:)