RichEditCtrl, RichEdit 사용해보자
먼저,
1. resource에 RichEdit를 집어넣자.!!!
2. AfxInitRichEdit() 함수를 콜하자.. 그래야 실행이 된다.
// CPG_XXX 생성
CPG_XXX::CPG_XXX()
{
AfxInitRichEdit( );
}
// initInstance() ..... 윗부분에 있음.
3. 변수와 연결시켜줍니다.
DDX_Control(pDX, IDC_CMDMSG, m_cmdmsg);
4. 원하는 폰트를 설정하기.
xxx.h 선언.
CFont m_Font2;
xxx.cpp Oninitdialog() 선언.
m_Font2.CreateFont(16, 0, 0, 0, FW_NORMAL , FALSE, FALSE, 0,
ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, FF_SWISS, "Tahoma");
m_cmdmsg.SetFont(&m_Font2);
5. RichEdit 꾸미기.
m_cmdmsg.SetTargetDevice(NULL, 0); // CDC 독립적으로 수행되도록 ??
m_cmdmsg.SetReadOnly();
m_cmdmsg.SetBackgroundColor(FALSE, RGB(0,0,0));
//the changer According to kind value.
#define NORMAL 0
#define FAIL 1
#define PASS 2
// color status.
#define P_COLOR RGB(200,157,21)
#define F_COLOR RGB(255,0,0)
#define N_COLOR RGB(88,228,106)
// RichEditCtrl 처음 초기화를 위하여 작업
void CPG_SendDlg::CmdMsgBoxInit(int kind, CString strbuf)
{
CHARFORMAT cf;
// Initialize character format structure
cf.cbSize = sizeof(CHARFORMAT);
cf.dwMask = CFM_COLOR;
cf.dwEffects = 0; // To disable CFE_AUTOCOLOR
cf.yHeight = 200;
switch(kind){
case NORMAL: cf.crTextColor = N_COLOR; break;
case FAIL: cf.crTextColor = F_COLOR; break;
case PASS: cf.crTextColor = P_COLOR; break;
}
m_cmdmsg.SetWordCharFormat(cf);
//strbuf+="\r\n";
int nOldLines = 0, nNewLines = 0, nScroll = 0;
long nInsertionPoint = 0;
nOldLines = m_cmdmsg.GetLineCount();
nInsertionPoint = m_cmdmsg.GetWindowTextLength();
int cline=m_cmdmsg.GetLineCount();
m_cmdmsg.ReplaceSel(strbuf);
int nline=m_cmdmsg.GetLineCount();
int scroll=nline-cline;
m_cmdmsg.SetCaretPos(CPoint(0,m_cmdmsg.GetLineCount()-1)); // Auto Scroll
m_cmdmsg.LineScroll(scroll);
ClearCmdMsgBox(0);
}
#define MAXMSGCNT 500
// RichEditContext Clear.
void CPG_SendDlg::ClearCmdMsgBox(int mode)
{
int maxcnt=0;
static int msgcnt=0;
msgcnt++;
if(mode == 0) maxcnt = MAXMSGCNT;
else maxcnt = 0;
if(msgcnt > maxcnt)
{
m_cmdmsg.LockWindowUpdate();
m_cmdmsg.SetWindowText("");
m_cmdmsg.UnlockWindowUpdate();
msgcnt=0;
}
}
// 실제 RichEdit에다 데이터 넣기..
void CPG_SendDlg::MsgDisp(const char * pFmt, ...)
{
char msgbuf[1024]="";
va_list vlist;
// 취득된 문자열 가져오기 시작
va_start(vlist, pFmt);
vsprintf(msgbuf, pFmt, vlist);
fflush(NULL);
// 끝 - 모든 변수를 읽은 후 정상적인 리턴을 위하여
va_end(vlist);
strcat(msgbuf,"\r\n");
m_msgout.ReplaceSel(msgbuf);
m_msgcount++;
if(m_msgcount > 500) {
m_msgout.SetWindowText("");
m_msgcount=0;
}
}