将 RTF 文本从数据库加载到 TRichEdit [英] Loading RTF text from database into TRichEdit

查看:34
本文介绍了将 RTF 文本从数据库加载到 TRichEdit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将我们的软件解决方案从 Delphi 7 迁移到 2010.大部分更改都很简单,只剩下少量障碍.

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.

在一个表单上,我们使用了一个 TRichEdit,它显示了从 MSSQL 数据库中的 blob 字段中抓取的 rtf 文本.这是它在 Delphi 7 中的工作方式:

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);

这将在 TRichEdit 组件中按预期显示 RTF,但 Delphi 2010 中的相同代码将 RTF 显示为纯文本,每个字符之间带有制表符.我认为这与从 Ansi 到 Unicode 的变化有很大关系,但我没有运气纠正这个问题.

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

推荐答案

好的,我想通了.

用于加载 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;

这花了我太长时间才弄明白:) 我想我已经学到了一件事……如果 Delphi 2010 出现问题,它通常与 unicode 相关 ;)

This took me way too long to figure out :) I guess I have learned one thing though... if something goes wrong in Delphi 2010, its usually related to unicode ;)

这篇关于将 RTF 文本从数据库加载到 TRichEdit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆