bdn.borland.com

Article #27255: Finding the column and row of the cursor in a TRichEdit

Finding the column and row of the cursor in a TRichEdit
By Corbin Dunn
Delphi Developer Support

You may have come across the problem of trying to find out the current column and row position of the cursor in a rich edit. This is pretty easy to do, and the best time to do it is in the Application.OnIdle event:

procedure TForm1.ApplicationEvents1Idle(Sender: TObject;
  var Done: Boolean);
var
  Row, Col: Integer;
begin
  // Find out what line we are on, based on the current selection's
  // end (start + length).
  Row := SendMessage(Richedit1.Handle, EM_EXLINEFROMCHAR, 0,
    RichEdit1.SelStart + RichEdit1.SelLength);
  // Find the column, from the selection end, minus the char we are on  
  Col := RichEdit1.SelStart + RichEdit1.SelLength -
    SendMessage(Richedit1.Handle, EM_LINEINDEX, -1, 0);
  // Display it in a status bar
  Statusbar1.SimpleText := IntToStr(Row+1)+ ' ' + IntToStr(Col+1)
    + ' ' + DateTimeToStr(Now);
end;

Download the complete source from Borland CodeCentral

Last Modified: 27-APR-01