delphi 7编写的代码查看编辑器对保留字的着色

//Richedit的scrollbars不能为ssNone否则Richedit里的内容不能着色
//为了便于移植代码,没有用自定义函数 + 函数调用的方法    
  
procedure TForm1.RichEdit1Change(Sender: TObject);    
var
  point: TPoint;
  x, x1, y, y1, ip, i, i1, i2, i3, i4, p, p1: Integer;
  s, s1: string;
  pa: array[1..10] of char;
  strs: TStrings;

(*delphi的关键字进行高亮显示,字符串短的要放在前面显示,长字符串放在后面显示,否则显示不正常*)

  const

  str1 :string= ('absolute,finalization,abstract,initialization,assembler,implementation,automated,writeonly,constructor,contains,default,destructor,dispinterface,dynamic,except,export,exports,external,filethreadvar,nodefault,interface,resourcestring,procedure');

  str2 :string= ('finally,forward,function,implements,index,inherited,inline,library,message,object,overload,override,dispid,package,packed,pascal,private,program,property,protected,public,published,raise,readonly,record,register,reintroduce,requires,resident,safecall');

  str3 :string= ('and,as,asm,at,in,div,far,try,type,unit,uses,var,is,mod,name,near,nil,on,or,out,xor,shl,shr,with,array,not,set,read,goto,label,stdcall,stored,string,virtual,write,cdecl,class,const');

  str4 :string= ('if,then,else,case,of,repeat,until,while,for,to,do,downto'); //语法关键字

  str5 :string= ('begin,end');

begin
  (********鼠标位置和焦点位置保存*********)
  x := point.X;
  y := point.Y;
  x1 := RichEdit1.SelStart;


  (*对保留字进行删除时,对字符串删除后的前半部分恢复常规并且字体加黑设置,如:'interface'删除'e'后'interfac'部分要恢复常规并且字体加黑*)
  i := 0;
  p := Length(RichEdit1.Text);
  RichEdit1.SelStart := i;
  RichEdit1.SelLength := p;
  RichEdit1.SelAttributes.Style := [];
  RichEdit1.SelAttributes.Color := clBlack;

 /(5)
  strs := TStringList.Create;
  strs.CommaText := str5;
  for ip := 0 to Strs.Count - 1 do
  begin
    Application.ProcessMessages;
    i := 0;
    i1 := 0;
    p := 0;
    p1 := 0;


    while i >= 0 do
    begin
      Application.ProcessMessages;


      i := PosEx(Strs[ip], RichEdit1.Text, i1) - 1;
      if i <> -1 then
      begin
        p := Length(Strs[ip]);
        i1 := i + p + 1;
       
        if i = 0 then
        begin
          RichEdit1.SelStart := i;
          RichEdit1.SelLength := p;
          Richedit1.SelAttributes.Color := clMaroon;
          RichEdit1.SelAttributes.Style := [fsBold];
        end;


        s1 := (copy(RichEdit1.Text, i, 1));
        pa[1] := s1[1];
        if not (pa[1] in ['a'..'z', 'A'..'Z', '0'..'9']) then
        begin
          RichEdit1.SelStart := i;
          RichEdit1.SelLength := p;
          Richedit1.SelAttributes.Color := clMaroon;
          RichEdit1.SelAttributes.Style := [fsBold];
        end;




        s1 := (copy(RichEdit1.Text, i1, 1));
        pa[1] := s1[1];
        if (pa[1] in ['a'..'z', 'A'..'Z', '0'..'9']) then
        begin
          RichEdit1.SelStart := i;
          RichEdit1.SelLength := p;
          Richedit1.SelAttributes.Color := clBlack;
          RichEdit1.SelAttributes.Style := [];
        end;
      end
      else
        Break;
    end;
  end;
  //(4)


  strs.CommaText := str4;
  for ip := 0 to Strs.Count - 1 do
  begin
    Application.ProcessMessages;
    i := 0;
    i1 := 0;
    p := 0;
    p1 := 0;


    while i >= 0 do
    begin
      Application.ProcessMessages;


      i := PosEx(Strs[ip], RichEdit1.Text, i1) - 1;
      if i <> -1 then
      begin
        p := Length(Strs[ip]);
        i1 := i + p + 1;
       
        if i = 0 then
        begin
          RichEdit1.SelStart := i;
          RichEdit1.SelLength := p;
          Richedit1.SelAttributes.Color := clBlue;
          RichEdit1.SelAttributes.Style := [fsBold];
        end;


        s1 := (copy(RichEdit1.Text, i, 1));
        pa[1] := s1[1];
        if not (pa[1] in ['a'..'z', 'A'..'Z', '0'..'9']) then
        begin
          RichEdit1.SelStart := i;
          RichEdit1.SelLength := p;
          Richedit1.SelAttributes.Color := clBlue;
          RichEdit1.SelAttributes.Style := [fsBold];
        end;




        s1 := (copy(RichEdit1.Text, i1, 1));
        pa[1] := s1[1];
        if (pa[1] in ['a'..'z', 'A'..'Z', '0'..'9']) then
        begin
          RichEdit1.SelStart := i;
          RichEdit1.SelLength := p;
          Richedit1.SelAttributes.Color := clBlack;
          RichEdit1.SelAttributes.Style := [];
        end;
      end
      else
        Break;
    end;
  end;


  (3)


  strs.CommaText := str3;
  for ip := 0 to Strs.Count - 1 do
  begin
    Application.ProcessMessages;
    i := 0;
    i1 := 0;
    p := 0;
    p1 := 0;


    while i >= 0 do
    begin
      Application.ProcessMessages;


      i := PosEx(Strs[ip], RichEdit1.Text, i1) - 1;
      if i <> -1 then
      begin
        p := Length(Strs[ip]);
        i1 := i + p + 1;
    
        if i = 0 then
        begin
          RichEdit1.SelStart := i;
          RichEdit1.SelLength := p;
          Richedit1.SelAttributes.Color := clActiveCaption;
          RichEdit1.SelAttributes.Style := [fsBold];
        end;


        s1 := (copy(RichEdit1.Text, i, 1));
        pa[1] := s1[1];
        if not (pa[1] in ['a'..'z', 'A'..'Z', '0'..'9']) then
        begin
          RichEdit1.SelStart := i;
          RichEdit1.SelLength := p;
          Richedit1.SelAttributes.Color := clActiveCaption;
          RichEdit1.SelAttributes.Style := [fsBold];
        end;




        s1 := (copy(RichEdit1.Text, i1, 1));
        pa[1] := s1[1];
        if (pa[1] in ['a'..'z', 'A'..'Z', '0'..'9']) then
        begin
          RichEdit1.SelStart := i;
          RichEdit1.SelLength := p;
          Richedit1.SelAttributes.Color := clBlack;
          RichEdit1.SelAttributes.Style := [];
        end;
      end
      else
        Break;

    end;
  end;


  ///(2)


  strs.CommaText := str2;
  for ip := 0 to Strs.Count - 1 do
  begin
    Application.ProcessMessages;
    i := 0;
    i1 := -1;
    p := 0;
    p1 := 0;


    while i >= 0 do
    begin
      Application.ProcessMessages;

      i := PosEx(Strs[ip], RichEdit1.Text, i1) - 1;
      if i <> -1 then
      begin
        p := Length(Strs[ip]);
        i1 := i + p + 1;
       
        if i = 0 then
        begin
          RichEdit1.SelStart := i;
          RichEdit1.SelLength := p;
          Richedit1.SelAttributes.Color := clActiveCaption;
          RichEdit1.SelAttributes.Style := [fsBold];
        end;


        s1 := (copy(RichEdit1.Text, i, 1));
        pa[1] := s1[1];
        if not (pa[1] in ['a'..'z', 'A'..'Z', '0'..'9']) then
        begin
          RichEdit1.SelStart := i;
          RichEdit1.SelLength := p;
          Richedit1.SelAttributes.Color := clActiveCaption;
          RichEdit1.SelAttributes.Style := [fsBold];
        end;




        s1 := (copy(RichEdit1.Text, i1, 1));
        pa[1] := s1[1];
        if (pa[1] in ['a'..'z', 'A'..'Z', '0'..'9']) then
        begin
          RichEdit1.SelStart := i;
          RichEdit1.SelLength := p;
          Richedit1.SelAttributes.Color := clBlack;
          RichEdit1.SelAttributes.Style := [];
        end;
      end
      else
        Break;

    end;
  end;
  (1)
  strs.CommaText := str1;
  for ip := 0 to Strs.Count - 1 do
  begin
    Application.ProcessMessages;
    i := 0;
    i1 := -1;
    p := 0;
    p1 := 0;


    while i >= 0 do
    begin
      Application.ProcessMessages;


      i := PosEx(Strs[ip], RichEdit1.Text, i1) - 1;
      if i <> -1 then
      begin
        p := Length(Strs[ip]);
        i1 := i + p + 1;
       
        if i = 0 then
        begin
          RichEdit1.SelStart := i;
          RichEdit1.SelLength := p;
          Richedit1.SelAttributes.Color := clActiveCaption;
          RichEdit1.SelAttributes.Style := [fsBold];
        end;


        s1 := (copy(RichEdit1.Text, i, 1));
        pa[1] := s1[1];
        if not (pa[1] in ['a'..'z', 'A'..'Z', '0'..'9']) then
        begin
          RichEdit1.SelStart := i;
          RichEdit1.SelLength := p;
          Richedit1.SelAttributes.Color := clActiveCaption;
          RichEdit1.SelAttributes.Style := [fsBold];
        end;




        s1 := (copy(RichEdit1.Text, i1, 1));
        pa[1] := s1[1];
        if (pa[1] in ['a'..'z', 'A'..'Z', '0'..'9']) then
        begin
          RichEdit1.SelStart := i;
          RichEdit1.SelLength := p;
          Richedit1.SelAttributes.Color := clBlack;
          RichEdit1.SelAttributes.Style := [];
        end;
      end
      else
        Break;
    end;
  end;
  (完)
(****************************(完)*****************************************)

{对注释进行着色的代码应该放在这里,否则字体显示不正常}    
    
(********鼠标位置和焦点位置重新植入*******************************************)    
  point.X := x;    
  point.Y := y;    
  RichEdit1.SelStart := x1;    
  RichEdit1.SetFocus;    
end;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冰雪青松

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值