Solved

RichEdit-Question ...

Posted on 2001-07-10
18
427 Views
Last Modified: 2013-11-22
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.
0
Comment
Question by:wimmeyvaert
  • 5
  • 4
  • 4
  • +2
18 Comments
 
LVL 5

Expert Comment

by:TheNeil
Comment Utility
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 =:)
0
 
LVL 6

Author Comment

by:wimmeyvaert
Comment Utility
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.
0
 
LVL 5

Expert Comment

by:TheNeil
Comment Utility
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 =:)
0
 
LVL 2

Expert Comment

by:egono
Comment Utility
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
0
 
LVL 2

Accepted Solution

by:
egono earned 150 total points
Comment Utility
you should do a reTmp.SelectAll before reTmp.CopyToClipboard;

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

good luck
0
 
LVL 3

Expert Comment

by:Stefaan
Comment Utility
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
0
 
LVL 6

Author Comment

by:wimmeyvaert
Comment Utility
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.
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
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.
0
 
LVL 3

Expert Comment

by:Stefaan
Comment Utility
This time I have to congratulate you with the good work ;-)

Best regards,


Stefaan
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 6

Author Comment

by:wimmeyvaert
Comment Utility
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.
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
include richedit in the uses-clause
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
like

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

0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
stefaan, do you mean me?
0
 
LVL 3

Expert Comment

by:Stefaan
Comment Utility
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
0
 
LVL 3

Expert Comment

by:Stefaan
Comment Utility
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.
0
 
LVL 6

Author Comment

by:wimmeyvaert
Comment Utility
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 ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
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
0
 
LVL 2

Expert Comment

by:egono
Comment Utility
wimmeyvaert - thanks
kretzschmar - really hot stuff
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now