Forum > LCL
TMEMO How to get line of the tmemo underneath mouse cursor?
(1/1)
Josh:
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.
rvk:
For Windows ok?
Something like this:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses Windows; procedure TForm1.Memo1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: integer);var Pt: integer; Idx: DWord; CharIdx, LineIdx, Col: word;begin Pt := (X and $FFFF) or ((Y and $FFFF) shl 16); Idx := SendMessage(Memo1.Handle, EM_CHARFROMPOS, 0, Pt); // linesindex and character index in memo CharIdx := Idx and $FFFF; LineIdx := (Idx shr 16) and $FFFF; Col := Idx - SendMessage(Memo1.Handle, EM_LINEINDEX, LineIdx, 0); // get col within line Inc(LineIdx); // was 0-based Inc(Col); // was 0-based Label1.Caption := Format('X %d Y %d', [X, Y]); Label2.Caption := Format('Line: %d Pos: %d', [LineIdx, Col]);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.
Navigation
[0] Message Index