0

I have written a procedure that fills a RichEdit component depending on the input.

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;

Note: The above procedure is stored in an external .pas file called Functions.

When I go to call the procedure in my form the RichEdit remains empty. However, if I were to place that code block in the form itself, the RichEdit component fills the data without a problem as expected. Now I could place the above code block in the form itself, but I plan on using the procedure multiple times in a case statement.

What will I need to include in order for my procedure to work?

Thank you in advanced!

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
ple103
  • 2,080
  • 6
  • 35
  • 50
  • Is the external file you are loading a valid RTF file? I ask because I load the RTF a little differently than you've shown in your code, and it always works for me -- and the code is in a separate unit. – James L. Oct 05 '12 at 02:14
  • Yes I'm sure it should be a valid RTF file because I have gotten it to load internally. But what do you mean by valid? Would it be possible for you to share you code? – ple103 Oct 05 '12 at 02:20
  • I'll post what I do, though it is quite similar to what you've done... I do add a couple more things though. Perhaps they will do the trick. – James L. Oct 05 '12 at 02:46

1 Answers1

1

We use the TJvRichEdit control instead of TRichEdit so that we can support embedded OLE objects. This should work very similarly with 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;

I adapted this from another routine, so it isn't the exact same method we use. We stream the content from a database, so we don't ever read from a file. But we do write the string on to a memory stream to load it into the RTF control, so this in essence does the same thing.

James L.
  • 9,384
  • 5
  • 38
  • 77
  • 1
    As your comment says, Invalidate only works if the control is visible, and the way to `send a paint message` isn't to call `WMPaint`; it's to either `SendMessage` or `PostMessage` to the `RTFControl.Handle` or use `RTFControl.Perform`. You also use `PaintMsg`, which isn't defined anywhere in your code. Half of what you posted is invalid or incorrect code, or comments. – Ken White Oct 05 '12 at 02:49
  • Your edit helps, but you're still doing it wrong. You do **not** call `WMPaint` directly; you post or send the `WM_PAINT` message to the control using `PostMessage` or `SendMessage`, or just use `RTFControl.Perform(WM_Paint, 0, 0)`, which is all your PaintMsg would do. It's not the solution here to the problem asked, either. – Ken White Oct 05 '12 at 02:54
  • The question asked was whether or not the code that loads the RTF into the control can be in a separate unit or not. It works with the code that I use and the OP asked to see that code... – James L. Oct 05 '12 at 02:56
  • Turns out that the extra things I did in `WMPaint()` are irrelevant to the OP's question, so I simplified the example. Thanks for your comments @KenWhite – James L. Oct 05 '12 at 02:59
  • Thanks James, but I'm not sure I have the `TJvRichdit` component. Is this a base component or third-party? – ple103 Oct 05 '12 at 03:14
  • Give it a try with your `TRichEdit`. It should work because that is what we were using before we switched to `TJvRichEdit`, which is part of the JEDI JVCL - a 3rd party library of controls. – James L. Oct 05 '12 at 03:15