从外部过程填充RichEdit

问题描述 投票:0回答:1

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

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

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

为了使我的程序有效,我需要包括哪些内容?

先谢谢你!

delphi delphi-xe richedit
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控件中,因此这本质上也是如此。

© www.soinside.com 2019 - 2024. All rights reserved.