我自己包了个函数实现你说的功能, 其中m_editLog是多行文本框控件。参数strLog为你要输出的内容, bAddTime为是否在要输出的内容前加上时间。 其中m_critical锁可有可无,当你这个函数可能被多个线程同时调用的时候,就需要加!
void xxx::AddLog(CString strLog, BOOL bAddTime)
{
if (bAddTime)
{
CTime t = CTime::GetCurrentTime();
strLog = t.Format("%x %X ") + strLog;
}
strLog += _T("\n");
m_crical.Lock();
int nLineCnt = m_editLog.GetLineCount();
int nBegin, nEnd;
nBegin = m_editLog.LineIndex(nLineCnt-1);
if (nBegin != -1)
{
nEnd = nBegin + m_editLog.LineLength(nBegin);
m_editLog.SetSel(nBegin, nEnd);
m_editLog.ReplaceSel(strLog);
}
else
{
m_editLog.SetSel(0, 0);
m_editLog.ReplaceSel(strLog);
}
if (nLineCnt >= 500)
{
m_editLog.SetSel(0, -1);
m_editLog.Clear();
}
m_crical.Unlock();
}