Results 1 to 6 of 6

Thread: [RESOLVED] Move Cursor then click?

  1. #1

    Thread Starter
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Resolved [RESOLVED] Move Cursor then click?

    maybe im missing something but all the code i find seems EXTREMELY complex to just click the left mouse button?

    I move it..

    Windows.Forms.Cursor.Position = New Point(907, 649)

    Now.. click (its not a control on my form)

    I tried sendinput but it doesnt seem to work.

    Thanks!
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,398

    Re: Move Cursor then click?

    Try this:
    Code:
    Public Class Form1
    
        'API
        Declare Sub mouse_event Lib "user32" Alias "mouse_event" _
            (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer)
    
        Private Const left_down As Integer = &H2
        Private Const left_up As Integer = &H4
    
        'Controls
        Private btn1, btn2 As Button
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Set the buttons to new instance
            btn1 = New Button
            btn2 = New Button
    
            'Set the button's properties
            With btn1
                .Location = New Point(5, 5)
                .Text = "Click Me"
            End With
    
            With btn2
                .Left = btn1.Right + 5
                .Text = "Don't Click"
                .Top = btn1.Top
            End With
    
            'Add the buttons to the form
            Me.Controls.AddRange({btn1, btn2})
    
            'Generate the click event for the buttons
            AddHandler btn1.Click, AddressOf btn1_Click
            AddHandler btn2.Click, AddressOf btn2_Click
        End Sub
    
        Private Sub btn1_Click(ByVal sender As Object, ByVal e As EventArgs)
            'Move the cursor to the position of button2
            Windows.Forms.Cursor.Position = Me.PointToScreen(Button2.Location)
    
            'Click the mouse button down and then up
            mouse_event(left_down, 0, 0, 0, 1)
            mouse_event(left_up, 0, 0, 0, 1)
        End Sub
    
        Private Sub btn2_Click(ByVal sender As Object, ByVal e As EventArgs)
            'Show that something has happened
            MessageBox.Show("Automatic Click")
        End Sub
    
    End Class
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Move Cursor then click?

    perfection... thank you.


    (still seems so much to click when you can move it so easily!)
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,398

    Re: [RESOLVED] Move Cursor then click?

    If you don't like the verboseness of having the APIs and everything, then simply create an extension. To do that, create a new module and add this code:

    Code:
    Option Strict On
    Option Explicit On
    
    Imports System.Runtime.CompilerServices
    Module PointerExtension
    #Region "API"
    
        Declare Sub mouse_event Lib "user32" Alias "mouse_event" _
            (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer)
    
        Private Const left_down As Integer = &H2
        Private Const left_up As Integer = &H4
        Private Const middle_down As Integer = &H20
        Private Const middle_up As Integer = &H40
        Private Const right_down As Integer = &H8
        Private Const right_up As Integer = &H10
    
    #End Region
    
    #Region "Methods"
    
        <Extension()>
        Public Sub LeftClick(ByVal cursor As Cursor)
            mouse_event(left_down, 0, 0, 0, 1)
            mouse_event(left_up, 0, 0, 0, 1)
        End Sub
    
        <Extension()>
        Public Sub LeftClick(ByVal cursor As Cursor, ByVal location As Point)
            cursor.Position = location
            mouse_event(left_down, 0, 0, 0, 1)
            mouse_event(left_up, 0, 0, 0, 1)
        End Sub
    
        <Extension()>
        Public Sub RightClick(ByVal cursor As Cursor)
            mouse_event(left_down, 0, 0, 0, 1)
            mouse_event(left_up, 0, 0, 0, 1)
        End Sub
    
        <Extension()>
        Public Sub RightClick(ByVal cursor As Cursor, ByVal location As Point)
            cursor.Position = location
            mouse_event(right_down, 0, 0, 0, 1)
            mouse_event(right_up, 0, 0, 0, 1)
        End Sub
    
        <Extension()>
        Public Sub MiddleClick(ByVal cursor As Cursor)
            mouse_event(left_down, 0, 0, 0, 1)
            mouse_event(left_up, 0, 0, 0, 1)
        End Sub
    
        <Extension()>
        Public Sub MiddleClick(ByVal cursor As Cursor, ByVal location As Point)
            cursor.Position = location
            mouse_event(middle_down, 0, 0, 0, 1)
            mouse_event(middle_up, 0, 0, 0, 1)
        End Sub
    
    #End Region
    
    End Module
    Then to get the cursor to click left, right, or middle then call:
    Code:
    'Left Click
    Cursor.LeftClick()
    'Or
    Cursor.LeftClick(Me.PointToScreen(Button2.Location))
    
    'Right Click
    Cursor.RightClick()
    'Or
    Cursor.RightClick(Me.PointToScreen(Button2.Location))
    
    'Middle Click
    Cursor.MiddleClick()
    'Or
    Cursor.MiddleClick(Me.PointToScreen(Button2.Location))
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [RESOLVED] Move Cursor then click?

    to add some clarity and some extra verbosity to this thread, first dday, where does this magic 1 for dwExtraInfo come from???
    the mouse_event api allows you to specify where to click as well as which button, click,release etc:

    Code:
    Public Class Form1
    
        Declare Function mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Int32, ByVal dX As Int32, ByVal dY As Int32, ByVal cButtons As Int32, ByVal dwExtraInfo As Int32) As Boolean
    
        Const MOUSEEVENTF_MOVE As Int32 = &H1 ' mouse move
        Const MOUSEEVENTF_LEFTDOWN As Int32 = &H2 ' left button down
        Const MOUSEEVENTF_LEFTUP As Int32 = &H4 ' left button up
        Const MOUSEEVENTF_RIGHTDOWN As Int32 = &H8 ' right button down
        Const MOUSEEVENTF_RIGHTUP As Int32 = &H10 ' right button up
        Const MOUSEEVENTF_MIDDLEDOWN As Int32 = &H20 ' middle button down
        Const MOUSEEVENTF_MIDDLEUP As Int32 = &H40 ' middle button up
        Const MOUSEEVENTF_ABSOLUTE As Int32 = &H8000 ' absolute move
        Const MOUSEEVENTF_WHEEL As Int32 = &H800 ' wheel button rolled
    
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            'click button1
            Dim p As Point = Button1.PointToScreen(New Point(10, 10))
            Dim x As Integer = CInt(p.X * 65535 / Screen.PrimaryScreen.Bounds.Width)
            Dim y As Integer = CInt(p.Y * 65535 / Screen.PrimaryScreen.Bounds.Height)
            mouse_event(MOUSEEVENTF_ABSOLUTE + MOUSEEVENTF_MOVE + MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, x, y, 0, 0)
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            MsgBox("clicked")
        End Sub
    
    End Class

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,398

    Re: [RESOLVED] Move Cursor then click?

    first dday, where does this magic 1 for dwExtraInfo come from???
    I don't know. I was going off of previous examples I have seen.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width