35 Votes

Delphi/Lazarus: Retrieve Cursor Position relative to Form/Window

Tip by Delphian | Last update on 2023-11-24 | Created on 2013-04-03

With the function GetCursorPos(), you are able to get the cursor position relative to the screen. Here is a way of how to get the position of the cursor relative to a form (window):

var
  P: TPoint;
begin
  GetCursorPos(P);
  P := Form1.ScreenToClient(P);
  Form1.Caption := inttostr(P.X + ' ' + inttostr(P.Y));
end;

First, we are using GetCursorPos() to save the current mouse position to the point P. After that, we convert the screen coordinates to the form coordinates of Form1 by using ScreenToClient(). Finally, we output the converted coordinates as the caption of our window.

ReplyPositiveNegative

About the Author

AvatarThe author has not added a profile short description yet.
Show Profile

 

Related Topics

Android Splash Screen Tutorial

Tutorial | 0 Comments

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.