Показать сообщение отдельно
  #8  
Старый 22.08.2010, 09:55
Asinkrit Asinkrit вне форума
Местный
 
Регистрация: 29.10.2009
Сообщения: 446
Репутация: 271
По умолчанию

Вот код, давненько писал, он подсвечивает слова в RichEdit-е, его недостаток, он подсвечивает весь текст разом, если ты хочешь что бы слова подсвечивались при вводе или наоборот снимали подсветку при удалении, как в дельфях к примеру, то надо обрабатывать только тот текст, вблизи которого происходят изменения, тогда лаги даже от такого подхода уберутся.
Код:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;

const
  CREZERVWORDS : array [0..9] of  String = ('BEGIN','END','IF','THEN','WITH','DO','FOR','AND','TO',
                                            'ELSE');
  CWORDSCOLOR = clBlack;
  CREZERVWORDSCOLOR  = clBlack;

type

  TForm1 = class(TForm)
    RichEdit1: TRichEdit;
    procedure RichEdit1KeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.RichEdit1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  i,w,sp:Integer;
  str:String;
  f:boolean;
  spos,slen:Integer;
begin
  str:='';
  sp:=0;
  with RichEdit1 do
    if Length(Text)>0 then
      begin
      RichEdit1.Lines.BeginUpdate;
      spos:=RichEdit1.SelStart;
      slen:=RichEdit1.SelLength;
      for i:=1 to Length(Text) do
        if (Text[i] <> ' ') and (Text[i] <> ';') and (Text[i] <> #13) then str:=str+Text[i]
          else
            begin
            f:=false;
            for w:=Low(CREZERVWORDS) to High(CREZERVWORDS) do
              if AnsiUpperCase(str)=CREZERVWORDS[w] then
                begin
                f:=true;
                break;
                end;
            SelStart:=sp;
            SelLength:=i-sp;
            if f then
              begin
              SelAttributes.Color:=CREZERVWORDSCOLOR;
              SelAttributes.Style:=[fsBold];
              end
                else
                  begin
                  SelAttributes.Color:=CWORDSCOLOR;
                  SelAttributes.Style:=[];
                  end;
            sp:=i;
            str:='';
            end;
      RichEdit1.SelStart:=spos;
      RichEdit1.SelLength:=slen;
      SelAttributes.Style:=[];
      RichEdit1.SelAttributes.Color:=CWORDSCOLOR;
      RichEdit1.Lines.EndUpdate;
      end;
end;

procedure TForm1.FormShow(Sender: TObject);
var
  Key:Word;
begin
  Key:=0;
  RichEdit1KeyUp(RichEdit1 , Key, []);
end;

end.
Ответить с цитированием