改变RichEdit的游标位置

// 指定输入游标的位置
procedure SetCaret(RTF: TRichEdit; var Row, Col: word);
var
i, iStopLine, iSelStart: integer;
begin
if (RTF = nil) then Exit;
if Row = 0 then Row := 1;
if Col = 0 then Col := 1;

// 到第 Row 列, Col 行共几个字元
iStopLine := Row - 1;
iSelStart := 0;
for i := 0 to RTF.Lines.Count - 1 do
begin
if i = iStopLine then
begin
if Length(RTF.Lines[i]) >= Col then
Inc(iSelStart, Col)
else
Inc(iSelStart, Length(RTF.Lines[i]) + 2);
Break;
end;
Inc(iSelStart, Length(RTF.Lines[i]) + 2);
end;
if iSelStart > 0 then Dec(iSelStart);

// 以设定标记的方式指定游标位置
SendMessage(RTF.Handle, EM_SETSEL, iSelStart, iSelStart);

// 再次侦测游标位置
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
iRow, iCol: word;
begin
iRow := 17;
iCol := 3;
SetCaret(RichEdit1, iRow, iCol);
RichEdit1.SetFocus;
end;

转载于:https://www.cnblogs.com/myamanda/articles/1528745.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值