Richedit控件的自定义颜色问题,求解
我写了个函数,功能是根据颜色属性,把文本添加到richedit控件里,如下
void AddText(char* pTxt, COLORREF color)
{
CHARFORMAT cf;
ZeroMemory(&cf, sizeof(CHARFORMAT));
cf.cbSize = sizeof(CHARFORMAT);
cf.dwMask = CFM_BOLD | CFM_COLOR | CFM_FACE |
CFM_ITALIC | CFM_SIZE | CFM_UNDERLINE;
cf.dwEffects = 0;
cf.yHeight = 14*14;//文字高度
cf.crTextColor = color; //文字颜色
strcpy(cf.szFaceName ,_T("宋体"));//设置字体
//在添加新的字符串前,先记录当前末尾的位置
long lBegin = GetWindowTextLength();
SetSel(-1, -1);
ReplaceSel(pTxt);
//添加完毕,记录新的长度
long lEnd = GetWindowTextLength();
SetSel(lBegin, pTxt ); //设置新的字符串区域
SetSelectionCharFormat(cf); //设置这个新添加的区域颜色和字体
}
AddText("test1\n", RGB(255,0,0));
AddText("test2\n", RGB(0,255,0));
AddText("test3\n", RGB(0,0,255));
long lBegin = GetWindowTextLength() + 1;
long lEnd = lBegin + strlen(pTxt);
//在SetSel前设置字体信息
SetSelectionCharFormat(cf);
SetSel(lBegin, lEnd );
ReplaceSel(pTxt);
ReplaceSel("\n");