Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Location of mouse on an object

Posted on 2003-02-18
Medium Priority
Last Modified: 2010-04-04
I need to return the current location of the mouse when I perform an onclick (from the panel/lable etc). Any ideas?

Question by:Morgans43
  • 3

Author Comment

ID: 7979248
.. additional.... the location needs to be relative to the object rather than the screen as a whole... ie I need to know WHERE on the OBJECT I clicked.. not WHERE on the SCREEN...

Expert Comment

ID: 7979359

Why don't you implement a OnMouseUp() event, this returns the X, Y position of the mouse when you let the mouse up.

declare a private variable to the form called FMousePoint of type TPoint.

in the form create, initialize the x and y values to 0

in your OnMouseUp event handler

FMousePoint.X := X;
FMousePoint.Y := Y;

in your OnClick() utilize the FMousePoint values.

that seems to work, but perhaps I misunderstood your needs, if so, please explain in more detail.

hope this helps,

Author Comment

ID: 7979397
Was under the impression that this will give the location of the mouse on the screen rather than on the object.. will try it though.

Accepted Solution

Codeslave earned 150 total points
ID: 7979402
Another option is to make use of the global Mouse object. It records the current mouse position in screen coordinates. In order to convert to local coordinates, simply use the ScreenToClient method that is introduced in TControl.

For example,


procedure TMyComponent.DoSomething;
  lLocal: TPoint;
  lLocal := ScreenToClient(Mouse.CursorPos);
  // lLocal now contains the mouse cursor position in
  // local component coordinates.

Author Comment

ID: 7979676
Thanks to both, but this was the one that really helped me...

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This is an update to some code that someone else posted on Experts Exchange. It is an alternate approach, I think a little easier to use, & makes sure that things like the Task Bar will update.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question