0

我编写了一个根据输入填充 RichEdit 组件的过程。

procedure LoadCPData(ResName: String);
begin
  ResName := AnsiLowercase(ResName) + '_data';
  rs := TResourceStream.Create(hInstance, ResName, RT_RCDATA);
  try
    rs.Position := 0;
    info.reMeta.Lines.LoadFromStream(rs);
  finally
    rs.Free;
  end;
end;

注意:上述过程存储在一个.pas名为 Functions 的外部文件中。

当我在表单中调用该过程时,RichEdit 仍然为空。但是,如果我将该代码块放在表单本身中,RichEdit 组件会按预期填充数据而不会出现问题。case现在我可以将上面的代码块放在表单本身中,但我计划在一个语句中多次使用该过程。

为了使我的程序起作用,我需要包括什么?

提前谢谢你!

4

1 回答 1

1

我们使用TJvRichEdit控件代替,TRichEdit以便我们可以支持嵌入的 OLE 对象。这应该与TRichEdit.

procedure SetRTFData(RTFControl: TRichEdit; FileName: string);
var
  ms: TMemoryStream;
begin
  ms := TMemoryStream.Create;
  try
    ms.LoadFromFile(FileName);
    ms.Position := 0;
    RTFControl.StreamFormat := sfRichText;
    RTFControl.Lines.LoadFromStream(ms);
    ms.Clear;

    RTFControl.Invalidate;

    // Invalidate only works if the control is visible.  If it is not visible, then the
    // content won't render -- so you have to send the paint message to the control
    // yourself.  This is only needed if you want to 'save' the content after loading
    // it, which won't work unless it has been successfully rendered at least once.
    RTFControl.Perform(WM_PAINT, 0, 0);
  finally
    FreeAndNil(ms);
  end;
end;

我从另一个例程中改编了这个,所以它不是我们使用的完全相同的方法。我们从数据库流式传输内容,因此我们从不从文件中读取。但是我们确实将字符串写入内存流以将其加载到 RTF 控件中,所以这本质上是做同样的事情。

于 2012-10-05T02:45:02.620 回答