Recent

Author Topic: TMEMO How to get line of the tmemo underneath mouse cursor?  (Read 364 times)

Josh

  • Hero Member
  • *****
  • Posts: 1142
Hi

How can I get the line of the text undeneath the mouse cursor of a tmemo, the caretpos is no use as you have to click the line? Hopefully there is a cross platfprm way.

Ideally if there is a way to convert the mouse.cursor ordinates to the tmemo to get the line underneath..

Thnks in advance.
« Last Edit: June 06, 2023, 03:27:53 pm by Josh »
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

rvk

  • Hero Member
  • *****
  • Posts: 5481
Re: TMEMO How to get line of the tmemo underneath mouse cursor?
« Reply #1 on: June 06, 2023, 04:17:18 pm »
For Windows ok?

Something like this:

Code: Pascal  [Select][+][-]
  1. uses Windows;
  2.  
  3. procedure TForm1.Memo1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: integer);
  4. var
  5.   Pt: integer;
  6.   Idx: DWord;
  7.   CharIdx, LineIdx, Col: word;
  8. begin
  9.   Pt := (X and $FFFF) or ((Y and $FFFF) shl 16);
  10.   Idx := SendMessage(Memo1.Handle, EM_CHARFROMPOS, 0, Pt); // linesindex and character index in memo
  11.   CharIdx := Idx and $FFFF;
  12.   LineIdx := (Idx shr 16) and $FFFF;
  13.   Col := Idx - SendMessage(Memo1.Handle, EM_LINEINDEX, LineIdx, 0); // get col within line
  14.   Inc(LineIdx); // was 0-based
  15.   Inc(Col); // was 0-based
  16.   Label1.Caption := Format('X %d  Y %d', [X, Y]);
  17.   Label2.Caption := Format('Line: %d Pos: %d', [LineIdx, Col]);
  18. end;

Notes:
- Pos doesn't go beyond the line.
- except for the last character. If mouse is beyond the line the Pos is Linelength + 1.

 

TinyPortal © 2005-2018