The TRichEdit Delphi control is an editor control that supports rich text formatting: text that includes variation in font attributes and paragraph formatting information - rich text formatting.
RTF files are actually ASCII files with special commands to indicate formatting information, such as fonts and margins.
Append or Insert RTF from one RichEdit to Another
The TRichEdit control does not expose a method to append or insert a piece of RTF text. It does have a Lines property to let you add more text - but you'll have trouble if you want to append rich text formatted content - it will be added as a (ASCII) simple
text.
If you want to "move" the entire text from one rich editor to another you can use streams - but the content of the "destination" rich editor will be totally overwritten by the "source" editor's text.
Here's a function that allows appending or inserting RTF text from one rich editor to another - using rich edit specific callback functions:
uses RichEdit;
procedure AppendToRichEdit(const source, destination : TRichEdit) ;
var
rtfStream: TEditStream;
sourceStream : TMemoryStream;
function EditStreamReader(
dwCookie: DWORD;
pBuff: Pointer;
cb: LongInt;
pcb: PLongInt): DWORD; stdcall;
begin
result := $0000;
try
pcb^ := TStream(dwCookie).Read(pBuff^, cb) ;
except
result := $FFFF;
end;
end; (*EditStreamReader*)
begin
destination.Lines.BeginUpdate;
sourceStream := TMemoryStream.Create;
try
source.Lines.SaveToStream(sourceStream) ;
sourceStream.Position := 0;
destination.MaxLength := destination.MaxLength + sourceStream.Size;
rtfStream.dwCookie := DWORD(sourceStream) ;
rtfStream.dwError := $0000;
rtfStream.pfnCallback := @EditStreamReader;
destination.Perform(
EM_STREAMIN,
SFF_SELECTION or SF_RTF or SFF_PLAINRTF, LPARAM(@rtfStream)
) ;
if rtfStream.dwError <> $0000 thenb
raise Exception.Create('Error appending RTF data.') ;
finally
sourceStream.Free;
destination.Lines.EndUpdate;
end;
end;
The AppendToRichEdit procedure takes two parameters. AppendToRichEdit copies the entire content of "source" rich editor to "destination" rich editor.
Depending on the selection inside the destination rich editor and the position of the text cursor, the above procedure will either
replace any current selection - if there's a selection in "destination",
insert content from source to destination at the point of the cursor,
append content from source to destination.
Note: There's a LoadFromStream method exposed by TRichEdit butLoadFromStream does not append the loaded text to the text already in the editor. Also, to move RTF from one rich editor to another, you have to use streams - and that's what the above code does
+ plus some specifi rich editor callbacks.