How does a mouse position translate into a scrollable control?

I have a control that scrolls vertically. I need to calculate the position of the mouse (by click) relative to the top of the control, and not just the visible area.

For example, let's say my control has a height of 500. The scroll bar causes the visible rectangle to have a height of 100. Therefore, when I partially scroll, the client coordinate (from a mouse click) will return a number relative to the client rectangle (for example, 50).

But what I want to know is the offset from the beginning of my control, so it will be something like 250.

I tried to understand this for some time, and I think that I should lose sight of something simple, because I do not find much information on this topic.

Thank.

+3
source share
1 answer

Try to find the PointToClient method and MousePosition method and try to take the Mouse ScreenCoordinates and convert it to the relative coordinates of your control.

Point screenPos = new Point(MousePosition.X, MousePosition.Y);
Point myPos = myControl.PointToClient(screenPos);
+2
source

All Articles