想实现一个类似QQ聊天窗口的程序,就是我输入一行文字“123”,然后可以在显示框里显示出
Memory_Off(1111111)  11:23:30
123
并且 Memory_Off(1111111)  11:23:30 这一行的颜色(红色)和123这一行的颜色(蓝色)不一样。在网上找了个代码,他的代码的确是可以做到。然后原封不动的移植到我的程序里就不行了,123中1是红色,23是蓝色。最后发现他用的是richedit控件
而我用的是richedit2.0控件 但想不明白的是为什么2.0控件就不行下面是他的代码
void CMyRichEditCtrl::SetTitle(CString &strTitle, COLORREF &title_color)
{
// 写信息头 在 RichEdit 里面
int ititleLength = GetWindowTextLength();
SetSel(ititleLength, ititleLength);
ReplaceSel((LPCTSTR)strTitle);
int iStartPos = ititleLength; // 格式化 信息头
CHARFORMAT cf;
cf.cbSize = sizeof (CHARFORMAT);  
cf.dwMask = CFM_COLOR;
         cf.dwEffects = (unsigned long)~(CFE_AUTOCOLOR);
cf.crTextColor = title_color;  //RGB=(0,128,0); // The last ititleLength is The iStartPos
// After Replace the strTitle, ReCall GetWindowTextLength() 
//and the return value is iEndPos.. 
int iEndPos = GetWindowTextLength();
SetSel(iStartPos, iEndPos);
  SetSelectionCharFormat(cf); 
HideSelection(TRUE, FALSE);
         LineScroll(1);
}void CMyRichEditCtrl::SetMainInfo(CString &maininfo, COLORREF infocolor, LOGFONT info_lf)
{
    
     int ititleLength = GetWindowTextLength();
SetSel(ititleLength, ititleLength);
ReplaceSel((LPCTSTR)maininfo);
int iStartPos = ititleLength; // 格式化 主信息
CHARFORMAT cf;
cf.cbSize   = sizeof (CHARFORMAT);  
cf.dwMask   = CFM_COLOR | CFM_UNDERLINE | CFM_BOLD | CFM_FACE | CFM_CHARSET | CFM_SIZE | CFM_STRIKEOUT | CFM_ITALIC;
cf.dwEffects  = (unsigned long)~(CFE_AUTOCOLOR | CFE_BOLD | CFE_ITALIC | CFE_STRIKEOUT | CFE_UNDERLINE );
cf.dwEffects |= info_lf.lfUnderline ? CFE_UNDERLINE : cf.dwEffects ;
    cf.dwEffects |= info_lf.lfWeight==FW_BOLD ? CFE_BOLD : cf.dwEffects ;
    cf.dwEffects |= info_lf.lfItalic ? CFE_ITALIC : cf.dwEffects ;
cf.dwEffects |= info_lf.lfStrikeOut ? CFE_STRIKEOUT : cf.dwEffects ;
    cf.yHeight=-info_lf.lfHeight*15;
cf.crTextColor = infocolor;  //RGB=(0,128,0);

sprintf(cf.szFaceName, info_lf.lfFaceName);

// The last ititleLength is The iStartPos
// After Replace the strTitle, ReCall GetWindowTextLength() 
//and the return value is iEndPos.. 
int iEndPos = GetWindowTextLength();
SetSel(iStartPos, iEndPos);
  SetSelectionCharFormat(cf); 
HideSelection(TRUE, FALSE); 
LineScroll(1);
}