请问如何在TRichViewEdit中动态移动光标位置?

解决方案 »

  1.   

    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;
    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   RichEdit1.SetFocus;
    iRow := 2;
    iCol := 4;
    SetCaret(RichEdit1, iRow, iCol);end;