Adding a string into Delphi memoedit

Rojin picture Rojin · May 17, 2014 · Viewed 7.9k times · Source

I want to add an string into specified place of a Memo Edit in delphi, how can I do this? I mean I want to know where the mouse cursor is within TMemo, and then add a string to this position. Is that possible?

Answer

Sertac Akyuz picture Sertac Akyuz · May 17, 2014

You can use EM_CHARFROMPOS to determine the character position of where the mouse cursor points:

var
  Pt: TPoint;
  Pos: Integer;
begin
 Pt := Memo1.ScreenToClient(Mouse.CursorPos);
 if (Pt.X >= 0) and (Pt.Y >= 0) then begin
   Pos := LoWord(Memo1.Perform(EM_CHARFROMPOS, 0, MakeLong(Pt.x, Pt.Y)));
   Memo1.SelLength := 0;
   Memo1.SelStart := Pos;
   Memo1.SelText := '.. insert here ..';
 end;
end;