Link to home
Start Free TrialLog in
Avatar of wimmeyvaert
wimmeyvaert

asked on

RichEdit-Question ...

Hi experts,

In my application, I want to show the contents of an RTF-File WITH FORMAT, but I can't get this to work. I only see the text, but with no formatting.

I use the LoadFromFile-Method to get the RTF-File loaded into my richedit.
PlainText-prop is set to False.

The formatting I use in the RTF-File is just coloring some words and some indentation (nothing spectacular).

Can anybody help me showing RTF-Files with format in my RichEdit?

Thanx very much,
The Mayor.
Avatar of TheNeil
TheNeil

You should just be able to do this:

RichEdit1.Lines.LoadFromFile('MyFile.rtf');

Just using the properties as set when you drop the TRichEdit straight onto the form will be enough to get formatting (ie no modification needed)

The Neil =:)
Avatar of wimmeyvaert

ASKER

Hi TheNeil,

I tested your suggestion in a new simpler app and indeed, it worked fine.
I searched the code of my original and I found what probably causes my problem :

There is a for-loop in which a condition is tested. If the condition is True, the a specific RTF-File (depending on the counter of the do-loop) should be ADDDED to the content of the RichEdit on the form.
Therefore the following code is used :
    reTmp.Lines.LoadFromFile(strActionPath + IntToStr(intX) + '.Rtf');
    reActions.Lines.AddStrings(reTmp.Lines);

By doing this (AddStrings) I suppose you lose all the formatting. Is there a workaround for this that you know about ?
If you can give me a good suggestion, then I'll grant you the points !!!

Best regards,
The Mayor.
As soon as you start accessing the lines directly you do lose the formatting data. You CAN access it by going to the paragraph data but I can't think of a quick way to get the data across (Assign loses the formatting too).

Why not simply do this:

reTmp.Lines.LoadFromFile(strActionPath + IntToStr(intX) + '.Rtf');
reActions.Lines.AddStrings(strActionPath + IntToStr(intX) + '.Rtf');

Ok it's not a groundbreaking solution but it will get the formatting into both controls. If you're some sort of masochist and desperately want to copy the data from reTmp to reActions then I can work out the code and post it but it'll be a lot slower and you won't really gain anything

The Neil =:)
you might using the clipboard

//untested on
reTmp.Lines.LoadFromFile(strActionPath + IntToStr(intX) + '.Rtf');
reTmp.CopyToClipboard;
//look for the end of reActions
reActions.SelectAll;
reActions.SelStart:=reActions.SelLength;
reActions.SelLength:=0;
reActions.PasteFromClipboard;
//untested off

good luck
ASKER CERTIFIED SOLUTION
Avatar of egono
egono

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Wim,

Met al uw moelijke vragen altijd ;-P


Well my suggestion would be to load everything into a normal memo first.  So load the original file into a normal Memo, add the lines that should be added, let the normal Memo save everything to a new file and read the new file in the RichEdit.

That should do the trick ;-)


Groeten,


Stefaan
Hi TheNeil, egono and Stefaan,

Right now I don't have much time (as usual), but I'll try all your suggestions later on this day.

Please be patience, I'll be back ...

Best regards,
The Mayor.

P.S. for Stefaan : Ja jong, ik kan u toch niet altijd lastig vallen met mijn ambetante vragen he. Groeten aan
uw vrouwmens en de kleine Lesage.
Avatar of kretzschmar
from my paq:

sure,
the other way (insert rtf-content into rtf-doc without copy/paste)
is a little bit more complex (my hair is grayed by figure out this, because
the documentation is wrong).

a working sample

unit riched_in_u;

interface

uses
 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
 StdCtrls, ComCtrls, richedit;  //richedit is used

type
 TForm1 = class(TForm)
   RichEdit1: TRichEdit;
   Button1: TButton;
   OpenDialog1: TOpenDialog;
   procedure Button1Click(Sender: TObject);
   procedure FormCreate(Sender: TObject);
   procedure FormClose(Sender: TObject; var Action: TCloseAction);
 private
   { Private-Deklarationen }
 public
   { Public-Deklarationen }
 end;

Function MyEditStreamCallBackIn(dwCookie: Longint; pbBuff: PByte;
                               cb: Longint; var pcb: Pointer): Longint; stdcall;


var
 Form1: TForm1;
 L : Longint = 0;
 EditStream : TEditStream;
 ms : TMemoryStream;

implementation

{$R *.DFM}


Function MyEditStreamCallBackIn(dwCookie: Longint; pbBuff: PByte;
                               cb: Longint; var pcb: Pointer): Longint; stdcall;
var
 dw : DWord;
Begin
 pcb := @cb;
 if cb > ms.Size - ms.position then
   dw := ms.Size - ms.position
 else
   dw := cb;
 if dw <> 0 then
   ms.ReadBuffer(pbbuff^,dw);
 result := 0;  //Must allways 0
 dw := ms.position;
 move(dw,pcb,sizeof(dw));
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 if opendialog1.execute then
 begin
   ms.LoadFromFile(opendialog1.filename);
   ms.Position := 0;
   editStream.dwCookie := SF_RTF or SFF_SELECTION;
   editStream.pfnCallback := @MyEditStreamCallBackIn;
   Richedit1.Perform(EM_StreamIn,SF_RTF or SFF_SELECTION,DWord(@EditStream));
 end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 ms := TMemoryStream.Create;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 ms.Free;
end;

end.

the rtf-content is inserted at cursorpos or replaced a selection.
This time I have to congratulate you with the good work ;-)

Best regards,


Stefaan
Hi Experts,

To TheNeil :
Your suggestion (reActions.Lines.AddStrings(strActionPath + IntToStr(intX) + '.Rtf'); ) won't work cause the AddStrings-Method needs a TStrings and what you gave me is just a string (= the name of a file).


To egono :
Your suggestion seems to work !!!! I'm just gonna evaluate the suggestion of Kretzschmar and then I'll grant the points to the best solution !!

To Stefaan :
Your suggestion is not very helpfull to me, because the files I want to load are RTF-Files with formatting in it.
If you load these into TMemo, I suppose you lose the formatting. Sorry

To Kretzschmar :
I want to test your suggestion, but I  can't compile it
(TEditStream is not known, and I cant find anything in the Help). Any suggestions ?

Best regards,
The Mayor.
include richedit in the uses-clause
like

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, richedit;  //richedit is used

stefaan, do you mean me?
Hi,

Kretzschmar, yes I meant you.

Wim, well you could read in the first part into the normal memo as pure text, add the rest of the RTF text to the same memo using add lines and then save the complete contents back to a new RTF file which you can then use to read into a RichEdit.

I was only giving that suggestion to simply allow you to add the additional lines using the AddLines method of the simpel memo.  I will send you a demo application in a minute so you can see what I meant.

Best regards,


Stefaan
Wim,

An even much easier way to achieve what you do is to simply copy the old contents of the EditMemo to the clipboard.  Load in the new file.  Set the selection to the begining of the RichEdit and past the contents back into it :

Read in the First part :

  redtRichEdit2.Lines.LoadFromFile('C:\ToBeDeleted\FirstPart.rtf' );


Append with a second part :

  redtRichEdit2.SelectAll;
  redtRichEdit2.CopyToClipboard;

  redtRichEdit2.Lines.LoadFromFile('C:\ToBeDeleted\SecondPart.rtf' );
  redtRichEdit2.SelStart := 0;
  redtRichEdit2.SelLength := 0;
  redtRichEdit2.PasteFromClipboard;


Voila, I tested it out and it works.

Best regards,


Stefaan.
Hi Experts,

Kretzschmar,
  Your suggestion works fine, but for 'heavy formatted' RTF's I get an AV in RichEd32.DLL

Therefore, I'm gonna grant egono the points.

Stefaan, I hope you have not already started to make your demo. I ncase you haven't don't start makeing one.
In case you already have started, stop right now !! ;-)

Anyway, I want to thank everyone for the quick replies.

Best regards,
The Mayor

P.S. Kretzschmar, I hope your hair has not turned completely white by putting all the time in the suggestion you made ;-)
not at all, wim, my hair is brown again ;-)

>but for 'heavy formatted' RTF's I get an AV in RichEd32.DLL

guessing thats a timing problem, because the riched32.dll is parsing/rendering the incoming content, or maybe i have missed something
wimmeyvaert - thanks
kretzschmar - really hot stuff