How to convert the mouse coordinates into a line and character offset in a TRichEdit (Views: 28)
Problem/Question/Abstract: I am currently trapping the OnMouseMove event, however have run into significant problems converting the mouse coordinates into a line and character offset in a rich edit. Answer: procedure TForm1.RichEdit1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); var Point: TPoint; Value: LongInt; LineNumber: Integer; LinePos: Integer; Line: string; begin {Get absolute position of character beneath mouse} Point.x := X; Point.y := Y; Value := RichEdit1.Perform(EM_CHARFROMPOS, 0, LParam(@Point)); if Value >= 0 then begin {Get line number} LineNumber := RichEdit1.Perform(EM_LINEFROMCHAR, Value, 0); {Get line position} LinePos := Value - RichEdit1.Perform(EM_LINEINDEX, LineNumber, 0); {Get line} Line := RichEdit1.Lines[LineNumber]; Label1.Caption := Format('Line: %d Column: %d: %s', [LineNumber, LinePos, Line]); end else begin Label1.Caption := EmptyStr; end; end; This only works for RichEdits. |