Added the duilib edit Control's prompt function and multiple text colors, duilibedit

Source: Internet
Author: User
Tags drawtext

Added the duilib edit Control's prompt function and multiple text colors, duilibedit

Reprinted, please describe the original source. Thank you ~~ : Http://blog.csdn.net/zhuhongshu/article/details/41786407

The CEditUI control of duilib uses the win32 native edit control. In a recent project, I need to enhance some functions of the CEditUI control. I will write the improved code to my blog. In fact, it is very easy to improve the code, but it may be used by others, so it will not take any longer.


The added functions are as follows:

1,The prompt text is added and the color of the prompt text can be set. When CEditUI is initialized or there is no text, the prompt text is automatically displayed in a certain color. (Do not set the prompt text)

2,You can set the text color for the native edit Control (the original text color is the same for both the native edit Control and CEditUI)


The corresponding attributes are described as follows:

<Attribute name = "nativetextcolor" default = "0x00000000" type = "DWORD" comment = "text color of the windows native edit control, such as (0 xFFFFFFFF) "/> <Attribute name =" tipvalue "default =" "type =" STRING "comment =" text box prompts text, when the text box is blank and the focus is lost, the text box "/> <Attribute name =" tipvaluecolor "default =" 0xFFBAC0C5 "type =" DWORD "comment =" text box displays color "/>


After this modification, you can set the initial prompt string and color for the prompt function. It is often used in the search box and user account input, when you click CEditUI, the prompt string disappears automatically. If no text exists in CEditUI, the prompt string appears automatically. Setting the text color of the native edit control can make the current status of CEditUI more obvious. For example, if there is no focus, the text is a color, when getting the focus, the text is of another color (actually similar to the hottextcolor attribute of other controls ).


In this way, the modified CEditUI has three text color effects and can be used as needed. Here we will first post a description of this effect. When the initial state or no text is available, CEditUI displays a gray text prompt. When the user inputs the text, the color is black and the text changes to red after the input is complete:




The xml Description of the effect is as follows:

<Edit name = "edt_url" padding = "10, 5," textpadding = "5, 5, 5. 5 "bkcolor =" # FFFFFFFF "width =" 350 "height =" 28 "font =" 0 "tipvalue =" Enter the user name "tipvaluecolor =" # FF989898 "nativetextcolor = "# FF000000 "nativebkcolor =" # FFFFFFFF "textcolor =" # FFFF0000 "/>


Improvement Process 1:


In fact, the CEditUI control in the Uilib Library (duilib's extended version, also provides Uilib in my maintenance Library) provides this function, but it is not enough to use it, I transplanted the prompt text function from Uilib to Duilib, and enhanced the prompt function and logic:


First, add several member functions to CEditUI:

void SetTipValue(LPCTSTR pStrTipValue);void SetTipValueColor(LPCTSTR pStrColor);DWORD GetTipValueColor();CDuiString GetTipValue();LPCTSTR GetSrcTipValue();CDuiString m_sTipValue;CDuiString m_sSrcTipValue;DWORD m_sTipValueColor;

The corresponding implementation code is:


void CEditUI::SetTipValue( LPCTSTR pStrTipValue ){if(m_sText != _T(""))m_sText = pStrTipValue;m_sSrcTipValue= pStrTipValue;m_sTipValue = CDuiString(_T("__IsTipValue__"))+pStrTipValue;}void CEditUI::SetTipValueColor( LPCTSTR pStrColor ){if( *pStrColor == _T('#')) pStrColor = ::CharNext(pStrColor);LPTSTR pstr = NULL;DWORD clrColor = _tcstoul(pStrColor, &pstr, 16);m_sTipValueColor = clrColor;}DWORD CEditUI::GetTipValueColor(){return m_sTipValueColor;}CDuiString CEditUI::GetTipValue(){return m_sTipValue;}LPCTSTR CEditUI::GetSrcTipValue(){return m_sSrcTipValue.GetData();}


The SetTipValue function determines that if the text of the CEditUI control is empty, set its initial text to tipvalue. This is the prompt text displayed when the CEditUI control is initialized. If the text attribute of CEditUI is set in xml writing, the SetTipValue function will not initialize the text of CEditUI.


Then add code to the SetAttribute function:

else if( _tcscmp(pstrName, _T("tipvalue")) == 0 ) SetTipValue(pstrValue);else if( _tcscmp(pstrName, _T("tipvaluecolor")) == 0 ) SetTipValueColor(pstrValue);else if( _tcscmp(pstrName, _T("nativetextcolor")) == 0 ) SetNativeEditTextColor(pstrValue);

Next, rewrite the PaintText function to complete this function:


void CEditUI::PaintText(HDC hDC){DWORD mCurTextColor = m_dwTextColor;if( m_dwTextColor == 0 ) mCurTextColor = m_dwTextColor = m_pManager->GetDefaultFontColor();if(GetText() == m_sSrcTipValue || GetText() == _T(""))mCurTextColor = m_sTipValueColor;if( m_dwDisabledTextColor == 0 ) m_dwDisabledTextColor = m_pManager->GetDefaultDisabledColor();CDuiString sText;if( m_sText.IsEmpty() ) sText = m_sSrcTipValue;else sText = m_sText;if( m_bPasswordMode ) {sText.Empty();LPCTSTR p = m_sText.GetData();while( *p != _T('\0') ) {sText += m_cPasswordChar;p = ::CharNext(p);}}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( IsEnabled() ) {CRenderEngine::DrawText(hDC, m_pManager, rc, sText, mCurTextColor, \m_iFont, DT_SINGLELINE | m_uTextStyle);}else {CRenderEngine::DrawText(hDC, m_pManager, rc, sText, m_dwDisabledTextColor, \m_iFont, DT_SINGLELINE | m_uTextStyle);}}

In the Code, if the current text is empty or the text in this article happens to be the prompt text, set the color of the drawn text to the color of the prompt text. Next, let's determine if CEditUI's text is empty and let him draw the prompt text. This completes the function of displaying prompt text when CEditUI has no text.


Improvement process 2:


The CEditUI control does not have the hottextcolor (or focustextcolor is more suitable) attribute. If this attribute is available, you can set more color values to make the text color of the input State different from that of the normal state, this allows users to clearly distinguish that CEditUI is entering and inputting.


In the input state, the edit Control of win32 actually works, so you can set the color for it. To do this, add an attribute named nativetextcolor. Add the member function as follows:

void SetNativeEditTextColor( LPCTSTR pStrColor );DWORD GetNativeEditTextColor() const;DWORD m_dwEditTextColor;


Corresponding implementation code:

void CEditUI::SetNativeEditTextColor( LPCTSTR pStrColor ){if( *pStrColor == _T('#')) pStrColor = ::CharNext(pStrColor);LPTSTR pstr = NULL;DWORD clrColor = _tcstoul(pStrColor, &pstr, 16);m_dwEditTextColor = clrColor;}DWORD CEditUI::GetNativeEditTextColor() const{return m_dwEditTextColor;}

M_dwEditTextColor is initialized to 0x000000, and the code added to SetAttribute will not be pasted. Next, the WM_CTLCOLOREDIT and WM_CTLCOLORSTATIC message responses of the HandleMessage function of the CEditWnd class are as follows:


LRESULT CEditWnd: HandleMessage (UINT uMsg, WPARAM wParam, LPARAM lParam ){//..... else if (uMsg = ocm1_base + WM_CTLCOLOREDIT | uMsg = ocm1_base + WM_CTLCOLORSTATIC) {if (m_pOwner-> GetNativeEditBkColor () = 0 xffffffffff) return NULL ;:: setBkMode (HDC) wParam, TRANSPARENT); DWORD dwTextColor; if (m_pOwner-> GetNativeEditTextColor ()! = 0x000000) dwTextColor = m_pOwner-> GetNativeEditTextColor (); elsedwTextColor = m_pOwner-> GetTextColor ();: SetTextColor (HDC) wParam, RGB (GetBValue (dwTextColor ), getGValue (dwTextColor), GetRValue (dwTextColor); if (m_hBkBrush = NULL) {DWORD clrColor = m_pOwner-> GetNativeEditBkColor (); m_hBkBrush = :: createSolidBrush (RGB (GetBValue (clrColor), GetGValue (clrColor), GetRValue (clrColor);} return (LRESULT) m_hBkBrush ;}//..... omitted}

It is determined that if you have set nativetextcolor (that is, it is not 0x000000), the text color will be nativetextcolor, otherwise it will be consistent with the textcolor of CEditUI.


Summary:

It is easy to modify the code, but it is quite convenient for me to use it. The modified code has been updated to the duilib and uilib libraries maintained by me. The address is: Click to open the link.


Redrain 2014.12.7


QQ: 491646717


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.