Vb.Net Move Mouse inside PictureBox

1.4k views Asked by At

Hello i want to move mouse to X,Y coordinates inside a Picture Box i am getting my coordinates from my Sub Like this

 Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    LocalMousePosition = PictureBox1.PointToClient(Cursor.Position)

    txt_MouseLoc.Text = ("X=" & LocalMousePosition.X & "," & "Y= " & LocalMousePosition.Y)
End Sub

now let say x= 100 and Y = 100

i want to click a button and move mouse to ... you guessed it x100 y100

but i am using this to move mouse

Windows.Forms.Cursor.Position = New Point(x, y)
        Thread.Sleep(2000)
        Do_LMouseClick()
        Thread.Sleep(2000)

it moves the mouse to x100 and y100 according to screen and not picturebox1

i have tried

MouseLocation = picturebox1.pointtoscreen(x,y)

but no go any ideas? thanks in advanced!

1

There are 1 answers

0
Keith Mifsud On BEST ANSWER

The location needs to add the form's location and picturebox's location:

Windows.Forms.Cursor.Position = New Point(x + Me.Location.X + PictureBox1.Location.X, _
                                          y + Me.Location.Y + PictureBox1.Location.Y)

UPDATE:

The form's title bar needs to be compensated by adding its height in the Y equation.