5

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.

4

1 に答える 1

1

PointToClientメソッドMousePositionメソッドを確認し、マウスのScreenCoordinatesを取得して、コントロールの相対座標に変換してみてください。

Point screenPos = new Point(MousePosition.X, MousePosition.Y);
Point myPos = myControl.PointToClient(screenPos);
于 2012-04-20T04:05:00.983 に答える