Link to home
Start Free TrialLog in
Avatar of davidgreen
davidgreen

asked on

RichText Control Problem

I have a string containing richtext code copied from notepad so it doesn't reconize it as richtext. The problem is how do I get a richedit control to recognize this as richtext again? If I just paste it into the richtext control or assign the string to the edit or lines property of the richtext control all I get is a display of the codes. Any ideas guys?
Avatar of saar071697
saar071697

it is very simple just PASTE it into the richedit
Avatar of davidgreen

ASKER

Have you got an example of this Saar? How do I go from the string - to the clipboard - to the richtext control and have it recognized as richtext format. If I open a richtext document in notepad, copy the text, including codes and then paste into a richtext control (even wordpad) all I get is a display of codes again, it doesn't display with formatting.
I know I can easily load it via LoadFromFile but I want to be able to do it in memory... could LoadFromStream be used?
Never did it, but my guess is that word, wordpad or whoever, registers a clipboard format for that.

You need to use the clipboard hasformat methos and formats array to identify this, and then copy the text buffer to the richedit.lines or to a file and the load it.

Hope this helps
I have investigated this approach but it doesn't seem to work. It seems this simple sounding problem is harder than it first apprears.

Any expert programmers out there wanting a challenge?
Didn't like your reply, so I did some digging, and I was right.

Just use RichEdit1.PasteFromClipboard;  

That'll do the trick and it does just what I said.

:-)
ssites, I didn't mean to sound condescending. Say for example I have the following code in a memo control

{\rtf1\ansi\deff0\deflang1033{\fonttbl {\f0\fswiss
Arial;}}
{\colortbl ;\red0\green0\blue0;}
\uc1\pard\cf1\ulnone\f0\fs144 D\ul A\ulnone\b\i
V\b0\i0 E\fs16\par
}

If I save that to a file with a rtf extension it will load and display 'dave' complete with formatting. If I just do a copy and paste to a richtext edit control all I end up with is the above text. How do I copy it to a richedit control and display it with formatting? Did you create an example that worked?

DG
David, Only now, I understand what you want to do. Look at yur questions, you never mentioned a memo control before. I'm going to look it up and let you know.
Hi guys

i've got a little work-around for this problem:
why don't you save the whole text (after pasting in the formatted text) in the richedit-control
as normal text and load them again as rtf-text.
(i know, this isn't so smart, but i haven't a better solution
at the moment)

regards
rene100
ups, sorry guys, my answer above should be comment, please
reject it.
I got the answer, dave. Reject that answer and I'll post it.
Rene100 - I really need to do this is memory, the number of conversions of this sort that needs to be done would be horrid if I had to save to disk and reload.

Fire away ssite, I still haven't found a solution yet.

Hi all,

I have done this before with EM_STREAMIN and EM_STREAMOUT, using a callback function.  I'll let ssite post first, and if the answer doesn't user EM_STREAMIN/OUT or doesn't work, I will post some code.

Cheers,
JB

Appreciated JimBob.... This one is driving me crazy!!

DG
horus@vmicro.com

Ok, Dave, here is what I did.

Notes:
RichMemStream is a global variable that can be accessed by the callback (EdStreamCallBack).  You must get the raw text into this stream.  In my simple example I use RichMemStream.LoadFromFile, and I load a text file.

My e.g. assumes your rich edit is called rchTest.

(EdStream.dwCookie is used to tell the callback whether you are reading or writing to the stream.  It's a user-defined value...)

=== START OF CODE ===
var
  RichMemStream: TMemoryStream;

function EdStreamCallBack(dwCookie: Longint; pbBuff: PByte; cb: Longint; var pcb: Longint): Longint; stdcall;
begin
      if (dwCookie = 1) then
            begin                  // EM_STREAMOUT
                  RichMemStream.SetSize(cb);
                  CopyMemory(RichMemStream.Memory, pbBuff, cb);
                  Result := 1;
            end
      else
            begin            // EM_STREAMIN
                  CopyMemory(pbBuff, RichMemStream.Memory, RichMemStream.Size);
                  pcb := RichMemStream.Size;
                  Result := NOERROR;
            end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
      EdStream: TEditStream;
begin
      RichMemStream := TMemoryStream.Create;

      FillChar(EdStream, SizeOf(TEditStream), 0);

  { To get the content of a rich edit into the stream, do the following:
      EdStream.dwCookie := 1;
      EdStream.pfnCallback := @EdStreamCallBack;
      rchFrom.Perform(EM_STREAMOUT, SF_RTF, Integer(@EdStream));}

  { In your case, however, you want to put the stream's text INTO the rich edit.
  Thus, do the following: }

  // You must some how get a stream with the raw text.
  // In this simple example I use Stream.LoadFromFile...
  RichMemStream.LoadFromFile('C:\Test.txt');
      EdStream.dwCookie := 2;
  EdStream.pfnCallback := @EdStreamCallBack;
      RichMemStream.Position := 0;
      rchTest.Perform(EM_STREAMIN, SF_RTF, Integer(@EdStream));

      RichMemStream.Free;
end;
=== END OF CODE ===

Cheers,
JB
I was asleep and jimbob beet me to it. Although there's a slightly easier way which is to use
richedit.lines.loadfromstream;

:-)
Sorry, ssite, I didn't mean to push in.  ;-)

I originally used RichEdit.Lines.LoadFromStream, but I encountered problems.  This was several months ago, but I remember that I had several problems which I couldn't sort out.  Hence the EM_STREAMIN/OUT approach.

If you can give Dave Green a method with Lines.LoadFromStream he may prefer that.  I would like it too!  :-)

Cheers,
JB
Thanks JimBob, I really appreciate your help. I'm going to experiment with your code and see if this solves it.

Ssite I would be interested in seeing your approach as well.

Big question is - how do I reward you both?!

Hehe...  the reward?  If ssite gives you code using Lines.LoadFromStream, you'll probably prefer that approach, so the points should go to ssite.

JB
No way ! jimbob did the work (jumbob : you didn't push in). Let him have it ! :-)
Thanks SSite - JimBob, answer the question and I'll grade your answer.

SSite: What were your ideas on loadfromstream? Have you got a code snippet?

Thanks alot guys for your help, its appreciated.

DG.


The code is extremely simple : create a TMemorystream, use savetostream from the memo and then use loadfromstream on the richedit control. should work !  But then jumbob says there're problems with this method.
Hi all,

Before I submit an answer, I must say that the following DOES work.  (memTest is a TMemo; rchTest is a TRichEdit.)

var
  MemStr: TMemoryStream;
begin
  MemStr := TMemoryStream.Create;
  memTest.Lines.SaveToStream(MemStr);
  MemStr.Position := 0;
  rchTest.Lines.LoadFromStream(MemStr);
  MemStr.Free;
end;

(It's important to set MemStr.Position := 0.)

However, I remember having other problems when using this technique, but I can't find the sample program that I was working on to remind myself of what the problems were!

If the above code works for your needs, then ssite MUST get the points as this is by far an easier solution.  Please ssite, no more arguing, just answer the question!!  :-)

JB
Hi again

I was playing around and I remembered what I was having problems with.  I had 2 richedits, and I was trying to copy the text of one into the other.  This worked fine if #1 was richly formatted, but not if #1 had the raw RTF text.

JB
Jimbob, what david wants to do is copy from a memo, and that worked for me fine.
Yeah, that's what I was getting at in my last 2 comments (which should have been one comment).  My problem (when I eventually found it again) was between 2 RichEdits when one of them had the raw text with all the control characters.

JB
GREAT! JimBob... the above does work... and is much simpler than the other code. Thank you JB and SSite... I wish I had 1,000,000 points to give you both.

SSite... JB wants you to have the points.. let's put this to bed.

THANKS AGAIN....
ASKER CERTIFIED SOLUTION
Avatar of ssite
ssite

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
Thanks :))))))