-1

I want know how detect to what side i'm moving mouse: to left, right, top, bottom inside TImage component on mousemove event?

Thank you.

  • What is stopping you working it out by recording tthe previous mouse coordinates and then calculating the change yourself? – MartynA Aug 20 '18 at 16:29

1 Answers1

2

Here's an example to be used in an FMX project. For a VCL project, you would use integer variables.

First, declare two variables Xold, Yold: single; for example in the private section of the form.

private
  Xold, Yold: Single;

Initialize these variables e.g. in the forms OnCreate() event. Using NaN requires System.Math in the uses clause.

procedure TForm5.FormCreate(Sender: TObject);
begin
  Xold := NaN;
  Yold := NaN;
end;

Then, in the OnMouseMove() event, calculate the movement horizontally and vertically, negative value indicate moving left or up, positive right or down.

procedure TForm5.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Single);
var
  horz, vert: Single;
begin
  if not IsNan(Xold) then horz := X - Xold else horz := 0;
  if not IsNan(Yold) then vert := Y - Yold else vert := 0;
  Xold := X; // save new values
  Yold := Y; //
  // use horz and vert as needed
  Label1.Text := Format('h: %f, v: %f',[horz, vert]);
end;

You may also want to reset the Xold and Yold variables to NaN when the mouse leaves the image.

procedure TForm5.Image1MouseLeave(Sender: TObject);
begin
  Xold := NaN;
  Yold := NaN;
end;

It was asked in comments, why initialize to NaN instead of just zero? Xold := 0; Yold := 0 is the top-left corner. If the mouse entry to the image happens at e.g. right side, the first move would be a jump from 0 to image width. Using NaN we can omit the first entry as a move and just store the entry point in Xold and Yold for use with next move.

Tom Brunberg
  • 20,312
  • 8
  • 37
  • 54
  • thank you, but i need obtain the four sides separately. In your example, you get 2 sides (for example right and down) in only one moviment. I need for example: i move the mouse to top, then `Label1.Caption := 'you moved mouse to top` –  Aug 20 '18 at 18:28
  • 2
    Oh then just apply some simple rules, this is what I meant with *use horz and vert as needed*: `if vert < 0 then label1.text := 'you are moving to top side' else if vert > 0 then label1.text := 'you are moving to bottom side' else if horz < 0 then label1.text := 'you are moving to left side' else if horz > 0 then label1.text := 'you are moving to right side'` – Tom Brunberg Aug 20 '18 at 19:08
  • I just need to ask why the Nan I mean you could've just initialized them with zero in the first place – Nasreddine Galfout Aug 20 '18 at 19:19
  • @Nas To be able to distinguish from valid values at the entry to the `TImage`, zero would indicate top left corner, what if entry happens at bottom left. Thanks for asking though, I just realized, the values should be set back to Nan at exit from the image. – Tom Brunberg Aug 20 '18 at 19:27
  • Oh I did not see that case. Thank you for this I learnt something +1 – Nasreddine Galfout Aug 20 '18 at 19:43