0

I have a picture box and i get the current position of the mouse and store them in two separated labels. And I have a button to set a new position to the mouse but the position I get its not correct.

I get x = 399 y = 237

But when I click the button to set the mouse position to this location i get x = 175 y = 175

This is the code I use to get the x and y:

Private Sub PictureBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseClick
    PPoint = New Point(e.X, e.Y)
    Label8.Text = e.X
    Label9.Text = e.Y
End Sub

And this is the code i use to set the new position to the mouse

Private Sub Button8_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button8.Click
    Cursor.Position = PPoint
End Sub

I have try a different way to do but i continue with the same problem. Tis is the code i have try from Find Mouse Position

  • Try in PictureBox1_MouseClick replacing PPoint with "PPoint = PointToScreen(New Point(e.X, e.Y))" and seeing if it helps. Your PictureBox coordinates have 0,0 at the top-left of the picturebox. Cursor.Position uses screen coordinates, with 0,0 being the top-left corner of the screen. – Aaron Dec 29 '17 at 16:06
  • @Aaron Tank you for the help. – Helder da silveira ventura Dec 29 '17 at 16:09
  • @Aaron i have try your changes and its more close but its not correct yet its not far from the point i have done but the position continues to not have the right position – Helder da silveira ventura Dec 29 '17 at 16:11
  • You need to go back to client to do what you need. I made an answer that will show you with graphics (so that moving the mouse doesn't impact it). You can change it to set cursor.position if you want, just know that moving the mouse after clicking the button will still move the mouse from where you set it. – Aaron Dec 29 '17 at 16:25

1 Answers1

0

This will put a red dot on the exact point you click. I wonder how useful setting the cursor position will be though, as they will almost certainly move the mouse after clicking the button (inadvertently or not).

Setting the Cursor position needs to be in Screen coordinates - this converts back to client coordinates for drawing. I don't believe the PointToClient is necessary for the cursor position. In the below code, it is an unnecessary conversion, as you just go back to client coordinates. I left it in to show an example of each conversion, so that you can experiment with them.

Public Class Form1
Private PPoint As Point
Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()
    PictureBox1.BackColor = Color.White
    PictureBox1.BorderStyle = BorderStyle.Fixed3D
    AddHandler PictureBox1.MouseClick, AddressOf PictureBox1_MouseClick
    AddHandler Button8.Click, AddressOf Button8_Click
    ' Add any initialization after the InitializeComponent() call.

End Sub

Private Sub Button8_Click(sender As Object, e As EventArgs)
    Dim g As Graphics = PictureBox1.CreateGraphics()
    Dim rect As New Rectangle(PictureBox1.PointToClient(PPoint), New Size(1, 1))
    g.DrawRectangle(Pens.Red, rect)
End Sub

Private Sub PictureBox1_MouseClick(sender As Object, e As MouseEventArgs)
    PPoint = PictureBox1.PointToScreen(New Point(e.X, e.Y))
    Label8.Text = PPoint.X.ToString()
    Label9.Text = PPoint.Y.ToString()

End Sub
End Class
Aaron
  • 1,313
  • 16
  • 26