C++ CRICHEDITCRL追加彩色文本?
我在MFC项目中有一个CRICHEDITCRL,用作报告日志。 根据给定的情况,我需要在控件中添加不同颜色的文本(例如,标准通知用蓝线,错误用红线,等等)。 我已经非常接近让它工作了,但它仍然表现得很奇怪:C++ CRICHEDITCRL追加彩色文本?,c++,text,mfc,colors,cricheditctrl,C++,Text,Mfc,Colors,Cricheditctrl,我在MFC项目中有一个CRICHEDITCRL,用作报告日志。 根据给定的情况,我需要在控件中添加不同颜色的文本(例如,标准通知用蓝线,错误用红线,等等)。 我已经非常接近让它工作了,但它仍然表现得很奇怪: void CMyDlg::InsertText(CString text, COLORREF color, bool bold, bool italic) { CHARFORMAT cf = {0}; CString txt; int txtLen = m_txtL
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...
因此,在添加一行新的彩色文本时,如何将此问题修复为所有以前的文本和格式保持不变?您的示例代码通过调用
getExtange()
从对话框中读取旧文本。这不包括任何丰富的格式,因此,当文本放回原位时,它不会被格式化。通过将光标设置到文本区域的末尾而不进行任何选择和调用,您可以完全放弃在文本区域的末尾“插入”
我认为你的方法应该是这样的:
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);
}
我从尝试实现这一点中学到的是,不能像上面的示例那样设置标志。这样做:
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
如果您不这样做,格式将保持不变-即使您清除了它。这让我挠头了一段时间,我用谷歌搜索了很多时间来修复它。在插入新文本之前指定格式。如果在插入后应用格式,则最终将生成格式不正确的文本
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);
将相应行更改为
m_txtLog.ReplaceSel((txtLen<1?L”“:L“\n”)+文本)后代码>它按预期追加一行新行。谢谢@听到这个消息我很高兴我认为遮罩应该是cf.dwMask=CFM_BOLD | CFM_ITALIC | CFM_COLOR;否则,一旦启用粗体或斜体,就无法将其禁用。此外,~CFE\u AUTOCOLOR
不符合您的要求,应该禁用。这并不影响本例,因为正如前面提到的那样,dwMask
也是错误的。