I'd like to have a block of text in a TRichEdit to show up as follows when
the form opens. The user cannot edit it. It's for display purposes only. I
want to write code to enter the text in and format it during the create
event of the form. I need the strings of text to be added to the control
from within the exe (not from an html or other external file). Here's what
it should look like when it shows (Font is Tahoma 12 point with the other
formatting as shown. There should be 4 lines broken up as shown as well):
This copy of xyz software has been produced by abc technologies, inc.
and is for the sole purpose of managing the distribution of widgets that
are manufactured by abc technologies, inc. Use of xyz software is at
your own risk. There are no warranties expressed or implied.
Because I cannot show you bold, italic and colors in the above I'll explain
what it should look like:
Anywhere the string "xyz software" appears should be bold, italic and red.
In addition there should be a superscript "TM" after it. Anywhere the string
"abc technologies, inc" appears should be bold, italic and blue. I'd like
the string "at your own risk" to be bold black and underlined.
I figured this could be done with a series of "Add," "SelAttribute," and
other text formatting methods (making use of fsBold, fsItalic, clBlue, etc).
I just cannot find any example of how to do it. Is this not possible here?
With RichEdit1 do
begin
Add('some text string that would be added to the editor here');
Then Select some text of the text above using SelStart and SelLength
Font.Style = [fsBold, fsItalic];
Font.Color = clBlue;
End;
How is this done?
Thanks,
Keith
Set SelAttributes and then SelText repeatedly, e.g. here's an example doing
the first line (minus TM):
procedure TForm1.FillRichEdit;
procedure InsertText (const atext: string; acolor: TColor; astyles:
TFontStyles);
begin
with RichEdit1 do
begin
SelAttributes.Color := acolor;
SelAttributes.Style := astyles;
SelAttributes.Style := astyles;
SelText := atext;
end;
end;
begin
with RichEdit1 do
begin
SelAttributes.Name := 'Tahoma';
SelAttributes.Height := 12;
InsertText('This copy of ', clBlack, []);
InsertText('xyz software ', clRed, [fsBold, fsItalic]);
InsertText('software has been produced by ', clBlack, []);
InsertText('abc technologies, inc. ', clBlue, [fsBold, fsItalic]);
end;
end;
The problem with this is I don't see anyway to do superscript for TM, though
the control can certainaly display it. Therefore, I recommend pasting your
text into Word, marking it up as you want there, saving it in RTF. From
there, you can either load it directly from that file:
RichEdit1.Lines.LoadFromFile('c:\temp\xxx.rtf');
Or compile it into your application as a standard Windows resource you can
get at during runtime, save to a memory stream, and again then load it
using:
RichEdit1.Lines.LoadFromStream(myStream);
--
Wayne Niddery - Winwright, Inc (www.winwright.ca)
"Bandwagons are like streetcars, there'll be another along in a few
minutes."
----------
In article <44d3c526$1...@newsgroups.borland.com>, "Keith G Hicks"
<k...@comcast.net> wrote:
> I'd like to have a block of text in a TRichEdit to show up as follows when
> the form opens. The user cannot edit it. It's for display purposes only.
Richedit1.ReadOnly:=TRUE;
> I figured this could be done with a series of "Add,"
Generally avoid Add or any other direct manipulation of Lines.
Here it is essential to do it the proper way. shown below..
> "SelAttribute," and
> other text formatting methods (making use of fsBold, fsItalic, clBlue, etc).
> I just cannot find any example of how to do it. Is this not possible here?
>
> With RichEdit1 do
> begin
> Add('some text string that would be added to the editor here');
> Then Select some text of the text above using SelStart and SelLength
> Font.Style = [fsBold, fsItalic];
> Font.Color = clBlue;
No this sets the default colour and style for the whole component, not those
of the currrent selection.
> End;
>
> How is this done?
{put the caret to the end of the text, to add (or any other place you want
to insert at)}
Richedit1.SelStart:=$FFFFFFF;
{Select nothing (or select something to be replaced)}
Richedit1.SelLength:=0;
{set the attributes of the inserted text}
Richedit1.SelAttributes.Style = [fsBold, fsItalic];
Richedit1.SelAttributes.Color = clBlue;
{put the text in}
Richedit1.Seltext:='some text string that would be added to the editor
here';
> The problem with this is I don't see anyway to do superscript for TM,
quite right, (I missed this requirement!), there is no way to do superscript
just by manipulating Trichedit properties and methods..
> though
> the control can certainaly display it.
it can indeed.
> Therefore, I recommend pasting your
> text into Word, marking it up as you want there, saving it in RTF. From
> there, you can either load it directly from that file:
(But I think Keith wants it to be in the program not a file so the user
can't edit the file).
Anyway you should load it into a TRichedit and save it before shipping to
remove the garbage that Word puts in, such as a list of all fonts on the
*source* computer even if not used, the Printer name of the source
computer.. etc.
> RichEdit1.Lines.LoadFromFile('c:\temp\xxx.rtf');
>
> Or compile it into your application as a standard Windows resource you can
> get at during runtime, save to a memory stream, and again then load it
Another way to achieve superscript:
Put a very distinctive marker that will not occur elsewhere in the text as a
placeholder.
Save the Richedit to a temp file or stream. Reload it as plain text.
Find the marker and its preceding space.
Replace it with rtf superscript tags and the real text -
'\up6 TM\up0 '
Save, and reload with plaintext false.
(or there's a less kludgy way, by preparing a string containing fully formed
rtf of the superscripted stuff, and making a 'insertfromstring' method, that
would involve a customised call to EM_STREAMIN with different flags to the
standard LoadFromStream, so it inserts rather than replaces the whole text).
uses
RichEdit;
type
TCharacterFormat = (CFM_Superscript, CFM_Subscript, CFM_Normal);
procedure RE_SetCharFormat(RE: TRichEdit; CharacterFormat:
TCharacterFormat);
var
FontSize : integer;
Format: TCharFormat;
begin
FontSize := RE.SelAttributes.Size;
FillChar(Format, SizeOf(Format), 0);
with Format do
begin
cbSize := SizeOf(Format);
dwMask := CFM_OFFSET or CFM_SIZE;
case CharacterFormat of
CFM_Superscript: yOffset := (FontSize * 20) div 3; // 1/3 normal
CFM_Subscript: yOffset := (FontSize * -20) div 8; // 1/8 normal
CFM_Normal: yOffset := 0;
end;
yHeight := (FontSize * 40) div 3; // 2/3 normal
end;
RE.Perform(EM_SETCHARFORMAT, SCF_SELECTION, Longint(@Format));
end;
procedure AddDisclaimer(RE: TRichEdit; const SoftwareTitle: string);
var
sz, i : integer;
begin
(*
*)
with RE do begin
sz := Length(Text);
SelStart := sz +1;
SelAttributes.Name := 'Times New Roman'; // true type font
SelAttributes.Size := 10;
SelText := #13#10+
#9+ 'This copy of xyz software has been produced by abc
technologies, inc.' +#13#10+
#9+ 'and is for the sole purpose of managing the
distribution of widgets that' +#13#10+
#9+ 'are manufactured by abc technologies, inc. Use of
xyz software is' +#13#10+
#9+ 'at your own risk. There are no warranties expressed
or implied.' +#13#10;
// Note: 'at your own risk' has to be on one single line,
// otherwise FindText() cannon find it.
i := FindText('xyz software', sz, Length(Text) -sz, []);
while i > -1 do begin
SelStart := i;
SelLength := Length('xyz software');
SelAttributes.Color := clRed;
SelAttributes.Style := [fsBold, fsItalic];
SelText := ' ' +SoftwareTitle +' TM '; // add spaces for italics
SelStart := i +Length(' ' +SoftwareTitle +' ');
SelLength := 2;
RE_SetCharFormat(RE, CFM_Superscript);
i := FindText('xyz software ', i +1, Length(Text) -(i +1), []);
end;
i := FindText('abc technologies, inc', sz, Length(Text) -sz, []);
while i > -1 do begin
SelStart := i;
SelLength := Length('abc technologies, inc');
SelAttributes.Color := clBlue;
SelAttributes.Style := [fsBold, fsItalic];
SelText := 'abc technologies, inc '; // add a space for italics
i := FindText('abc technologies, inc', i +1, Length(Text) -(i
+1), []);
end;
i := FindText('at your own risk', sz, Length(Text) -sz, []);
while i > -1 do begin
SelStart := i;
SelLength := Length('at your own risk');
SelAttributes.Color := clBlack;
SelAttributes.Style := [fsBold, fsUnderline];
i := FindText('at your own risk', i +1, Length(Text) -(i +1), []);
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
AddDisclaimer(RichEdit1, 'Your software title');
end;
Keith
"Iain Macmillan" <he...@ariesps.co.uk> wrote in message
news:44d3...@newsgroups.borland.com...
In article <44d3efe6$1...@newsgroups.borland.com>, "Wayne Niddery [TeamB]"
<wnid...@chaffaci.on.ca> wrote:
> (But I think Keith wants it to be in the program not a file so the user
> can't edit the file).
This is correct.
"Keith G Hicks" <k...@comcast.net> wrote in message
news:44d559fb$1...@newsgroups.borland.com...
> Wait. You said something that sounds like I can enter the formatted
> text directly into the TRichEdit control during design time. I had no
> idea this was possible. How can I do that?
You can write the text in WordPad, save it to a rtf file, add the file
as a RCDATA resource to a resource script (RC) file, add that to your
project, and, at run-time, attach a TResourceStream to it and load the
richedit from that using its Lines.LoadFromStream method.
Here is an older post with a bit more detail:
Create a file textres.rc:
TESTDOC RCDATA "textdoc.rtf"
Compile this with brcc32 to textres.res. Include it into your project
with an
{$R textres.res}
line.
procedure TForm1.Button2Click(Sender: TObject);
var
rs: TResourceStream;
Begin
rs := TResourceStream.Create( hinstance, 'TESTDOC', RT_RCDATA );
try
richedit1.plaintext :=false;
richedit1.lines.loadfromstream(rs);
finally
rs.free;
end;
end;
Note that since D5 you can also simply add the RC file to your project
to get it compiled automatically. This also adds a $R statement to your
projects DPR file, so you do not need to add one manually.
--
Peter Below (TeamB)
Don't be a vampire (http://slash7.com/pages/vampires),
use the newsgroup archives :
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be
1. I created an rtf file in MS Word (yes, an rtf file, not a doc file) with
some formatting (underline, color, bold, etc) called Test.rtf.
2. I copied brcc32.exe and rw32core.dll to my c:\ root (to make the paths
easier in the DOS window)
3. I copied my Test.rtf into c:\ root also
4. In a DOS window (after running cd\ command) I typed "brcc32.exe Test.rtf"
(withouth the quotation marks) and hit enter. I get this error >>> Error
Test.rtf 1 1: Expecting resource name or resource type name
5. I also tried renaming my rtf file with an rc extention because I read
something on the web about that. Still didnt' work - same error.
What did I do wrong? According to
http://delphi.about.com/od/objectpascalide/l/aa021301b.htm compiling the res
file seems pretty clear. I think I'm missign a step - someting to do with
RCDATA but I can't find good information on that.
(using D7 by the way)
Keith
"Peter Below (TeamB)" <none> wrote in message
news:xn0epnfu...@newsgroups.borland.com...
Please re-read Peter's post. He included the following steps:
> Create a file textres.rc:
>
> TESTDOC RCDATA "textdoc.rtf"
>
> Compile this with brcc32 to textres.res.
This is what you should have done at step 4. The "textres.rc file is a
text file, and it contains only one line:
TESTDOC RCDATA "textdoc.rtf"
Compile that using brcc32.exe (or, as Peter said, just and textres.rc to
your project and the compiler will do the rest for you).
-- Stuart
> Create a file textres.rc:
>
> TESTDOC RCDATA "textdoc.rtf"
>
> Compile this with brcc32 to textres.res.
was telling me to run the line of code:
TESTDOC RCDATA "textdoc.rtf"
somewhere (delphi, dos window, ??) and then that line of code would create
textdoc.rc from textdoc.rtf. It was not clear that TESTDOC RCDATA
"textdoc.rtf" is supposed to be a line in a text file. Thanks for clearing
that up for me. I did what you said to do, added the rc file to the project,
compiled it and it runs fine. Nicely formatted text in my rich edit control.
Thanks again,
Keith
"Stu" <SstuP...@com.cast> wrote in message
news:44d6b820$1...@newsgroups.borland.com...
I absolutely sympathize - I remember being totally flummoxed by the
roundabout way resources are compiled and linked when I first had to do
this. Seemed arcane in the world of IDEs. Hence, the ability to add
the rc file to the project was a big convenience in D5, making it much
easier to support.
Glad this is working for you now.
-- Stu