MFC CRichEdit 打印日志

来源:互联网 发布:四川遂宁广电网络客服 编辑:程序博客网 时间:2024/06/03 20:42

原文链接: http://www.orcode.com/article/Edit_20120879.html


首先在应用程序CxxxApp 类的 InitInstance 中 AfxEnableControlContainer()  后面添加 AfxInitRichEdit2();

然后在资源中添加RichEdit2控件

根据需要改变控件属性需要垂直滚动条把 Auto VScroll 设置为true , 只需打印 Read Only 置为true, 需要换行 Want Return 置为true.


然后开始添加方法.


int CRichEditDemoDlg::AppendToLog(CString str, COLORREF color)
{
int nOldLines = 0;
int nNewLines = 0;
int nScroll = 0;
long nInsertionPoint = 0;


CHARRANGE cr;
CHARFORMAT cf;


nOldLines = m_logEdit.GetLineCount();


cf.cbSize = sizeof(CHARFORMAT);
cf.dwMask = CFM_COLOR;
cf.dwEffects = 0;
cf.crTextColor = color;


nInsertionPoint = m_logEdit.GetWindowTextLengthW();


nInsertionPoint = -1;


m_logEdit.SetSel(nInsertionPoint, -1);


m_logEdit.SetSelectionCharFormat(cf);


m_logEdit.ReplaceSel(str);


nNewLines = m_logEdit.GetLineCount();


nScroll = nNewLines - nOldLines;


m_logEdit.LineScroll(nScroll);


return 0;
}


需要换行的,输入str中自行在末尾添加'\n'.



原创粉丝点击