Results 1 to 4 of 4

Thread: Simulate mouse click on form

  1. #1

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Simulate mouse click on form

    *Edit
    After reading a few things on google, is there a way to just go to x,y from within the form, not the whole window display itslef?

    I have some functions i found online.
    I want to go to x,y cords from within my form, and move the mouse to it, and simulate a left mouse click.

    When i click my button to move the mouse, it puts the mouse way outside my form

    Code:
    'Button to move mouse and click
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            MoveMouse(653, 264)
            LeftClick()
        End Sub
    
    Public 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)
        Public Declare Function SetCursorPos Lib "user32" (ByVal X As Integer, ByVal Y As Integer) As Integer
        Public Declare Function GetCursorPos Lib "user32" (ByVal lpPoint As POINTAPI) As Integer
        Public Const MOUSEEVENTF_LEFTDOWN = &H2
        Public Const MOUSEEVENTF_LEFTUP = &H4
        Public Const MOUSEEVENTF_MIDDLEDOWN = &H20
        Public Const MOUSEEVENTF_MIDDLEUP = &H40
        Public Const MOUSEEVENTF_RIGHTDOWN = &H8
        Public Const MOUSEEVENTF_RIGHTUP = &H10
        Public Const MOUSEEVENTF_MOVE = &H1
    
        Public Structure POINTAPI
            Dim X As Integer
            Dim Y As Integer
        End Structure
    
        Public Function GetCurrentX() As Integer
            Dim Position As POINTAPI
            GetCursorPos(Position)
            GetCurrentX = Cursor.Position.X
        End Function
    
        Public Function GetCurrentY() As Integer
            Dim Position As POINTAPI
            GetCursorPos(Position)
            GetCurrentY = Cursor.Position.Y
        End Function
    
        Public Function GetMousePos() As Integer
            GetCurrentX()
            GetCurrentY()
        End Function
    
        Public Sub LeftClick()
            LeftDown()
            LeftUp()
        End Sub
    
        Public Sub LeftDown()
            Mouse_Event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
        End Sub
    
        Public Sub LeftUp()
            Mouse_Event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
        End Sub
    
        Public Sub MiddleClick()
            MiddleDown()
            MiddleUp()
        End Sub
    
        Public Sub MiddleDown()
            Mouse_Event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0)
        End Sub
    
        Public Sub MiddleUp()
            Mouse_Event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0)
        End Sub
    
        Public Sub MoveMouse(ByVal xMove As Integer, ByVal yMove As Integer)
            mouse_event(MOUSEEVENTF_MOVE, xMove, yMove, 0, 0)
        End Sub
    
        Public Sub RightClick()
            RightDown()
            RightUp()
        End Sub
    
        Public Sub RightDown()
            Mouse_Event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
        End Sub
    
        Public Sub RightUp()
            Mouse_Event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)
        End Sub
    Last edited by joefox; Oct 3rd, 2010 at 08:23 PM.

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

    Re: Simulate mouse click on form

    try this:

    vb Code:
    1. 'Button to move mouse and click
    2. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    3.     Dim p As Point = Me.PointToScreen(New Point(653, 264))
    4.     MoveMouse(p.x, p.y)
    5.     LeftClick()
    6. End Sub

  3. #3

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: Simulate mouse click on form

    Cool thanks!

    Now is there a way to copy whats in textbox1 to where the current mouse is at?

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

    Re: Simulate mouse click on form

    Quote Originally Posted by joefox View Post
    Cool thanks!

    Now is there a way to copy whats in textbox1 to where the current mouse is at?
    how do you mean? draw it on the form, or is the another textbox there?

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