Solved

RichEdit-Question ...

Posted on 2001-07-10
18
431 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 4
  • +2
18 Comments
 
LVL 5

Expert Comment

by:TheNeil
ID: 6269459
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
ID: 6269545
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
ID: 6269612
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:egono
ID: 6269913
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
ID: 6269922
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
ID: 6272010
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
ID: 6272030
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
ID: 6272252
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
ID: 6272277
This time I have to congratulate you with the good work ;-)

Best regards,


Stefaan
0
 
LVL 6

Author Comment

by:wimmeyvaert
ID: 6272483
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
ID: 6272503
include richedit in the uses-clause
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6272505
like

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

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6272515
stefaan, do you mean me?
0
 
LVL 3

Expert Comment

by:Stefaan
ID: 6272532
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
ID: 6272646
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
ID: 6272666
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
ID: 6272680
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
ID: 6273011
wimmeyvaert - thanks
kretzschmar - really hot stuff
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

726 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