delphi 7编写的代码查看编辑器对注释的着色

来源:互联网 发布:up to date数据库介绍 编辑:程序博客网 时间:2024/04/30 09:21
procedure TForm1.RichEdit1Change(Sender: TObject);var  point: TPoint;  x, x1, y, i, i1, i2, i3, i4, p: Integer;  s1: string;  pa: array[1..10] of char;begin (********鼠标位置和焦点位置保存*********)  x := point.X;  y := point.Y;  x1 := RichEdit1.SelStart;  (*对注释进行删除时,对注释删除后的前半部分恢复常规并且字体加黑设置,如:'(*'删除'*'后'('部分要恢复常规并且字体加黑*)  i := 0;  p := Length(RichEdit1.Text);  RichEdit1.SelStart := i;  RichEdit1.SelLength := p;  RichEdit1.SelAttributes.Style := [];  RichEdit1.SelAttributes.Color := clBlack;//变量赋初值  i := 0;  i1 := 0;  p := 0;  //////////////////////////////////////////////////////////////////////////////  (*******注释"{"和"}"的渲染*************************)  while p >= 0 do  begin    i := 0;    p := 0;    i := PosEx('{', RichEdit1.Text, i1) - 1;    p := PosEx('}', RichEdit1.Text, i1);    if i > 0 then    begin      if (p = 0) or (p < i) then      begin        p := Length(RichEdit1.Text);      end;    end;    if p <> 0 then    begin      i1 := p + 1;      s1 := (copy(RichEdit1.Text, i, 1));      pa[1] := s1[1];      if (not (pa[1] in [''''])) and (not (pa[1] in ['"'])) or (i < p) then      begin        RichEdit1.SelStart := i;        RichEdit1.SelLength := p - i;        Richedit1.SelAttributes.Color := clGreen;        RichEdit1.SelAttributes.Style := [fsItalic];      end;    end    else      Break;  end; //////////////////////////////////////////////////////////////////////////////  //变量赋初值  i := 0;  i1 := 0;  p := 0;  (*******注释"(*"和'*)"的渲染 ********************************************)  while p >= 0 do  begin    i := 0;    p := 0;    i := PosEx('(*', RichEdit1.Text, i1) - 1;    p := PosEx('*)', RichEdit1.Text, i1);    if i > 0 then    begin      if (p = 0) or (p < i) then      begin        p := Length(RichEdit1.Text);      end;    end;    if p <> 0 then    begin      i1 := p + 2;      s1 := (copy(RichEdit1.Text, i, 1));      pa[1] := s1[1];      if (not (pa[1] in [''''])) and (not (pa[1] in ['"'])) or (i < p) then      begin        RichEdit1.SelStart := i;        RichEdit1.SelLength := p - i + 1;        Richedit1.SelAttributes.Color := clGreen;        RichEdit1.SelAttributes.Style := [fsItalic];      end;    end    else      Break;  end;////////////////////////////////////////////////////////////////////////////////(********************(注释'//"的渲染)*****************************************)  i := 0;  i1 := 0;  i2 := 0;  i3 := 0;  i4 := 0;  p := 0;  i := PosEx('//', RichEdit1.Text, i1);  if i > 0 then  begin    while i > 0 do    begin      i3 := 0;      i := PosEx('//', RichEdit1.Text, i1);      i1 := i + 3;      begin        RichEdit1.SelStart := i - 1;        i4 := SendMessage(Richedit1.handle, EM_LINEFROMCHAR, Richedit1.selstart, 0);        if i4 >= 0 then        begin          for i2 := 0 to i4 do          begin            i3 := i3 + Length(RichEdit1.Lines[i2]) + 2;  //DBRichEdit显示时用i3 := i3 + Length(RichEdit1.Lines[i2]);          end;          RichEdit1.SelLength := i3 - i;          Richedit1.SelAttributes.Color := clGreen;          RichEdit1.SelAttributes.Style := [fsItalic];        end;      end;    end;  end;   (********鼠标位置和焦点位置重新植入*******************************************)  point.X := x;  point.Y := y;  RichEdit1.SelStart := x1;  RichEdit1.SetFocus;end;

0 0
原创粉丝点击