Board index » delphi » RichEdit, Saving selected text with formatting

RichEdit, Saving selected text with formatting

What is the easiest and least resource intensive way to save the selected
text from a Rich Edit to memory keeping the formatting of that text?  And
how can I put it back in later if needed.  The clipboard contents should be
preserved during all this.
Thanks
Bill

 

Re:RichEdit, Saving selected text with formatting


'RichEdit1.SaveToStream' and 'RichEdit1.LoadFromStream'

comment :
the TStream object is an abstract object, use his descendant - TMemoryStream

eyal wilde

Quote
bill shlaer wrote in message <01bd7799$26111fc0$6692ad98@newmicronpc>...
>What is the easiest and least resource intensive way to save the selected
>text from a Rich Edit to memory keeping the formatting of that text?  And
>how can I put it back in later if needed.  The clipboard contents should be
>preserved during all this.
>Thanks
>Bill

Re:RichEdit, Saving selected text with formatting


Thank you, I will give it a try.  Will it save just the selection, or
everything in RichEdit?

eyal wilde <e...@impactsoft.co.il> wrote in article
<6iq6c4$37...@forums.borland.com>...

Quote

> 'RichEdit1.SaveToStream' and 'RichEdit1.LoadFromStream'

Re:RichEdit, Saving selected text with formatting


Quote
> What is the easiest and least resource intensive way to save the selected
> text from a Rich Edit to memory keeping the formatting of that text?  And
> how can I put it back in later if needed.  The clipboard contents should be
> preserved during all this.

Bill,

for that you need to use the EM_STREAMIN and EM_STREAMOUT messages. Here is
some code that provides two methods to read and write the selection from/to a
stream. Please remember to rewind a stream you saved a selection in to
Position 0 before you try to put its content back into the rich edit control.

You need the richEdit Unit in your uses clause.

Type
  // These declarations are wrong in richedit.pas, the stdcall is missing.
  // So we redefine these types here.
  TEditStreamCallBack = function (dwCookie: Longint; pbBuff: PByte;
    cb: Longint; var pcb: Longint): Longint; stdcall;

  TEditStream = record
    dwCookie: Longint;
    dwError: Longint;
    pfnCallback: TEditStreamCallBack;
  end;

Function EditStreamInCallback(dwCookie: Longint; pbBuff: PByte;
    cb: Longint; var pcb: Longint): Longint; stdcall;
 var
   theStream: TStream;
   dataAvail: LongInt;
 begin
   theStream := TStream(dwCookie);

   with theStream do begin
     dataAvail := Size - Position;
     if dataAvail <= cb then begin
       pcb := Read(pbBuff^, dataAvail);
       Result := 0;
     end
     else begin
       pcb := Read(pbBuff^, cb);
       Result := pcb;
     end;
   end;
 end;

Function EditStreamOutCallback(dwCookie: Longint; pbBuff: PByte;
    cb: Longint; var pcb: Longint): Longint; stdcall;
 var
   theStream: TStream;
 begin
   theStream := TStream(dwCookie);

   with theStream do begin
     If cb > 0 Then Begin
       pcb := Write(pbBuff^, cb);
       Result := pcb;
     End
     Else
       Result := 0;
   end;
 end;

Procedure GetRTFSelection( aRichEdit: TRichEdit; 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: TRichEdit; 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;

Peter Below (TeamB)  100113.1...@compuserve.com)

Other Threads