The advanced editing control is shown in the figure:
Compared with ordinary edit controls, in advanced edit controls, different text and paragraphs can be set with different fonts and colors.
To use advanced editing controls, you should first add the following code to the InitInstance() function of the APP class: AfxInitRichEdit();
Set the necessary properties: Multiline, WantReturn, etc. Others are set as needed.
The use of advanced editing controls is as follows:
CHARFORMAT cf = {sizeof(cf)}; cf.dwMask = CFM_COLOR|CFM_FACE|CFM_SIZE|CFM_BOLD;//总开关 cf.crTextColor = RGB(0,255,0);//font color cf.yHeight = 400; // size wcscpy(cf.szFaceName,_T("Official script"));//Font type m_ret.SetDefaultCharFormat(cf);//Set the format of all text in the control m_ret.SetWindowText(_T("Text entered for the first time")); //------------------------------------------------// int nLen = m_ret.GetWindowTextLength(); m_ret.SetSel (-1, -1); m_ret.ReplaceSel(_T("\r\nThe second input text")); cf.dwMask = CFM_COLOR|CFM_FACE|CFM_SIZE|CFM_BOLD|CFM_UNDERLINE;//总开关 cf.crTextColor = RGB(255,0,0);//font color cf.yHeight = 200; // size wcscpy(cf.szFaceName,_T("California"));//Font type cf.dwEffects = CFE_UNDERLINE;//Underline m_ret.SetSel(nLen,-1);//Select the text entered for the second time m_ret.SetSelectionCharFormat(cf);//Set the format of the selected text m_ret.SetSel(-1,-1);//Remove selection
The result is as follows:
When testing the example, I encountered a small problem, for the input "Chinese characters":
When the program character set uses multiple character sets, the number of characters returned by m_ret.GetWindowTextLength() = the number of Chinese characters * 2
When the program character set uses the Unicode set, the number of characters returned by m_ret.GetWindowTextLength() = the number of Chinese characters
There is no such problem when the text content is in English.