CRichEditCtrl附加彩色文本? [英] CRichEditCtrl appending colored text?

查看:137
本文介绍了CRichEditCtrl附加彩色文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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)| (斜体?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?\\\
:)+ text);
m_txtLog.SetSel(txtLen,m_txtLog.GetTextLength());
m_txtLog.SetSelectionCharFormat(cf);
}

最好的结果是,所有之前的文本变为黑色。除此之外,对于每个附加的文本行,开始选择似乎增加1.例如:

  1:
- [RED]这是第一行[/ RED]

调用#2:
- [BLACK]这是第一行[/ BLACK]
- [GREEN]这是第二行[/ GREEN]

调用#3:
- [BLACK]这是第一行[/ BLACK]
- BLACK]这是第二行[/ BLACK]
- [BLUE]这是第三行[/ BLUE]

调用#4:
- [BLACK]第一行[/ BLACK]
- [BLACK]这是第二行[/ BLACK]
- [BLACK]这是第三行[/ BLACK]
- [BLACK] T [/ BLACK] [YELLOW]他是第四行[/ YELLOW]

调用#5:
- [BLACK]这是第一行[/ BLACK]
- [BLACK]这是第二行[/ BLACK]
- [BLACK]这是第三行[/ BLACK]
- [BLACK]这是第四行[/ BLACK]
- [BLACK] Th [/ BLACK] [ORANGE] is is the five line [/ ORANGE]

etc ...

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

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 =(bold?CFM_BOLD:0)| (斜体?CFM_ITALIC:0)| CFM_COLOR;
cf.dwEffects =(bold?CFE_BOLD:0)| (italic?CFE_ITALIC:0)|〜CFE_AUTOCOLOR;
cf.crTextColor = color;

m_txtLog.SetSel(txtLen,-1); //将光标设置到文本区域的末尾,并取消选择所有内容。
m_txtLog.ReplaceSel(text); //在没有选择任何内容时插入。

//应用于刚刚插入的文本。
m_txtLog.SetSel(txtLen,m_txtLog.GetTextLength());
m_txtLog.SetSelectionCharFormat(cf);
}


I have a CRichEditCtrl in an MFC project, which I use as a report log. Depending on the given situation, I need to append different colored text to the control (ie. a blue line for standard notifications, a red line for errors, etc). I've come pretty close to getting this to work, but it still behaves strangely:

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);
}

At best, the end result is that the newly appended line is appropriately colored but all of the previous text turns black. On top of that, for each appended line of text, the starting selection seems to increase by 1. For example:

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...

So how can I fix this to where all the previous text and formatting remain as-is, while appending a new line of colored text?

解决方案

Your example code reads the old text out of the dialog with a call to GetTextRange(). This does not include any rich formatting so, when the text is put back in place, it is not formatted. You can completely forgo this by "inserting" at the end of the text area by setting the cursor to the end without any selection and calling ReplaceSel().

I think your method should look something like this:

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 = (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.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);
}

这篇关于CRichEditCtrl附加彩色文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆