我用GetWindowText获得全部文字,然后用setwindowtext将全部文字输出就发现,原来设置的格式乱了
(我原来的格式是一行一种颜色的,用setwindowtext后全部变成了同一种颜色)
用SetSel选中最行一行,并且用ReplaceSel替换则没事int CDCicle::AppendString(CString &str, int systeminfo/* = 0*/)
{
m_reall_record += str;//m_reall_record记录着所有编辑框所有文字 

CString alltext;
m_edit_record.GetWindowText(alltext);
m_edit_record.SetSel(alltext.GetLength(), alltext.GetLength());
m_edit_record.ReplaceSel(str);
        //m_edit_record.SetWindowText(m_reall_record); //如果用成这行代码,虽然文字是正确的,但格式就出现了混乱了 if (systeminfo)
{
//   声明并设置要设置的类型   
CHARFORMAT*   lpcf   =   new   CHARFORMAT;   
lpcf->cbSize   =   sizeof(CHARFORMAT);   
lpcf->dwMask   =   CFM_COLOR;   
lpcf->dwEffects   =   NULL;   
lpcf->crTextColor   =   RGB(125, 190, 105);   

//   要改变颜色的区域   
CHARRANGE   rg;   
rg.cpMin   =   alltext.GetLength();   
rg.cpMax   =   alltext.GetLength() + str.GetLength();   

//   选定要改变的区域   
m_edit_record.SendMessage(EM_EXSETSEL,   0,   (LPARAM)   (CHARRANGE   FAR   *)   &rg);   

//   改变颜色   
HRESULT   hr   =   m_edit_record.SendMessage(EM_SETCHARFORMAT,   (WPARAM)   (UINT)   SCF_SELECTION,   (LPARAM)   lpcf);   

//   释放资源   
delete   lpcf;
}
return 0;
}

解决方案 »

  1.   

    SetWindowText()和GetWindowText()是父类函数,仅支持纯字符格式,Richedit不支持。
      

  2.   

    用StreamIn、StreamOut来存取RichEdit,流格式采用RTF
    用这种方式才可以保存格式
      

  3.   

    给个例子给你
    RichEdit写入/读入RTF文件如果你不想存成文件,可以自己定义MyStreamOutCallback/MyStreamInCallback函数保存在定义的内存中从Richedit保存成RTF文件// My callback procedure that reads the rich edit control contents
    // from a file.
    static DWORD CALLBACK 
    MyStreamOutCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
    {
       CFile* pFile = (CFile*) dwCookie;   pFile->Write(pbBuff, cb);
       *pcb = cb;   return 0;
    }// The example code.
       // The pointer to my rich edit control.
       extern CRichEditCtrl* pmyRichEditCtrl;
       // The file to store the contents of the rich edit control.
       CFile cFile(TEXT("myfile.rtf"), CFile::modeCreate|CFile::modeWrite);
       EDITSTREAM es;   es.dwCookie = (DWORD) &cFile;
       es.pfnCallback = MyStreamOutCallback; 
       pmyRichEditCtrl->StreamOut(SF_RTF, es);
    载入RTF文件// My callback procedure that writes the rich edit control contents
    // to a file.
    static DWORD CALLBACK 
    MyStreamInCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
    {
       CFile* pFile = (CFile*) dwCookie;   *pcb = pFile->Read(pbBuff, cb);   return 0;
    }// The example code.
       // The pointer to my rich edit control.
       extern CRichEditCtrl* pmyRichEditCtrl;
       // The file from which to load the contents of the rich edit control.
       CFile cFile(TEXT("myfile.rtf"), CFile::modeRead);
       EDITSTREAM es;   es.dwCookie = (DWORD) &cFile;
       es.pfnCallback = MyStreamInCallback; 
       pmyRichEditCtrl->StreamIn(SF_RTF, es);
      

  4.   

    如刺客所说,使用CRichEditCtrl::StreamOut
    CRichEditCtrl::StreamInMSDN上面有例子
      

  5.   

    谢谢各位,现在我已经能够保存文本的格式到内存了但是还有个问题:我是在做聊天程序,要把发送消息的文本框的内容照原格式加到接收消息的文本框里面,不知道要怎么做好 我试过获得发送消息的文本框的rtf字符串,然后连接到接收消息文本框的rtf字符串尾部但是这样显示不成功
      

  6.   

    直接字符串相加(先获得A的rtf文本,再获得B的rtf文本) 这样做好像不行