将格式化文本输出到屏幕

时间:2013-12-23 09:17:47

标签: c++ richedit-control

我有一个存储对的向量。每对包含一个CString和一个bool。 如果要对CString加下划线,则bool为true,否则为false。

我想将矢量中的文本输出到屏幕,确保文本在正确的位置加下划线。

到目前为止,我有以下代码:

void CEmergenceView::AppendText( CString msg ) {
   int nBegin;
   CRichEditCtrl &rec = GetRichEditCtrl();
   nBegin = rec.GetTextLength();
   rec.SetSel(nBegin, nBegin);   // Select last character
   rec.ReplaceSel(msg);          // Append, move cursor to end of text
   rec.SetSel(-1,0);             // Remove Black selection bars
   nBegin = rec.GetTextLength(); // Get New Length
   rec.SetSel(nBegin,nBegin);    // Cursor to End of new text
   // Fix annoying "do you want to save your changes?" when program exits
   GetDocument()->SetModifiedFlag(FALSE); // -Optional- (sometimes you want this)
}

int nEnd = 0;

// loop through start of text to end of text
for(int k = 0; k < groups.size(); k++) {
    rEditCtrl.SetSel(nEnd, nEnd);
    rEditCtrl.GetSelectionCharFormat(cf);
    if(groups.at(k).second) {
        if(!cf.dwEffects & !CFE_UNDERLINE) {
            CRichEditView::OnCharUnderline();
        }
    }
    else if(!groups.at(k).second) {
                    if(cf.dwEffects & CFE_UNDERLINE) {
            CRichEditView::OnCharUnderline();
        }
    }    
    AppendText(groups.at(k).first);
    nEnd = nEnd + (groups.at(k).first.GetLength());
}

然而,这根本没有强调......谁能告诉我我做错了什么?谢谢!

1 个答案:

答案 0 :(得分:0)

我认为你应该实现OnCharUnderline

尝试调用自己的函数而不是默认函数:

您可以从here获取

void CMyRichEditView::OnCharUnderline ()
{
    CHARFORMAT2 cf;
    cf = GetCharFormatSelection();

    if (!(cf.dwMask & CFM_UNDERLINE) || !(cf.dwEffects & CFE_UNDERLINE))
        cf.dwEffects = CFE_UNDERLINE;
    else
        cf.dwEffects = 0;

    cf.dwMask = CFM_UNDERLINE;
    SetCharFormat(cf);
}