0

I'm trying to write something like rtf editor in BCB6 and I've encountered such problem while trying to add table to my RichEdit1:

    RichEdit1->PlainText=true;
    AnsiString ret=RichEdit1->Text;
    ret.Insert(table, RichEdit1->SelStart);
    RichEdit1->Text=ret;
    RichEdit1->PlainText=false;
    RichEdit1->Repaint();

This code adds formatted text (code of table) to the RichEdit1 instead of adding formatting code as plain text and displaying it like a table.

Am I doing it wrong, or it can be a problem with something else.

Johan
  • 74,508
  • 24
  • 191
  • 319
Pavlus
  • 1,651
  • 1
  • 13
  • 24

2 Answers2

0

The PlainText property is only used by the Lines->LoadFrom...() and Lines->SaveTo...() methods, nothing else.

The Text property only operates on plain text. Reading the property extracts the RichEdit's textual content without formatting. Setting the property does not process RTF code at all, the RichEdit's textual content is replaced with the new text as-is.

If you want to insert RTF code into the RichEdit, especially if you don't want to overwrite the RichEdit's current content, you will have to use the EM_STREAMIN message directly. For example:

DWORD CALLBACK StreamInCallback(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
    int numRead = reinterpret_cast<TStringStream*>(dwCookie)->Read(pbBuff, cb);
    if (pcb) *pcb = numRead;
    return 0;
}

TStringStream *strm = new TStringStream(table);

EDITSTREAM es = {0};
es.dwCookie = (DWORD_PTR) strm;
es.pfnCallback = &StreamInCallback;
SendMessage(RichEdit1->Handle, EM_STREAMIN, SF_RTF | SFF_SELECTION, reinterpret_cast<LPARAM>(&es));

delete strm;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • I've tried your solution, but it doesn't work for me. If I try to add a table to the non-empty document, all content is replaced with one space character, and even more, if there is selected text while adding a table, program crashes with access violation exception while reading from random addresses, and sometimes from addresses like 0xffff0000 and 0x0000ffff – Pavlus May 22 '13 at 17:20
  • And what's interesting - if I put SF_TEXT instead of SF_RTF, code of table is added as plain text to the richedit, so program should work. – Pavlus May 22 '13 at 18:38
  • `SF_RTF` tells the RichEdit to process the RTF coding and not interpret it as plain text. `SFF_SELECTION` tells the RichEdit to apply the RTF coding only to the currently selected text if any, or to the current insertion point if no selection, without replacing the entire content. If that is not working correctly for you, then please update your question with the actual code you are using now, and the actual RTF code you are trying to insert. – Remy Lebeau May 22 '13 at 20:24
  • i'm using your code. Problem was in formatting code, not in way it was added. Now it's `{}` and works perfect, but `` didn't work. – Pavlus May 25 '13 at 13:36
0

Problem solved, formatting was not added because of table code was not in {} brackets, after adding them around table code and using SendMessage, program works well.

Pavlus
  • 1,651
  • 1
  • 13
  • 24