MFC CRichEditCtrl实现不同行不同颜色

 

1.首先在添加在资源视图中添加CRichEditCtrl控件,并关联变量:

image.png

 

 

注意将属性Multiline和Read Only设为True:


image.png

 

2.初始化控件

在BOOL CRichEditTestApp::InitInstance()函数中添加:

...
if (!AfxInitRichEdit2())
{
    return FALSE;
}
...

3. 设置字体大小颜色

参考自Richedit在结尾添加一行,同时设置字体,大小及颜色,并在添加后自动滚屏到末行

int CRichEditTestDlg::GetNumVisibleLines(CRichEditCtrl* pCtrl)
{
    CRect rect;
    long nFirstChar, nLastChar;
    long nFirstLine, nLastLine;

    // Get client rect of rich edit control
    pCtrl->GetClientRect(rect);

    // Get character index close to upper left corner
    nFirstChar = pCtrl->CharFromPos(CPoint(0, 0));

    // Get character index close to lower right corner
    nLastChar = pCtrl->CharFromPos(CPoint(rect.right, rect.bottom));
    if (nLastChar < 0)
    {
        nLastChar = pCtrl->GetTextLength();
    }

    // Convert to lines
    nFirstLine = pCtrl->LineFromChar(nFirstChar);
    nLastLine = pCtrl->LineFromChar(nLastChar);

    return (nLastLine - nFirstLine);
}

int CRichEditTestDlg::AppendToLogAndScroll(const CString& csMsg, COLORREF color, int iFontSize)
{
    long nVisible = 0;
    long nInsertionPoint = 0;
    CHARFORMAT cf;
    m_richEdit.GetSelectionCharFormat(cf);
    // Initialize character format structure
    cf.cbSize = sizeof(CHARFORMAT);
    //static int iHeight = 1000;
    cf.dwMask |= CFM_BOLD;
    cf.dwEffects |= CFE_BOLD;//设置粗体,取消用cf.dwEffects&=~CFE_BOLD;
    cf.dwEffects &= ~CFE_AUTOCOLOR;
    cf.dwMask |= CFM_COLOR;
    cf.crTextColor = color;
    cf.dwMask |= CFM_SIZE;
    cf.yHeight = iFontSize;//设置高度
    cf.dwMask |= CFM_FACE;
    _tcscpy_s(cf.szFaceName, _T("微软雅黑"));//设置字体
    // Set insertion point to end of text
    nInsertionPoint = m_richEdit.GetWindowTextLength();
    m_richEdit.SetSel(nInsertionPoint, -1);

    // Set the character format
    m_richEdit.SetSelectionCharFormat(cf);

    // Replace selection. Because we have nothing
    // selected, this will simply insert
    // the string at the current caret position.
    m_richEdit.ReplaceSel(csMsg);

    // Get number of currently visible lines or maximum number of visible lines
    // (We must call GetNumVisibleLines() before the first call to LineScroll()!)
    nVisible = GetNumVisibleLines(&m_richEdit);

    // Now this is the fix of CRichEditCtrl's abnormal behaviour when used
    // in an application not based on dialogs. Checking the focus prevents
    // us from scrolling when the CRichEditCtrl does so automatically,
    // even though ES_AUTOxSCROLL style is NOT set.
    if (&m_richEdit != m_richEdit.GetFocus())
    {
        m_richEdit.LineScroll(INT_MAX);
        m_richEdit.LineScroll(1 - nVisible);
    }

    return 0;
}

4.使用示例:

void CRichEditTestDlg::OnBnClickedTest()
{
    CString strPerRet;
    strPerRet.Format(_T("红色行\n"));
    AppendToLogAndScroll(strPerRet, RGB(255, 0, 0), 300);

    strPerRet.Format(_T("绿色行\n"));
    AppendToLogAndScroll(strPerRet, RGB(0, 255, 0), 300);

    strPerRet.Format(_T("蓝色行\n"));
    AppendToLogAndScroll(strPerRet, RGB(0, 0, 255), 300);
}

5.效果图

image.png

 

6.代码

https://download.csdn.net/download/xy18990/11267263

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值