3

is there a way to change the caret position in pixel?

i would like to move the care pos everytime i move the mouse mouse.

like:

Onmousemove: MoveCaretPos(X, Y);

XBasic3000
  • 3,418
  • 5
  • 46
  • 91
  • I hope there is a good reason for you wanting to do this. If the user will be expected to enter data (type) then don't do it, as most people will move the mouse to position, click, then move the mouse away to allow them to see what they are doing. – mj2008 Jun 01 '11 at 07:49

1 Answers1

7

No, you cannot set the position of the caret in a specific point, instead you must set the caret in a character position. To do this you must use the EM_CHARFROMPOS message to retrieve the closest character to a specified point and then set the value returned to the SelStart property.

Check this sample

procedure TForm1.RichEdit1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var
 APoint  : TPoint;
 Index   : Integer;
begin
   APoint := Point(X, Y);
   Index :=  SendMessage(TRichEdit(Sender).Handle,EM_CHARFROMPOS, 0, Integer(@APoint));
   if Index<0 then Exit;
   TRichEdit(Sender).SelStart:=Index;
end;
RRUZ
  • 134,889
  • 20
  • 356
  • 483