2008. december 24., szerda

How to determine the caret position in a TMemo


Problem/Question/Abstract:

How to determine the caret position in a TMemo

Answer:

You can use the Windows API messages EM_LINEFROMCHAR and EM_LINEINDEX to determine the current line and offset within that line (starting from SelStart).

var
  LineNum: longint;
  CharsBeforeLine: longint;
begin
  LineNum := SendMessage(Memo1.Handle, EM_LINEFROMCHAR, Memo1.SelStart, 0);
  CharsBeforeLine := SendMessage(Memo1.Handle, EM_LINEINDEX, LineNum, 0);
  Label1.Caption := ' Line ' + IntToStr(LineNum + 1);
  Label2.Caption := ' Position ' + IntToStr((Memo1.SelStart - CharsBeforeLine) + 1);
end;

Nincsenek megjegyzések:

Megjegyzés küldése