Mouse Coordinates To Caret Position in Firemonkey TMemo Component

2.4k views Asked by At

Handling Mouse / Drag&Drop Events on Firemonkeys TMemo Component offers Mousecursor Coordinates. Is there a way to calculate the CaretPosition out of the Mouse Coordinates?

I want to drag a text into a TMemo and this text should be inserted at current MouseCoordinates.

2

There are 2 answers

3
Ondrej Kelle On BEST ANSWER

Try calling GetPointPosition.

Unfortunately, this seems to have been removed from TMemo in XE3. As a quick and dirty workaround, you could try this:

function GetPointPosition(Memo: TMemo; Pt: TPointF; RoundToWord: Boolean = False): TCaretPosition;
var
  I, LPos: Integer;
  Rgn: TRegion;
begin
  Result.Line := -1;
  Result.Pos := -1;
  for I := 0 to Memo.Lines.Count - 1 do
  begin
    if Memo.Lines.Objects[I] is TTextLayout then
    begin
      LPos := TTextLayout(Memo.Lines.Objects[I]).PositionAtPoint(Pt, RoundToWord);
      if LPos >= 0 then
      begin
        if LPos > 0 then
        begin
          Rgn := TTextLayout(Memo.Lines.Objects[I]).RegionForRange(TTextRange.Create(LPos, 1), RoundToWord);
          if (Length(Rgn) > 0) and (Rgn[0].Top > Pt.Y) then
            Dec(LPos);
        end;
        Result.Line := I;
        Result.Pos := LPos;
        Break;
      end;
    end;
  end;
end;
0
Atom On

Solution applicable in XE10 using RTTI:

procedure TMyForm.MemoMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Single);
var Obj: IControl;   
    af: TPointf;
    cp: TCaretPosition;
    aName: string;
    LVar: TStyledMemo;
    LClass: TRttiInstanceType;
    aLines: TObject;
    aMethod: TRttiMethod;
    aValue: TValue;
    pointValue, boolValue: TValue;
begin
   af := TPointF.Create(Panel1.Position.X + X, panel4.Height + Y);
   Obj := ObjectAtPoint(ClientToScreen(af));

   if Assigned(Obj) then
   begin
       LVar := TStyledMemo(Obj);
       LClass := LContext.GetType(TStyledMemo) as TRttiInstanceType;
       aLines := LClass.GetField('FLineObjects').GetValue(LVar).AsObject;

       af := TPointF.Create(X,Y);
       pointValue := TValue.From<TPointF>(af);  
       boolValue := TValue.From<Boolean>(false);

       LClass := LContext.GetType(aLines.ClassType) as TRttiInstanceType;
       aMethod := LClass.GetMethod('GetPointPosition');
       aValue := aMethod.Invoke(aLines, [pointValue, boolValue]);

       cp := aValue.AsType<TCaretPosition>;
   end;
end;