Why does CRichEditControl hide border around words

281 Views Asked by At

I am using CRichEditCtrl (RichEdit20A) to display rtf-text:

m_reText.SetWindowText(strRtfText);

The problem is that the control does not display a border around words in rtf-text like this:

{\rtf1
\box\brdrdot
Hello World
}

I also tried RichEdit5.0 in a way as it proposed here, but result is the same, border is not displayed. However, if I save the text in .rtf file and open it in MSWord or Libre/OpenOffice editor, the dotted border around text is displayed correctly:

enter image description here

Why does CRichEditControl hide the border in my case? Please help, I would appreciate any suggestions.

1

There are 1 best solutions below

8
Barmak Shemirani On

You can display tables and borders with rich edit. The following will show a box with solid borders:

str = L"{\\rtf1\
\\trowd\\trgaph72 \
\\clbrdrt\\brdrdot\\clbrdrl\\brdrdot\\clbrdrb\\brdrdot\\clbrdrr\\brdrdot \
\\cellx3000 TEXT\\intbl\\cell \
\\row\\pard\\par\
}";

If you run this in Microsoft Word it will show dotted lines like it's supposed to. RichEdit does not handle dotted borders like it's supposed to, or maybe it's expecting a different format. If you save the file from Word, it still doesn't show dotted lines.

If you don't need dotted lines then use these simpler examples to show boxes in RichEdit:

CString str;
str = L"{\\rtf1\
\\trowd\\trgaph72 \
\\cellx3000 TEXT\\intbl\\cell \
\\row\\pard\\par\
}";

str = L"\
{\\rtf1\\ansi\\deff0\
\\trowd\
\\cellx1000\
\\cellx2000\
\\cellx3000\
\\ TEXT1\\cell\
\\ TEXT2\\cell\
\\ TEXT3\\cell\
\\row\
}";

See also link

Note, CRichEditCtrl::SetWindowText will simply call ::SetWindowText WinAPI, it will set the string as plain text.

Use CRichEdit::StreamIn to set raw rtf string. In your case you are probably using your own class which overrides CRichEditCtrl::SetWindowText and runs the necessary streaming.


Try the following to get rtf string from Word's spell check RichEdit:

DWORD __stdcall rtfstreamget(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
    CStringA text;
    text.GetBufferSetLength(cb);
    CStringA *ptr = (CStringA*)dwCookie;
    for(int i = 0; i < cb; i++)
        text.SetAt(i, *(pbBuff + i));
    *ptr += text;
    *pcb = text.GetLength();
    text.ReleaseBuffer();
    return 0;
}

bool GetRTF(hWnd, CString &sW)
{
    CStringA sA;
    EDITSTREAM es{ 0 };
    es.dwCookie = (DWORD_PTR)&sA;
    es.pfnCallback = rtfstreamget;
    edit.StreamOut((CP_UTF8 << 16) | SF_USECODEPAGE | SF_RTF, es);
    SendMessage(hWnd, EM_STREAMOUT, 
            (CP_UTF8 << 16) | SF_USECODEPAGE | SF_RTF, (LPARAM)&es);
    sW = CA2W(sA, CP_UTF8);
    return es.dwError == 0;
}

CStringW s;
GetRTF(msword_spellcheck_hwindow, str);