Moving The Cursor

Discussion in 'Programming & Scripting' started by Nothinbeter2do, Dec 17, 2008 with 11 replies and 318 views.

  1. Nothinbeter2do

    Nothinbeter2do Banned

    Messages:
    2,425
    Ratings:
    526
    Hey. I need some help moving the cursor using Visual Basic.


    Right now, I have this code.
    Code:
    Public Class Form1
    
    Private Structure POINTAPI
    Dim x As Integer
    Dim y As Integer
    End Structure
    
    Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Integer, ByRef lpPoint As POINTAPI) As Integer
    Private Declare Function SetCursorPos Lib "user32" (ByVal x As Integer, ByVal y As Integer) As Integer
    
    Sub MoveMouse(ByRef x As Single, ByRef y As Single)
    Dim pt As POINTAPI
    pt.x = x
    pt.y = y
    ClientToScreen(Handle.ToInt32, pt)
    SetCursorPos(pt.x, pt.y)
    End Sub
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    MoveMouse(Button2.Left + Button2.Width / 2, Button2.Top + Button2.Height / 2)
    End Sub
    
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    MoveMouse(Button1.Left + Button1.Width / 2, Button1.Top + Button1.Height / 2)
    End Sub
    End Class
    But that moves the cursor so Button1 or Button2. How can I make it so that it moves it to a specific coordinate. I need it to move to 56 places as quick as possible.

    Can anyone also tell me how to find the coordinates of my current mouse position?
     
  2. Ibdc

    Ibdc Retired Retired

    Messages:
    5,100
    Ratings:
    2,072
    It sounds like you're trying to make a macro for runescape or something like that. Don't make it with VB, there's much simpler ways. Writing a script in SCAR (or downloading a premade one) would probably be the best way to go about doing this. But if you really wanna do this in VB, it's gonna fairly tedious, but alright. Delete your buttons, and the code for the buttons and put this in the load event for your form.

    Code:
    Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
    Me.Width = My.Computer.Screen.Bounds.Width
    Me.Height = My.Computer.Screen.Bounds.Height
    Me.Location = New Point("1", "1")
    Me.TransparencyKey = Me.BackColor
    Me.ShowInTaskbar = False
    
    MoveMouse("100", "100")
    System.Threading.Thread.Sleep(1000)
    MoveMouse("200", "200")
    System.Threading.Thread.Sleep(1000)
    MoveMouse("300", "300")
    System.Threading.Thread.Sleep(1000)
    MoveMouse("400", "400")
    System.Threading.Thread.Sleep(1000)
    MoveMouse("500", "500")
    
    It will move the mouse basically in a diagonal line, the first number in the parantheses is the x value, the second is the y, it waits one second in between each move (1000 milliseconds)
     
    • Like Like x 1
  3. OP
    Nothinbeter2do

    Nothinbeter2do Banned

    Messages:
    2,425
    Ratings:
    526
    Is 100,100 200,200 etc. where I put my mouse coordinates? Also, do i have to make it wait 1 second?
     
  4. Ibdc

    Ibdc Retired Retired

    Messages:
    5,100
    Ratings:
    2,072
    Yes, the first number is the x coordinate, the second is the y. I said that in my last post. Also it doesn't have to wait a second, but otherwise you wouldn't even see it in the first position and it would skip straight to the end.
     
  5. OP
    Nothinbeter2do

    Nothinbeter2do Banned

    Messages:
    2,425
    Ratings:
    526
    Alright, thanks.

    But how do I find the coordinates of a place on my screen?
     
  6. Ibdc

    Ibdc Retired Retired

    Messages:
    5,100
    Ratings:
    2,072
    There's a couple ways.

    One way is you could keep making logical guesses as to where you want it to be, and if you get it in the general area just tweak it a little to get it right on target. "1","1" is the top left corner, the first number is the x which is horizontal, the second number is the y, which is vertical.

    Another way would be to make a second program for your own personal use, and put a label and a timer on it, set the timer to enabled and use something like this:
    Code:
    Dim cursorPositionX as integer = System.Windows.Forms.Cursor.Position.X
    Dim cursorPositionY as integer = System.Windows.Forms.Cursor.Position.Y
    Label1.Text = cursorPositionX & "," & cursorPositionY
    
    Simple enough.
     
  7. OP
    Nothinbeter2do

    Nothinbeter2do Banned

    Messages:
    2,425
    Ratings:
    526
    I did that and it said my cursor was at 900, 400 or something like that. But then when I set the coordinates to that spot, it wasn't anywhere near the right spot.
     
  8. Ibdc

    Ibdc Retired Retired

    Messages:
    5,100
    Ratings:
    2,072
    You set the values to what the label displays, the first number on the label is the x, the second is the y.
     
  9. OP
    Nothinbeter2do

    Nothinbeter2do Banned

    Messages:
    2,425
    Ratings:
    526
    I did, but it doesn't place the cursor at the right position.
     
  10. Ibdc

    Ibdc Retired Retired

    Messages:
    5,100
    Ratings:
    2,072
    Then you did something wrong. That's all I can really tell you. It works fine for me
     
  11. OP
    Nothinbeter2do

    Nothinbeter2do Banned

    Messages:
    2,425
    Ratings:
    526
    Will it affect it if my screen resolution is 1400x900?
     
  12. Ibdc

    Ibdc Retired Retired

    Messages:
    5,100
    Ratings:
    2,072
    Well you can't go beyond 1400 for the x and 900 for the y, so (1500, 1000) would be impossible