未调试,else部分参考上面if部分 procedure TForm1.Button1Click(Sender: TObject); var i,j:integer; begin if RichEdit1.Lines.Count>RichEdit2.Lines.Count then begin for i:=0 to RichEdit2.Lines.Count-1 do begin if length(RichEdit1.Lines.Strings[i])>length(RichEdit2.Lines.Strings[i]) then begin for j:=0 to length(RichEdit2.Lines.Strings[i]) do begin if RichEdit1.Lines.Strings[i][j]<>RichEdit2.Lines.Strings[i][j] then begin RichEdit1.SelStart:=RichEdit1.CaretPos.Y+j; RichEdit1.SelLength:=1; RichEdit1.SelAttributes.Color:=clred; end; RichEdit1.SelStart:=RichEdit1.CaretPos.Y+j; RichEdit1.SelLength:=length(RichEdit1.Lines.Strings[i])-j; RichEdit1.SelAttributes.Color:=clred; end; end else begin end; end; for i:=i to RichEdit1.Lines.Count-1 do begin RichEdit1.SelStart:=RichEdit1.CaretPos.Y; RichEdit1.SelLength:=length(RichEdit1.Lines.Strings[i]); RichEdit1.SelAttributes.Color:=clred; end; end else begin end; end;
找指定的字符串和该字符串的起始位置我一般都是用循环 for i:=0 to RichEdit1.Lines.Count-1 do然后针对每一行查找 用pos就可以
procedure TForm1.Button1Click(Sender: TObject);begin FindDialog1.Position := Point(RichEdit1.Left + RichEdit1.Width, RichEdit1.Top); FindDialog1.Execute; end;procedure TForm1.FindDialog1Find(Sender: TObject); var FoundAt: LongInt; StartPos, ToEnd: Integer; begin with RichEdit1 do begin { begin the search after the current selection if there is one } { otherwise, begin at the start of the text } if SelLength <> 0 then StartPos := SelStart + SelLength else StartPos := 0; { ToEnd is the length from StartPos to the end of the text in the rich edit control } ToEnd := Length(Text) - StartPos; FoundAt := FindText(FindDialog1.FindText, StartPos, ToEnd, [stMatchCase]); if FoundAt <> -1 then begin SetFocus; SelStart := FoundAt; SelLength := Length(FindDialog1.FindText); end; end; end;
用richedit可以实现
procedure TForm1.Button1Click(Sender: TObject);
begin
RichEdit1.SelAttributes.Color:=clblue;
end;
richedit的内容是:你好,我好,大家好
procedure TForm1.FormShow(Sender: TObject);
begin
RichEdit1.SelStart:=12;
RichEdit1.SelLength:=6;
RichEdit1.SelAttributes.Color:=clred;
RichEdit1.SelStart:=0;
end;
其中selstart可以根据程序动态调
procedure TForm1.Button1Click(Sender: TObject);
var i,j:integer;
begin
if RichEdit1.Lines.Count>RichEdit2.Lines.Count then
begin
for i:=0 to RichEdit2.Lines.Count-1 do
begin
if length(RichEdit1.Lines.Strings[i])>length(RichEdit2.Lines.Strings[i]) then
begin
for j:=0 to length(RichEdit2.Lines.Strings[i]) do
begin
if RichEdit1.Lines.Strings[i][j]<>RichEdit2.Lines.Strings[i][j] then
begin
RichEdit1.SelStart:=RichEdit1.CaretPos.Y+j;
RichEdit1.SelLength:=1;
RichEdit1.SelAttributes.Color:=clred;
end;
RichEdit1.SelStart:=RichEdit1.CaretPos.Y+j;
RichEdit1.SelLength:=length(RichEdit1.Lines.Strings[i])-j;
RichEdit1.SelAttributes.Color:=clred;
end;
end
else
begin end;
end;
for i:=i to RichEdit1.Lines.Count-1 do
begin
RichEdit1.SelStart:=RichEdit1.CaretPos.Y;
RichEdit1.SelLength:=length(RichEdit1.Lines.Strings[i]);
RichEdit1.SelAttributes.Color:=clred;
end;
end
else
begin end;
end;
for i:=0 to RichEdit1.Lines.Count-1 do然后针对每一行查找
用pos就可以
FindDialog1.Position := Point(RichEdit1.Left + RichEdit1.Width, RichEdit1.Top);
FindDialog1.Execute;
end;procedure TForm1.FindDialog1Find(Sender: TObject);
var
FoundAt: LongInt;
StartPos, ToEnd: Integer;
begin
with RichEdit1 do
begin
{ begin the search after the current selection if there is one }
{ otherwise, begin at the start of the text }
if SelLength <> 0 then StartPos := SelStart + SelLength
else StartPos := 0; { ToEnd is the length from StartPos to the end of the text in the rich edit control } ToEnd := Length(Text) - StartPos; FoundAt := FindText(FindDialog1.FindText, StartPos, ToEnd, [stMatchCase]);
if FoundAt <> -1 then
begin
SetFocus;
SelStart := FoundAt;
SelLength := Length(FindDialog1.FindText);
end;
end;
end;