十年

  :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

RichEdit行号相关

    请问在RichEdit中我想将游标(文字)移动到某一位置要怎麽用呢?

    我用SendMessage可以取得行数、列数,然後要用那个叁数或是函数才能移动游标呢?

    这个需要自已处理, 例如本信所附的程式 --

    // 指定输入游标的位置
    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;
posted on 2005-01-29 11:48  留不住的时光  阅读(755)  评论(0)    收藏  举报