[RESOLVED] Relative to container not screen
I have a panel that is called "Self" and I would like to make it follow the exact mouse movements.
I have done this. However it is not centred with the mouse and is reliant upon where the form is located on the screen. I don't want this.
VB Code:
Private Sub Self_FollowMouse_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Self_FollowMouse.Tick
X = Control.MousePosition.X
Y = Control.MousePosition.Y
Self.Location = New Point(X - 5, Y - 5)
End Sub
How do I solve this problem. thanks.
Re: Relative to container not screen
Control.MousePosition returns a point relative to the screen.
To get the position of the mouse relative to your form, you need to convert this point by using the PointToClient method.
VB Code:
Private Sub Self_FollowMouse_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Self_FollowMouse.Tick
Dim p As Point = PointToClient(Control.MousePosition)
X = p.X
Y = p.Y
Self.Location = New Point(X - 5, Y - 5)
End Sub
Re: Relative to container not screen
Quote:
Originally Posted by Andy_P
Control.MousePosition returns a point relative to the screen.
To get the position of the mouse relative to your form, you need to convert this point by using the PointToClient method.
VB Code:
Private Sub Self_FollowMouse_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Self_FollowMouse.Tick
Dim p As Point = PointToClient(Control.MousePosition)
X = p.X
Y = p.Y
Self.Location = New Point(X - 5, Y - 5)
End Sub
:D Thank you.