Re:TRichEdit : saving and getting back rich text from an untyped file
Quote
> I save the text along with other fields in a binary file. When I read back
> the file and get the richtext string how do I put that in the Rich Text box
> and show the text with formatting rather than the whole richtext encoded
> string?
Paul,
since the richedit.lines.loadfromstream always reads to the end of stream you
will have to first extract the rich text part from your stored data, into an
auxillary memory stream. I hope you write the length of the data before you
store the rich text data or you will have trouble finding its end!
Example (UNTESTED!):
Procedure SaveRichEdit( re: Trichedit; toStream: TStream );
Var
ms: TMemoryStream;
oldPlaintext: Boolean;
numBytes: Integer;
Begin
ms:= TMemoryStream.Create;
try
oldPlainText := re.Plaintext;
try
re.Plaintext := False;
re.lines.savetostream( ms );
finally
re.Plaintext := oldPlaintext;
end;
ms.Position := 0;
numBytes := ms.Size;
toStream.WriteBuffer( numBytes, Sizeof( numBytes ));
toStream.CopyFrom( ms, numbytes );
finally
ms.free
end;
End;
Procedure LoadRichEdit( re: Trichedit; fromStream: TStream );
Var
ms: TMemoryStream;
oldPlaintext: Boolean;
numBytes: Integer;
Begin
fromStream.ReadBuffer( numBytes, Sizeof( numBytes ));
ms:= TMemoryStream.Create;
try
ms.CopyFrom( fromStream, numbytes );
ms.Position := 0;
oldPlainText := re.Plaintext;
try
re.Plaintext := False;
re.lines.loadfromstream( ms );
finally
re.Plaintext := oldPlaintext;
end;
finally
ms.free
end;
End;
Note that if you store mixed binary data into a stream your reading code must
exactly mirror the writing code, otherwise you will mix up the data something
fierce. If you call LoadrichEdit and the stream you pass it is not sitting on
the length of the data, followed by the rich text it will load garbage!
The length has to be accurate, the rich edit control is very fickle when it
comes to streaming its contents: you try to load data that does not start
with a '{\rtf1' and end with a '}', with the proper balance of braces in
between, and it will show the data as plain text!
Peter Below (TeamB) 100113.1...@compuserve.com)
No e-mail responses, please, unless explicitely requested!