Change text attributes without using RVStyles?

General TRichView support forum. Please post your questions here
Post Reply
miyomo
Posts: 5
Joined: Fri Apr 13, 2007 6:13 am

Change text attributes without using RVStyles?

Post by miyomo »

Hi! In a richedit if I would color a single word I use this code.

SelAttributes.Color := clRed;
SelText := 'Hi World!';
SelAttributes := DefAttributes;

Is it possible to do the same in a Richview without using RVStyles?
How can I change text attributes (font, backcolor, color, styles...) runtime?

My application needs to use text attributes choose by users, several times for every single line.

eg

word 1000 2000 word word word


I've should have to align colums, maybe without use of fixed font..

Thanks!
Sergey Tkachenko
Site Admin
Posts: 17569
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Styles are the only way to implement this.
How do you plan to define these attributes in your application?
If using TRichViewEdit, see the demo Demos\Delphi\Editors\Editor 2\ (or use RichViewActions).
If using some commands in text string, see this demo http://www.trichview.com/forums/viewtopic.php?t=63
miyomo
Posts: 5
Joined: Fri Apr 13, 2007 6:13 am

Post by miyomo »

Sergey Tkachenko wrote:Styles are the only way to implement this.
How do you plan to define these attributes in your application?
Hi Sergey! Thanks for reply.

Actually I use a richedit loading a simple file text and formatting it with colors and styles.
The richedit don't need to be editable, but users can define styles and colors in the general options.
Now I use the procedure below to format text and I was wondering is there is a way like that to do the same.

Richedit, as you know :-) , is heavily limited and i'm trying your trichview to see if I can implement some features, like tables (to align colums without using fixed font) or preview.


Thanks for the help!

**
procedure AddText(RichEdit: TRichEdit; sText:string; FontColor, BackColor: TColor; FS:TFontStyles);
var
Format: CHARFORMAT2;
begin
FillChar(Format, SizeOf(Format), 0);
with Format do
begin
cbSize := SizeOf(Format);
dwMask := CFM_BACKCOLOR;
crBackColor := BackColor;
Richedit.Perform(EM_SETCHARFORMAT, SCF_SELECTION, Longint(@Format));
end;

RichEdit.SelAttributes.Color := FontColor;
RichEdit.SelAttributes.Style := FS;
RichEdit.SelText := sText;
RichEdit.SelAttributes.Color := clWindowText;
end;
Sergey Tkachenko
Site Admin
Posts: 17569
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Code: Select all

procedure InsertText(rve: TCustomRichViewEdit; const sText:string;
  FontColor, BackColor: TColor; FS:TFontStyles);
var TextStyle: TFontInfo;
    StyleNo: Integer;
begin
  // creating text style with the desired attributes
  TextStyle := TFontInfo.Create(nil);
  TextStyle.Assign(rve.Style.TextStyles[rve.CurTextStyleNo]);
  TextStyle.Color := FontColor;
  TextStyle.BackColor := BackColor;
  TextStyle.Style := FS;
  // if such style already exists in rve.Style.TextStyles, using the existing one
  StyleNo := rve.Style.TextStyles.FindSuchStyle(
    rve.CurTextStyleNo, TextStyle, RVAllFontInfoProperties);
  if StyleNo<0 then begin
    // if does not exist, adding it
    rve.Style.TextStyles.Add;
    StyleNo := rve.Style.TextStyles.Count-1;
    rve.Style.TextStyles[StyleNo].Assign(TextStyle);
    rve.Style.TextStyles[StyleNo].Standard := False;
  end;
  TextStyle.Free;
  // inserting text
  rve.CurTextStyleNo := StyleNo;
  rve.InsertText(sText);
end;
Post Reply