不对呀。我用这个到是好好的。
*************************************************************************
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
RichEdit1: TRichEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure SetCaret(RTF:TRichEdit; var row, col: word);
var
i,iStopLine,iSelStart:integer;
Strings:TStrings;
begin
if (RTF=nil) then exit;
Strings:=RTF.Lines;
if Row=0 then Row:=1;
if Col=0 then Col:=1;
//到第Row列,Col行共几个字元
iStopLine:=Row-1;
iSelStart:=0;
for i:=0 to Strings.Count-1 do
begin
if i=iStopLine then
begin
if Length(Strings)>Col then
Inc(iSelStart,Col)
else
Inc(iSelStart,Length(Strings)+2);
Break;
end;
Inc(iSelStart,Length(Strings)+2);
end;
if iSelStart>0 then Dec(iSelStart);
//以设定标记的方式指定游标位置
RTF.SelStart :=iSelStart;// + Length(RTF.Lines)+2;
//再次侦测游标位置
Row:=SendMessage(RTF.Handle,EM_LINEFROMCHAR,RTF.SelStart,0);
Col:=RTF.SelStart-SendMessage(RTF.Handle,EM_LINEINDEX,Row,0);
//卷到游标所在位置
SendMessage(RTF.Handle,EM_SCROLLCARET,0,0);
end;
procedure TForm1.Button1Click(Sender: TObject);
var i,j:word;
begin
i:=2;
j:=3;
richedit1.SetFocus;
SetCaret(RichEdit1,i,j);
end;
end.
*************************************************************************