1. 程式人生 > >delphi 7編寫的程式碼檢視編輯器對保留字的著色

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;