C, C++, MFC
[MFC] CRichEditCtrl 텍스트 컬러 지정
수동애비
2023. 5. 15. 17:01
반응형
아래의 함수에서 m_edClientLog 는 richEdit 의 컨트롤 변수이다.
로그 출력용 richEdit 에 1000 라인까지만 출력하도록 하는 기능
void outRichEditText(TCHAR *pwszLog, COLORREF color) {
CPoint point;
int first_pos = m_edClientLog.LineIndex(m_edClientLog.GetLineCount());
m_edClientLog.SetSel(first_pos, first_pos);
point = m_edClientLog.PosFromChar(first_pos);
m_edClientLog.SetCaretPos(point);
CHARFORMAT cf;
memset(&cf, 0, sizeof(CHARFORMAT));
cf.cbSize = sizeof(CHARFORMAT);
cf.dwMask = CFM_COLOR;
cf.crTextColor = color;
m_edClientLog.SetSelectionCharFormat(cf);
m_edClientLog.ReplaceSel(pwszLog);
CRect rcClientLog;
int pageLineCnt;
m_edClientLog.GetClientRect(&rcClientLog);
pageLineCnt = rcClientLog.Height() / 15;
int nDoLineScroll = m_edClientLog.GetLineCount() - pageLineCnt - m_edClientLog.GetFirstVisibleLine();
if (nDoLineScroll > 0 && m_chkScrollLock.GetCheck() == 0) {
m_edClientLog.LineScroll(nDoLineScroll);
}
if (m_edClientLog.GetLineCount() > 1000) {
m_edClientLog.SetRedraw(FALSE);
m_edClientLog.SetReadOnly(FALSE);
// 새로 추가된 내용 만큼 삭제
m_edClientLog.SetSel(0, (int)wcslen(pwszLog));
m_edClientLog.Clear();
// 라인 단위로 삭제하기 위해 첫라인 전체 삭제
int nFirstLineLen = m_edClientLog.LineLength(0);
m_edClientLog.SetSel(0, nFirstLineLen + 1);
m_edClientLog.Clear();
m_edClientLog.SetReadOnly(TRUE);
m_edClientLog.SetRedraw(TRUE);
}
}