从外部过程填充 RichEdit



我编写了一个根据输入填充 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文件中。

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

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

提前谢谢你!

我们使用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 控件中,因此这本质上执行相同的操作。

最新更新