11 Vote

Delphi/Lazarus: Determine Mouse Position

Question by Guest | 2016-01-18 at 13:33

Is there any possibility to specify the current position of the mouse in Lazarus or Delphi?

For example in terms of the recent X and Y coordinates on the screen?

ReplyPositiveNegative
1Best Answer1 Vote

Yes, it is possible. There are even several ways to do so. First of all, you can always use Mouse.CursorPos.X and Mouse.CursorPos.Y to get to the current mouse position on the screen. For example like that:

procedure TForm1.Timer1Timer(Sender: TObject);
var
  x, y: integer;
begin
  x := Mouse.CursorPos.X;
  y := Mouse.CursorPos.Y;
  Caption := IntToStr(x) + ' ' + IntToStr(y);
end;

In this example, we are using a Timer in order to read out the mouse position at regular intervals and to show them as caption of the Form.

However, many controls are offering directly the event OnMouseMove that is automatically making available the variables X and Y. Among others, also each Form offers this event, so that you can also retrieve the mouse position in the following way:

procedure TForm1.FormMouseMove(Sender: TObject; 
  Shift: TShiftState; X, Y: Integer);
begin
  Label1.Caption := IntToStr(X) + ' ' + IntToStr(Y);
end;

In this example, we are showing the current mouse position as caption of a Label every time the mouse moves over the Form.
2016-01-18 at 18:54

ReplyPositive Negative
Reply

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.