我用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;
}
(我原来的格式是一行一种颜色的,用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;
}
解决方案 »
- 受不鸟了...x64下编译总是出现LNK2019错误
- vc2008如何删除控件关联变量
- GetVersionEx 的一个bug,或许是winme系统bug setup.exe
- 对于对话框的菜单问题
- d3d开发 有没有什么语言限制 vc/vb/....
- 关于xml的问题,解决问题者都有分
- 我的程序是基于对话框的,我要在对话框一出来,鼠标就限制在对话框里面,用GetWindowRect(rt);ClipCursor(rt);结果把鼠标限制在屏幕左上
- 关于3des,试了很多次,还没成功,求助
- 请问怎样才能够读入一个.txt之类的文件,并且把内容显示出来,就像readbook 阅读器那样的
- 如何让对话框程序一启动就隐藏?
- 如何根据窗口宽度和给定字符串,计算窗口一行可显示字符串中的多少个字符?
- 关于静态成员
用这种方式才可以保存格式
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);
CRichEditCtrl::StreamInMSDN上面有例子