whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

33 Visitors Online


 
...color text in a TRichEdit?
Autor: Thomas Stutz
[ Print tip ]  

Tip Rating (40):  
     


{
  To color text in a TRichEdit, follow this two steps:

  Um einen Text in einem TRichEdit einzufärben, müssen folgende 2 Schritte
  gemacht werden:

  1) Select the text with the SelStart, SelLength properties.
     Markiere den Text mit SelStart, SelLength Eigenschaften.

  2) Set the text attribtutes through the SelAttributes property.
     Die Textattribute mit SelAttributes setzen.
}

{
  1. Example/ Beispiel:

  Add a colored line to a TRichEdit:
  Eine farbige Zeile zu einem TRichEdit hinzufügen:
}

procedure AddColoredLine(ARichEdit: TRichEdit; AText: string; AColor: TColor);
begin
  with 
ARichEdit do
  begin
    
SelStart := Length(Text);
    SelAttributes.Color := AColor;
    SelAttributes.Size := 8;
    SelAttributes.Name := 'MS Sans Serif';
    Lines.Add(AText);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  
AddColoredLine(RichEdit1, 'Hallo', clRed);
  AddColoredLine(RichEdit1, 'Hallo', clGreen);
end;

{
  2. Example/ Beispiel:

  To color the 5 characters.
  Die ersten 5 Zeichen im Richedit blau einfärben.
}


procedure TForm1.Button1Click(Sender: TObject);
begin
  
RichEdit1.SelStart  := 0;
  RichEdit1.SelLength := 5;
  RichEdit1.SelAttributes.Color := clBlue;
end;

{
  3. Example/ Beispiel: ( by www.delphimania.de)

  To color a specified line with a color
  So kann eine beliebige Zeile mit einer Farbe gefärbt werden:
}

procedure RE_ColorLine(ARichEdit: TRichEdit; ARow: Integer; AColor: TColor);
begin
  with 
ARichEdit do
  begin
    
SelStart := SendMessage(Handle, EM_LINEINDEX, ARow - 1, 0);
    SelLength := Length(Lines[ARow - 1]);
    SelAttributes.Color := AColor;
    SelLength := 0;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  
ZeileFaerben(RichEdit1, 4, clGreen);
end;

 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners