You can use SelAttributes to insert formatted text. See
www.tamaracka.com/search.htm for more on this TCustomRichEdit property.
You could also try creating this unit and using these functions...
unit RTFProcs;
interface
uses Windows, ComCtrls, Classes;
type
TEditStreamCallBack = function (dwCookie: Longint; pbBuff: PByte;cb:
Longint; var pcb: Longint): DWORD; stdcall;
TEditStream = record
dwCookie: Longint;
dwError: Longint;
pfnCallback: TEditStreamCallBack;
end;
procedure GetRTFSelection( aRichEdit: TCustomRichEdit; intoStream:
TStream );
procedure PutRTFSelection( aRichEdit: TCustomRichEdit; sourceStream:
TStream);
procedure InsertRTF(aRichEdit:TCustomRichEdit; s : String);
procedure CopyRTF(aSource, aDest:TCustomRichEdit);
procedure AppendRTF(aRichEdit:TCustomRichEdit; s : String);
implementation
uses RichEdit, Messages;
function EditStreamInCallback(dwCookie: Longint; pbBuff: PByte;
cb: Longint; var pcb: Longint): DWORD; Stdcall;
var
theStream: TStream;
dataAvail: LongInt;
begin
theStream := TStream(dwCookie);
with theStream do
begin
dataAvail := Size - Position;
Result := 0;
if dataAvail <= cb then
begin
pcb := Read(pbBuff^, dataAvail);
if pcb <> dataAvail then
result := DWord(E_FAIL);
end
else
begin
pcb := Read(pbBuff^, cb);
if pcb <> cb then
result := DWord(E_FAIL);
end;
end;
end;
function EditStreamOutCallback(dwCookie: Longint; pbBuff: PByte; cb:
Longint; var pcb: Longint): DWORD; stdcall;
var theStream: TStream;
begin
theStream := TStream(dwCookie);
with theStream do
begin
if cb > 0 then
pcb := Write(pbBuff^, cb);
Result := 0;
end;
end;
procedure GetRTFSelection( aRichEdit: TCustomRichEdit; intoStream:
TStream );
var editstream: TEditStream;
begin
with editstream do
begin
dwCookie:= Longint(intoStream);
dwError:= 0;
pfnCallback:= EditStreamOutCallBack;
end;
aRichedit.Perform( EM_STREAMOUT, SF_RTF or SFF_SELECTION,
longint(@editstream));
end;
procedure PutRTFSelection( aRichEdit: TCustomRichEdit; sourceStream:
TStream);
var editstream: TEditStream;
begin
with editstream do
begin
dwCookie:= Longint(sourceStream);
dwError:= 0;
pfnCallback:= EditStreamInCallBack;
end;
aRichedit.Perform( EM_STREAMIN, SF_RTF or SFF_SELECTION,
longint(@editstream));
end;
procedure InsertRTF(aRichEdit:TCustomRichEdit; s : String);
var aMemStream: TMemoryStream;
begin
if Length(s) > 0 then
begin
aMemStream := TMemoryStream.Create;
try
aMemStream.Write(s[1],length(s));
aMemStream.Position := 0;
PutRTFSelection( aRichEdit, aMemStream );
finally
aMemStream.Free;
end;
end;
end;
procedure CopyRTF(aSource, aDest:TCustomRichEdit);
var aMemStream: TMemoryStream;
begin
aMemStream := TMemoryStream.Create;
try
GetRTFSelection(aSource, aMemStream );
aMemStream.Position := 0;
PutRTFSelection(aDest, aMemStream );
finally
aMemStream.Free;
end;
end;
procedure AppendRTF(aRichEdit:TCustomRichEdit; s : String);
var start, length, eventmask : integer;
begin
eventmask := SendMessage(aRichEdit.Handle,EM_SETEVENTMASK,0,0);
SendMessage(aRichEdit.Handle,WM_SETREDRAW,0,0);
start := aRichedit.SelStart;
length := aRichEdit.SelLength;
aRichEdit.SelLength := 0;
aRichEdit.SelStart := System.Length(aRichEdit.Text);
InsertRTF(aRichEdit,s);
aRichEdit.SelStart := start;
aRichEdit.SelLength := length;
SendMessage(aRichEdit.Handle,WM_SETREDRAW,1,0);
InvalidateRect(aRichEdit.Handle,nil,true);
SendMessage(aRichEdit.Handle,EM_SETEVENTMASK,0,eventmask);
end;
end.
-Paul