• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 838
  • Last Modified:

Simulate Mouse Click

I have written my code to determine where the mouse needs to go on the screen but I have been unable to make it click.  I need it have it click on a specific button or field that I have identified in a web browser
0
f0rdmstang
Asked:
f0rdmstang
  • 4
  • 3
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
So you have already figured out the coordinates where you need to click?

If so, then use the mouse_event() API:

    Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Integer, _
        ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, _
        ByVal dwExtraInfo As Integer)

    Private Const MOUSEEVENTF_LEFTDOWN = &H2
    Private Const MOUSEEVENTF_LEFTUP = &H4

    Private Const MOUSEEVENTF_RIGHTDOWN = &H8
    Private Const MOUSEEVENTF_RIGHTUP = &H10

    Private Sub LeftMouseClick(ByVal x As Integer, ByVal y As Integer)
        Cursor.Position = New Point(x, y)
        mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0)
        mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0)
    End Sub
0
 
f0rdmstangAuthor Commented:
That worked great.  I had tried something like this before but did not realize that you had to do a down and then a back up.
0
 
f0rdmstangAuthor Commented:
well I though I had.  I'm using a webbrowser embeded in my VB.net app and I'm trying to find the exact XY where it is located on the screen.  for example the ID of the HTMLELEMENT would be "SubmitButton"
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You could try clicking on using the WebBrowser itself.

Something like...

    WebBrowser.Document.All.Item("SubmitButton").Click()

0
 
f0rdmstangAuthor Commented:
Due to some weirdness messing with the site I need to navigate I can not simple just send a click to the element as for some reason it doesn't work eventhough I simple mouse click (manually) does.  What I need is the XY coordinates of it.  I'm trying to work through HTMLELEMENT.parent and grab the XY of OffsetRectangle but it doesn't seem to be giving me the corrent coordinates in conjuntion with the me.top, me.left and webbrowser.location.x, webbrowser.location.y  Any and all help is greatly appreciated.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I don't have much experience working with HTML objects unfortunately.  I would post another question and ask how to get the screen coordinates of a button in a web page.
0
 
neta1oCommented:
Which place do you put the coordinates in this code?
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Integer, _
        ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, _
        ByVal dwExtraInfo As Integer)

    Private Const MOUSEEVENTF_LEFTDOWN = &H2
    Private Const MOUSEEVENTF_LEFTUP = &H4

    Private Const MOUSEEVENTF_RIGHTDOWN = &H8
    Private Const MOUSEEVENTF_RIGHTUP = &H10

    Private Sub LeftMouseClick(ByVal x As Integer, ByVal y As Integer)
        Cursor.Position = New Point(x, y)
        mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0)
        mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0)
    End Sub
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You just call LeftMouseClick() and pass in the coords...

    LeftMouseClick(500, 250)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now