CRichEditCtrl appending colored text?

空扰寡人 提交于 2019-12-06 13:42:27

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

The thing I learned from trying to get this working is that you can't just set the flags like the examples above. Doing this:

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

Half works, but you will waste a lot of time if you are using the CRichEditCtrl (as an example) and getting the current formatting (though I think this was happening no matter what for me..) The issue is that the flags above are setting what you want to toggle the value of. So, this is actually more valid:

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

If you don't do it that way, the formatting will remain - even though you cleared it. It made me scratch my head for a while, and I did a lot of googling to fix it.

Assign the format prior to the insertion of the new text. If you apply the format after insertion, you will end up with an incorrectly formatted text.

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!