常用控件:RichEdit 2.0 Ctrl实现日志窗口

1.拖放RichEdit 2.0 控件到对话框
2.InitInstance()中调用 AfxInitRichEdit2();
3.定义全局变量:

CHARFORMAT cf;
long g_lMax = 0;
long g_lMin = 0;
CRichEditCtrl *g_pRichEditRecv2;
UINT g_uiFontSize;
UINT g_uiRGB_R;
UINT g_uiRGB_G;
UINT g_uiRGB_B;

4.在对话框初始化时,绑定控件

g_pRichEditRecv2 = (CRichEditCtrl *)GetDlgItem(IDC_RICHEDIT21);

5.在需要时调用:

//设置带格式文本
void SetCharFormat(CRichEditCtrl* pRichedit, CString strCharText, CString strFontName, UINT uiFontSize, UINT uiRed, UINT uiGreen, UINT uiBlue)
{
    ZeroMemory(&cf, sizeof(CHARFORMAT));
    cf.cbSize = sizeof(CHARFORMAT);
    cf.dwMask = CFM_BOLD | CFM_COLOR | CFM_FACE | CFM_ITALIC | CFM_SIZE | CFM_UNDERLINE;
    cf.dwEffects = 0;
    cf.yHeight = uiFontSize;//文字高度
    cf.crTextColor = RGB(uiRed, uiBlue, uiBlue); //文字颜色
    memcpy(cf.szFaceName, strFontName, strFontName.GetLength());//设置字体
    COLORREF cref = RGB(0,0,0);
    pRichedit->SetBackgroundColor(false, cref);
    pRichedit->SetSelectionCharFormat(cf);
    pRichedit->SetSel(g_lMin, g_lMax); //设置处理区域
    pRichedit->SetSel(-1, strCharText.GetLength()); //设置处理区域
    pRichedit->ReplaceSel(strCharText);
    pRichedit->PostMessage(WM_VSCROLL, SB_BOTTOM, 0);
    CHARRANGE cr;
    pRichedit->GetSel(cr);
    g_lMin = cr.cpMin;
    g_lMax = cr.cpMax;
}
//调用
SetCharFormat(g_pRichEditRecv2, "我是日志", _T("宋体"), 180, 255, 0, 0);

你可能感兴趣的:(常用控件:RichEdit 2.0 Ctrl实现日志窗口)