Solved

Color Parsing

Posted on 1997-11-24
5
143 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
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.

758 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

20 Experts available now in Live!

Get 1:1 Help Now