1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| Unit Unit1;
Interface
Uses Windows, Classes, Forms, Graphics, Messages, Controls,
SysUtils, StdCtrls, ExtCtrls, ComCtrls;
Type
TForm1 = Class(TForm)
RichEdit1: TRichEdit;
Button1: TButton;
Procedure FormCreate(Sender: TObject);
Procedure RichEdit1Change(Sender: TObject);
Procedure Button1Click(Sender: TObject);
End;
Var
Form1: TForm1;
{----------------------------------------------------------------}
{ }Implementation{ }
{----------------------------------------------------------------}
{$R *.DFM}
Type
TSyntax = Record
Mot: ShortString;
Color: TColor;
Style: TFontStyles;
Size: Integer;
Name: TFontName;
End;
Var
ASyntax: Array Of TSyntax;
{----------------------------------------------------------------}
Procedure AddSyntax(AMot:ShortString; AColor:TColor;
AStyle:TFontStyles; ASize:Integer; AName:TFontName);
Begin
SetLength(ASyntax, High(ASyntax) + 2);
With ASyntax[High(ASyntax)] Do
Begin
Mot := AMot;
Color := AColor;
Style := AStyle;
Size := ASize;
Name := AName;
End;
End;
{----------------------------------------------------------------}
Procedure TForm1.FormCreate(Sender: TObject);
Begin
RichEdit1.DoubleBuffered := False;
AddSyntax('test', clRed, [fsBold], 10, 'Lucida console');
AddSyntax('color', clBlue, [fsItalic, fsBold], 12, 'Terminal');
AddSyntax('select', clGreen, [fsUnderline], 14, 'Verdana');
End;
{----------------------------------------------------------------}
Procedure TForm1.RichEdit1Change(Sender: TObject);
Var
x, i, p, s: Integer;
st: String;
Begin
st := RichEdit1.Text;
If (st = '') Then Exit;
RichEdit1.Perform(Messages.WM_USER + 63, WParam(True), LParam(False));
s := RichEdit1.SelStart;
x := 1;
Repeat
With RichEdit1, SelAttributes Do
Begin
For i := 0 To High(ASyntax) Do
Begin
p := Pos(ASyntax[i].Mot, Copy(st, x, Length(ASyntax[i].Mot)));
If (p = 1) Then
Begin
SelStart := x - 1;
SelLength := Length(ASyntax[i].Mot);
Color := ASyntax[i].Color;
Style := ASyntax[i].Style;
Size := ASyntax[i].Size;
Name := ASyntax[i].Name;
SelLength := 0;
SelStart := s;
x := x + Length(ASyntax[i].Mot);
Break;
End;
End;
If (p = 0) And (x <= Length(st)) Then
Begin
SelStart := x - 1;
SelLength := 1;
Color := Font.Color;
Style := Font.Style;
Size := Font.Size;
Name := Font.Name;
SelLength := 0;
SelStart := s;
x := x + 1;
End;
End;
Until (x > Length(st));
RichEdit1.Perform(Messages.WM_USER + 63, WParam(False), LParam(False));
End;
{----------------------------------------------------------------}
Procedure TForm1.Button1Click(Sender: TObject);
Begin
ASyntax[0].Mot := 'nouveau'; // Remplace "test"
End;
{----------------------------------------------------------------}
End. |
Partager