//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;
delphi 7编写的代码查看编辑器对保留字的着色
最新推荐文章于 2021-06-23 09:15:11 发布