سؤال

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.

هل كانت مفيدة؟

المحلول

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;

نصائح أخرى

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top