9

I am currently in the process of migrating our software solution from Delphi 7 to 2010. Mostly the changes have been simple and there are only a small amount of hurdles left to go.

On a form we use a TRichEdit which displayed rtf text grabbed from a blob field in a MSSQL db. This is how it worked in Delphi 7:

//Get RTF text from Blob field using TADOQuery
rtfStream := sql.CreateBlobStream(sql.FieldByName('rtftext'), BmRead) as TMemoryStream;

//Load into TRichEdit
RichEdit.PlainText := False;
RichEdit.Lines.LoadFromStream(rtfStream);

This would display the RTF as expected in the TRichEdit component, but the same code in Delphi 2010 displays the RTF as plain text with tabs between each character. I assume this has a lot to do with the change from Ansi to Unicode, but I haven't had any luck rectifying the problem.

Any help getting this to work would be much appreciated. Thanks

4

2 に答える 2

13

わかりました。

rtfテキストをロードする場合:

//Get the data from the database as AnsiString
rtfString := sql.FieldByName('rtftext').AsAnsiString;

//Write the string into a stream
stream := TMemoryStream.Create;
stream.Clear;
stream.Write(PAnsiChar(rtfString)^, Length(rtfString));
stream.Position := 0;

//Load the stream into the RichEdit
RichEdit.PlainText := False;
RichEdit.Lines.LoadFromStream(stream);

stream.Free;

rtfテキストを保存するには:

//Save to stream
stream := TMemoryStream.Create;
stream.Clear;

RichEdit.Lines.SaveToStream(stream);
stream.Position := 0;

//Read from the stream into an AnsiString (rtfString)
if (stream.Size > 0) then begin
    SetLength(rtfString, stream.Size);
    if (stream.Read(rtfString[1], stream.Size) <= 0) then
        raise EStreamError.CreateFmt('End of stream reached with %d bytes left to read.', [stream.Size]);
end;

stream.Free;

//Save to database
sql.FieldByName('rtftext').AsAnsiString := rtfString;

これは私が理解するのに時間がかかりすぎました:)私は1つのことを学んだと思います...Delphi2010で何かがうまくいかない場合、それは通常ユニコードに関連しています;)

于 2010-11-03T04:05:34.353 に答える
5

PlainText が False の場合、LoadFromStream() は最初に RTF コードのロードを試み、それが失敗した場合、LoadFromStream() はストリームをプレーン テキストとして再度ロードしようとします。これは、すべての Delphi バージョンで常に当てはまります。Unicode の導入により、LoadFromStream() のEM_STREAMINコールバック ハンドラで何かが壊れた可能性があると思います。デバッガーを使用して LoadFromStream() の実際のソース コードにアクセスし、実際に何が起こっているかを確認することをお勧めします。

于 2010-11-02T18:41:16.780 に答える