效果:没有焦点时显示一个提示文本,获得焦点时则清空提示,开始输入
开始修改:
UIEdit.h:
private:
DWORD m_dwPlaceholderTextColor=0xFF8B8B8B;
CDuiString m_sPlaceholderText;
public:
DuiLib::CDuiString GetPlaceholderText() const { return m_sPlaceholderText; }
void SetPlaceholderText(const DuiLib::CDuiString& pstrText) { m_sPlaceholderText = pstrText; };
DWORD GetPlaceholderTextColor() const { return m_dwPlaceholderTextColor; }
void SetPlaceholderTextColor(DWORD val) { m_dwPlaceholderTextColor = val; }
UIEdit.cpp:
void CEditUI::PaintText(HDC hDC)
{
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( m_sText.IsEmpty() ){
if (!m_sPlaceholderText.IsEmpty()) {
CRenderEngine::DrawText(hDC, m_pManager, rc, m_sPlaceholderText, m_dwPlaceholderTextColor, \
m_iFont, DT_SINGLELINE | m_uTextStyle);
return;
}
}
CDuiString sText = m_sText;
if( m_bPasswordMode ) {
//LOGCON("pssmode");
//LOG("passmode");
sText.Empty();
LPCTSTR p = m_sText.GetData();
while( *p != _T('\0') ) {
sText += m_cPasswordChar;
p = ::CharNext(p);
}
}
//LOG(sText.GetData());
if( IsEnabled() ) {
CRenderEngine::DrawText(hDC, m_pManager, rc, sText, m_dwTextColor, \
m_iFont, DT_SINGLELINE | m_uTextStyle);
}
else {
CRenderEngine::DrawText(hDC, m_pManager, rc, sText, m_dwDisabledTextColor, \
m_iFont, DT_SINGLELINE | m_uTextStyle);
}
}
在SetAttribute中增加一行:
else if (_tcscmp(pstrName, _T("placeholdertext")) == 0) SetPlaceholderText(pstrValue);
这样可以在xml里直接设置placeholdertext属性: