Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does mouse position translate to a scrolled control?

Tags:

c#

.net

winforms

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

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

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

I've been trying for a while to figure this out, and I think I must be overlooking something easy because I'm not finding much info on the topic.

Thanks.

like image 573
TheSean Avatar asked Nov 13 '22 06:11

TheSean


1 Answers

Try looking at the PointToClient Method and the MousePosition Method and try taking the ScreenCoordinates of the mouse and converting it to the relative coordinates of your control.

Point screenPos = new Point(MousePosition.X, MousePosition.Y);
Point myPos = myControl.PointToClient(screenPos);
like image 167
Mark Hall Avatar answered Nov 16 '22 02:11

Mark Hall