procedure TfrmMain.RepCodeFind(Sender: TObject);
Var
intPos,intPAt:integer;
begin
//intCurLine:=memoCode.Perform(EM_LINEFROMCHAR, -1, 0);
With memoCode do
Begin
intPAt:=SelStart+SelLength;
intPos:=Position(RepCode.FindText,Copy(Text,SelStart+SelLength,Length(Text)),1);
intPos:=intPAt+intPos;
if intPos>0 then
Begin
SelStart:=intPos-2;
SelLength:=Length(RepCode.FindText);
//滚动 RichText 以显示找到的文字
Perform(EM_SCROLLCARET, 0 , 0);
End
Else
Begin
Application.MessageBox(Pchar('搜索完毕!'),Pchar('搜索'),MB_OK+MB_ICONINFORMATION);
SelStart:=0;
End;
//SetFocus;
End;
end;
Position 是自定义函数
function TfrmMain.Position(SubStr, SourceStr: string;
intOccur: integer): integer;
var
//intProvPos:integer;
//intLastPos:integer;
intSubLength:integer;
intSourceLength:integer;
intOccurNo:integer;
intI:integer;
strFind:String;
begin
if intOccur<1 then
begin
MessageBox(0,Pchar('参数错误,参数intOccur应大于0'),Pchar('参数'),MB_OK+MB_ICONWARNING+MB_TASKMODAL);
Result:=0;
exit;
end;
intSubLength:=Length(SubStr);
intSourceLength:=Length(SourceStr);
intOccurNo:=0;
for intI := 1 to intSourceLength do
Begin
strFind:=Copy(SourceStr,intI,intSubLength);
if strFind=SubStr then
begin
intOccurNO:=intOccurNO+1;
if intOccurNO=intOccur then
begin
Position:=intI;
exit;
end;
end;
end;
Position:=0;
End;