Solved

Color Parsing

Posted on 1997-11-24
5
146 Views
Last Modified: 2010-04-04
I want to display text on a trichedit component with different colors. I use a memory stream to enter raw rtf codes. But now my problem is how can i add the stream to the richedit leaving the contenent in tact.

Regards, Arijan
0
Comment
Question by:byte
  • 3
  • 2
5 Comments
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1351658
Hi

Try the following:
procedure FillRichEdit(ARichEdit: TRichEdit);
var
  MS: TMemoryStream;
begin
  MS := TMemoryStream.Create;

  // Fill memory stream with codes.
  // E.g.  MS.LoadFromFile('C:\Test.rtf');

  ARichEdit.Lines.LoadFromStream(MS);
  MS.Free;
end;

Regards,
JB
0
 

Author Comment

by:byte
ID: 1351659
Thanks i was so far =) but the problem with this method is that the contents of the richedit will be erased. I want to keep it.

Regards, Byte
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1351660
If you want to append to the existing text then why not save to the memory stream first, then add to the stream, then load it again?

The other way is to send the rich edit control an EM_STREAMIN message, but it's a lot more work and you have to provide a callback function.  I can give you a sample of this if you need one.

JB
0
 

Author Comment

by:byte
ID: 1351661
That would be great. Thx!
0
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 200 total points
ID: 1351662
The following sample inserts text at the position of the cursor.
My RichEdit control is called rchTest.

uses
  ..., RichEdit;

var
  RichMemStream: TMemoryStream;

function EdStreamCallBack(dwCookie: Longint; pbBuff: PByte; cb: Longint; var pcb: Longint): Longint; stdcall;
begin
  CopyMemory(pbBuff, RichMemStream.Memory, RichMemStream.Size);
  pcb := RichMemStream.Size;
  Result := NOERROR;
end;

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

  RichMemStream.LoadFromFile('C:\Test.txt');

  FillChar(EdStream, SizeOf(TEditStream), 0);
  EdStream.pfnCallback := @EdStreamCallBack;

  RichMemStream.Position := 0;
  rchTest.Perform(EM_STREAMIN, SF_RTF or SFF_SELECTION, Integer(@EdStream));

  RichMemStream.Free;
end;


Note that I fill the memory stream by loading it from C:\Test.txt, which has the following content:

{\rtf1\ansi\deff0\deftab720
{\fonttbl{\f0\fswiss MS Sans Serif;}
{\f1\froman\fcharset2 Symbol;}
{\f2\fswiss\fcharset1 MS Sans Serif;}}
{\colortbl\red0\green0\blue0;}
\deflang1033\pard\plain\f2\fs16
\par \b Hello there.\plain
}


Regards,
JB
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

821 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