Simulate a mouse click in a program  
Author Message
Jack0000





PostPosted: Visual Basic General, Simulate a mouse click in a program Top

I have a VB program and I need to simulate a mouse click in a webbrowser window. How would I go about simulating that click on the screen.

Thanks


Visual Basic20  
 
 
Twisted Hardware





PostPosted: Visual Basic General, Simulate a mouse click in a program Top

Hi There,
This is done using API call, This is the way to do any mouse action you need:

1- First you declare some API Functions, Subs and Constants

Public Declare Auto Function SetCursorPos Lib "User32.dll" (ByVal X As Integer, ByVal Y As Integer) As Long
Public Declare Auto Function GetCursorPos Lib "User32.dll" (ByRef lpPoint As Point) As Long
Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
Public Const MOUSEEVENTF_LEFTUP = &H4 ' left button up
Public Const MOUSEEVENTF_MIDDLEDOWN = &H20 ' middle button down
Public Const MOUSEEVENTF_MIDDLEUP = &H40 ' middle button up
Public Const MOUSEEVENTF_RIGHTDOWN = &H8 ' right button down
Public Const MOUSEEVENTF_RIGHTUP = &H10 ' right button up

2- Then you call your functions and subs
2.1- To move the cusor:
   SetCursorPos(X,Y) ' Where X and Y are in pixel
2.2- To get cursor location
   Dim tempPos As Point
   
Dim R As Long = GetCursorPos(tempPos) ' You'll get your location in TempPos
2.3- To semulate clicks
2.3.1 Left click
   mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
   mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
2.3.2 Right click
   mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
   mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)
2.3.3 Middle Click
   mouse_event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0)
   mouse_event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0)

2.4- To semulate drag and drop:
   mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
   SetCursorPos(X,Y) ' Where X and Y are in pixel
   mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)


I hope that answered your problem :)

If you have any technical questions about APIs or any other detail in my message feel free to contact me.

Abdullah Al-Rasheed


 
 
ReneeC





PostPosted: Visual Basic General, Simulate a mouse click in a program Top

Abdullah,

Is this is VB6 code, you can tell by the long declarations in the api calls.

Since this is a .net forum, it's very safe to assume that users are seeking .dot net solutions.

Your solution may or may not work were it moved in dot net if the longs were changed to integers. The reason that no one could say right off the bat is because it actually depends on how the dll passes data back to the caller. Very often marshalling and interop interfaces are required.

 
 
ggunn





PostPosted: Visual Basic General, Simulate a mouse click in a program Top

how would one get the position of a button in the webbrowser control, assuming one knows the name of the button or would one have to take a guess
 
 
Twisted Hardware





PostPosted: Visual Basic General, Simulate a mouse click in a program Top

Hi ReneeC,

   This was code is not VB6!! although I got all API declares from 'API Browser' but I modified the code to work with VB.NET and by the way I never send any code before testing it.

   And about the Long decalred parameters, they are not used in the program. so it doesn't realy matter if you use int32 'Integer' or int64 'Long'.

  Anyway it's better to change Long paramters to Integer when you get API declares from 'API Browser' even if you'r not going to use them :)

Abdullah Al-Rasheed

 
 
jgorlick





PostPosted: Visual Basic General, Simulate a mouse click in a program Top

Most often you'd use the API FindWindow().  The VS tool Microsoft Spy++ comes in mighty handy for determining the parameter values.

This is, however, not one of those FindWindow() cases.  If you Spy++ the button, you will find that the window is the "Internet Explorer_Server" (class) window.  That is, if you are using IE.

In this case, a way to determine the coordinates of the button is to use DHTML.  Best yet, use the most browser-agnostic DHTML possible.  I recommend using getElementById() to get the button.

Once you have the button, use the properties to determine where the button is coordinate-wise.  The following documentation should be helpful:
http://msdn.microsoft.com/library/default.asp url=/workshop/author/dhtml/reference/objects/input_submit.asp

Then tie this in with Abdullah's code.

 
 
averge joe





PostPosted: Visual Basic General, Simulate a mouse click in a program Top

Does anyone know what the equivelent of this code:

Public Declare Auto Function SetCursorPos Lib "User32.dll" (ByVal X As Integer, ByVal Y As Integer) As Long
Public Declare Auto Function GetCursorPos Lib "User32.dll" (ByRef lpPoint As Point) As Long
Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
Public Const MOUSEEVENTF_LEFTUP = &H4 ' left button up
Public Const MOUSEEVENTF_MIDDLEDOWN = &H20 ' middle button down
Public Const MOUSEEVENTF_MIDDLEUP = &H40 ' middle button up
Public Const MOUSEEVENTF_RIGHTDOWN = &H8 ' right button down
Public Const MOUSEEVENTF_RIGHTUP = &H10 ' right button up...... ect.ect.ect.ect.ect.

In visual basic express edition or any other express edition. anything would be helpful


 
 
jgorlick





PostPosted: Visual Basic General, Simulate a mouse click in a program Top

The WinApi.cs code in the following article is a good example:

http://www.eggheadcafe.com/articles/transparentform_send_mouse_click_to_desktop.asp

Porting this to VB.NET is only straight forward if you are familiar with the DllImport portion. Since that's what you are basically asking for help on...the important portion (the API call) ported...

<DllImport(user32.dll)>

Private Shared Sub Mouse_Event( _

ByVal dwFlags As Integer, _

ByVal dx As Integer, _

ByVal dy As Integer, _

ByVal dwData As Integer, _

ByVal dwExtraInfo As IntPtr_

)

Often times the easiest way to get VB.NET help is to lean on the C# rather than the VB community. An API tool in .NET similar to the one that we had in VB would be ideal.


 
 
Twisted Hardware





PostPosted: Visual Basic General, Simulate a mouse click in a program Top

Hi Everyone,

I know this is an old topic, but I reveived alot of emails on this subject since i wrote it more than one year ago, in fact, I'm still receiving messages on this subject. So let's solve this once and for all by posting the right declarations for the API. Thiw WILL WORK fine on VB 2003 and VB 2005:

Public Declare Auto Function SetCursorPos Lib "User32.dll" (ByVal X As Integer, ByVal Y As Integer) As Integer
Public Declare Auto Function GetCursorPos Lib "User32.dll" (ByRef lpPoint As Point) As Integer
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 Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
Public Const MOUSEEVENTF_LEFTUP = &H4 ' left button up
Public Const MOUSEEVENTF_MIDDLEDOWN = &H20 ' middle button down
Public Const MOUSEEVENTF_MIDDLEUP = &H40 ' middle button up
Public Const MOUSEEVENTF_RIGHTDOWN = &H8 ' right button down
Public Const MOUSEEVENTF_RIGHTUP = &H10 ' right button up

I hope that helped

Have fun people, and happy Ramadan

Abdullah Al-Rasheed