• 为DuiLib的Edit控件增加PlaceHolderText


    效果:没有焦点时显示一个提示文本,获得焦点时则清空提示,开始输入

    开始修改:
    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; }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    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);
    
    		}
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    在SetAttribute中增加一行:

    else if (_tcscmp(pstrName, _T("placeholdertext")) == 0) SetPlaceholderText(pstrValue);
    
    • 1

    这样可以在xml里直接设置placeholdertext属性:

    
    
    • 1
  • 相关阅读:
    2023/11/16JAVA学习
    Oracle 直接路径插入(Direct-Path Insert)
    MySQL集群:双主模式
    Mybatis 动态SQL
    零基础html学习-第五期
    Bytebase x Hacktoberfest 2023 黑客啤酒节开源挑战邀请
    2022年最新蚂蚁金服 Java 高级岗 2000+ 面试通关秘籍,就这水平
    文件系统理论详解,Linux操作系统原理与应用
    【Linux系统化学习】探索进程的奥秘 | 第一个系统调用
    C语言典范编程
  • 原文地址:https://blog.csdn.net/startl/article/details/133434379