2

我正在尝试将 TRichMemo 的内容保存到 TMemoryStream,然后能够将格式化的数据从流中加载回丰富的备忘录中。

问题是LoadRichText由于某种原因失败了。我知道数据已保存到我的流中,因为我实际上可以将其保存为 rtf 文件并在外部查看。

这基本上就是我所拥有的:

var
  FMyStream: TMemoryStream;

保存:

RichMemo1.SaveRichText(FMyStream);

装载:

FMyStream.Seek(0, soBeginning);
if not RichMemo1.LoadRichText(FMyStream) then
  raise Exception.Create('Failed to load data from stream.');

正如我所说,数据已保存以正确流式传输,但尝试加载到丰富的备忘录中每次都会遇到我的异常。

可能是什么问题呢?

LoadRichText函数的代码是:

function TCustomRichMemo.LoadRichText(Source: TStream): Boolean;
begin
  if Assigned(Source) and HandleAllocated then begin
    Result := TWSCustomRichMemoClass(WidgetSetClass).LoadRichText(Self, Source);
    if not Result and Assigned(RTFLoadStream) then begin
      Self.Lines.BeginUpdate;
      Self.Lines.Clear;
      Result:=RTFLoadStream(Self, Source);
      Self.Lines.EndUpdate;
    end;
  end else
    Result := false;
end;

SaveRichText代码:

function TCustomRichMemo.SaveRichText(Dest: TStream): Boolean;
begin
  if Assigned(Dest) and HandleAllocated then begin
    Result := TWSCustomRichMemoClass(WidgetSetClass).SaveRichText(Self, Dest);
    if not Result and Assigned(RTFSaveStream) then
      Result:=RTFSaveStream(Self, Dest);
  end else
    Result := false;
end;

谢谢。

4

1 回答 1

2

Ok, I found the solution to my problem.

At first I created a simple test project and LoadRichText and SaveRichText worked, which meant the problem was within my code somewhere...

My stream is declared in a class in a separate unit. In another form I have my rich memo control, when the form is closed the data is saved to the stream, that part I knew worked because I could save it to file and view it externally.

The problem was when I was creating the form that contains my rich memo, I was calling LoadRichText from the FormCreate event. So I moved it into FormActivate and now it works without error.

于 2013-09-11T12:31:34.520 回答