【问题标题】:CRichEditCtrl appending colored text?CRichEditCtrl 附加彩色文本?
【发布时间】:2012-11-19 07:13:02
【问题描述】:

我在 MFC 项目中有一个 CRichEditCtrl,我将其用作报告日志。 根据给定的情况,我需要将不同颜色的文本附加到控件(即标准通知的蓝线,错误的红线等)。 我已经非常接近让它工作了,但它仍然表现得很奇怪:

void CMyDlg::InsertText(CString text, COLORREF color, bool bold, bool italic)
{
    CHARFORMAT cf = {0};
    CString txt;
    int txtLen = m_txtLog.GetTextLength();
    m_txtLog.GetTextRange(0, txtLen, txt);

    cf.cbSize = sizeof(cf);
    cf.dwMask = (bold ? CFM_BOLD : 0) | (italic ? CFM_ITALIC : 0) | CFM_COLOR;
    cf.dwEffects = (bold ? CFE_BOLD : 0) | (italic ? CFE_ITALIC : 0) |~CFE_AUTOCOLOR;
    cf.crTextColor = color;

    m_txtLog.SetWindowText(txt + (txt.GetLength() > 0 ? "\n" : "") + text);
    m_txtLog.SetSel(txtLen, m_txtLog.GetTextLength());
    m_txtLog.SetSelectionCharFormat(cf);
}

最终的结果是,新添加的行被适当地着色,但之前的所有文本都变成黑色。最重要的是,对于每个附加的文本行,起始选择似乎增加了 1。例如:

Call #1:
- [RED]This is the first line[/RED]

Call #2:
- [BLACK]This is the first line[/BLACK]
- [GREEN]This is the second line[/GREEN]

Call #3:
- [BLACK]This is the first line[/BLACK]
- [BLACK]This is the second line[/BLACK]
- [BLUE]This is the third line[/BLUE]

Call #4:
- [BLACK]This is the first line[/BLACK]
- [BLACK]This is the second line[/BLACK]
- [BLACK]This is the third line[/BLACK]
- [BLACK]T[/BLACK][YELLOW]his is the fourth line[/YELLOW]

Call #5:
- [BLACK]This is the first line[/BLACK]
- [BLACK]This is the second line[/BLACK]
- [BLACK]This is the third line[/BLACK]
- [BLACK]This is the fourth line[/BLACK]
- [BLACK]Th[/BLACK][ORANGE]is is the fifth line[/ORANGE]

etc...

那么我该如何解决这个问题,让所有以前的文本和格式保持原样,同时附加一行新的彩色文本?

【问题讨论】:

    标签: c++ text mfc colors cricheditctrl


    【解决方案1】:

    您的示例代码通过调用GetTextRange() 从对话框中读取旧文本。这不包括任何丰富的格式,因此,当文本放回原位时,它不会被格式化。您可以通过将光标设置到末尾而不进行任何选择并调用ReplaceSel(),在文本区域的末尾“插入”来完全放弃这一点。

    我认为你的方法应该是这样的:

    void CMFCApplication2Dlg::InsertText(CString text, COLORREF color, bool bold, bool italic)
    {
        CHARFORMAT cf = {0};
        int txtLen = m_txtLog.GetTextLength();
    
        cf.cbSize = sizeof(cf);
        cf.dwMask = CFM_BOLD | CFM_ITALIC | CFM_COLOR;
        cf.dwEffects = (bold ? CFE_BOLD : 0) | (italic ? CFE_ITALIC : 0);
        cf.crTextColor = color;
    
        m_txtLog.SetSel(txtLen, -1); // Set the cursor to the end of the text area and deselect everything.
        m_txtLog.ReplaceSel(text); // Inserts when nothing is selected.
    
        // Apply formating to the just inserted text.
        m_txtLog.SetSel(txtLen, m_txtLog.GetTextLength());
        m_txtLog.SetSelectionCharFormat(cf);
    }
    

    【讨论】:

    • 将相应行更改为m_txtLog.ReplaceSel((txtLen < 1 ? L"" : L"\n") + text);后,它按预期追加了一个新行。谢谢!
    • @RectangleEquals 很高兴听到它! :D
    • 我认为掩码应该是 cf.dwMask = CFM_BOLD | CFM_斜体 | CFM_COLOR;否则一旦打开粗体或斜体,就无法关闭它们。
    • 另外,| ~CFE_AUTOCOLOR 不是您想要的,应该被忽略。它只是不会影响这个例子,因为dwMask 也是错误的,正如dougnorton 已经提到的那样。
    【解决方案2】:

    我从尝试使这项工作中学到的东西是,您不能像上面的示例那样只设置标志。这样做:

     cf.dwMask = (bold ? CFM_BOLD : 0) | (italic ? CFM_ITALIC : 0) | CFM_COLOR;
    

    成功了一半,但是如果您使用 CRichEditCtrl(作为示例)并获取当前格式(尽管我认为这对我来说无论如何都会发生......),那么您将浪费很多时间。上面的标志正在设置您要切换的值。所以,这实际上更有效:

    CHARFORMAT cf;
    cf.cbSize = sizeof(CHARFORMAT);
    GetSelectionCharFormat(cf);
    
    cf.dwMask = CFM_BOLD | CFM_ITALIC | CFM_COLOR; //set these to specify the flags you are changing
    
    if (bold)
    {
        cf.dwEffects |= CFE_BOLD; //toggle on
    }
    else
    {
        cf.dwEffects &= ~CFE_BOLD; //toggle off
    }
    
    if (italic)
    {
        cf.dwEffects |= CFE_ITALIC; //toggle on
    }
    else
    {
        cf.dwEffects &= ~CFE_ITALIC; //toggle off
    }
    
    cf.dwEffects &= ~CFE_AUTOCOLOR; //this only seemed to work if I set it this way
    

    如果您不这样做,格式将保留 - 即使您已清除它。这让我挠了挠头,我在谷歌上搜索了很多东西来修复它。

    【讨论】:

      【解决方案3】:

      在插入新文本之前指定格式。如果您在插入后应用格式,您最终会得到格式不正确的文本。

      CHARFORMAT cf = { 0 };
      int txtLen =  GetTextLength();
      
      cf.cbSize = sizeof(cf);
      cf.dwMask = CFM_COLOR;
      cf.dwEffects = ~CFE_AUTOCOLOR;//No auto color
      cf.crTextColor = RGB(0, 0, 255);//color of the text
      
      SetSel(txtLen, -1);//Deselect all
      SetSelectionCharFormat(cf);//Assign the format to the cursor pos
      ReplaceSel(newText);
      

      【讨论】:

        猜你喜欢
        • 2011-07-27
        • 1970-01-01
        • 1970-01-01
        • 2023-03-11
        • 2011-10-21
        • 1970-01-01
        • 2014-08-16
        • 2018-06-10
        • 1970-01-01
        相关资源
        最近更新 更多