Send mouse clicks to applications 
Author Message
 Send mouse clicks to applications
Dear All,

Question 1
I have a program that I can't quite control with Sendkeys, ENTERs and TABs.
The program window is always in one place, so I could easily automate tasks
by moving the mouse over certain areas (this works fine), and then send
mouse clicks...

Question 2
Maybe it works the same way, but can I send mouse clicks to certain
coordinates, regardless of what application is the mouse over?

I'd appreciate any help on how to do this in VB.

TIA for your help.



Sat, 28 Jun 2003 16:14:57 GMT  
 Send mouse clicks to applications
Yes, you can accomplish that using the mouse_event API.  This has the same
problem though as using SendKeys, in that if any other app comes on top
while your program is running, it will get the click...so you really can't
do anything else while your app is running.

A much better solution is to use SendMessage or PostMessage to send messages
to the application you are trying to control.  The great thing about it is
that the app you are manipulating doesn't need to be on top, or have
focus...it can even be minimized and it will still receive the messages, so
you can be clicking and typing away in another window while your program is
sending mouse clicks and text to another app.

All you need for this to work is the hWnd values of the controls you are
clicking or sending text to.  You don't need to send a click to a text box
before sending the text, so the click would really just be for simulating a
button click.

To test:

Start a new project, add two command buttons, and paste in the code below.
Then start the app you want to control, get the hWnd for a couple of buttons
and text boxes in that app by using Spy++,  and replace the value for
"thehWnd" in the examples below with the values that Spy++ gives you.  I
have used "SendMessage" in the examples below, but "PostMessage" would work
as well.

'Start Code
Option Explicit
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam
As Any) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA"
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam
As Long) As Long
Const WM_CHAR = &H102
Const WM_LBUTTONDOWN = &H201
Const WM_LBUTTONUP = &H202
Const MK_LBUTTON = &H1

Private Sub Command1_Click()
    Dim retval As Long
    Dim thehWnd As Long

    thehWnd = 'Enter a value here...For testing, use Spy++ to get the hWnd
of a button in the program you want to control

    retval = SendMessage(thehWnd, WM_LBUTTONDOWN, ByVal CLng(MK_LBUTTON),
ByVal CLng(0))
    retval = SendMessage(thehWnd, WM_LBUTTONUP, ByVal CLng(MK_LBUTTON),
ByVal CLng(0))
End Sub

Private Sub Command2_Click()
    Dim retval As Long, thehWnd As Long, theKey As Long
    Dim myString As String, myString1 As String
    Dim i As Integer

    thehWnd = 'Enter a value here...For testing, use Spy++ to get the hWnd
of the textbox in the program you want to control

    myString = "This is some text that will be entered into the textbox of
the other program"
    For i = 1 To Len(myString)
        myString1 = Mid(myString, i, 1)
        theKey = Asc(myString1)
        retval = SendMessage(thehWnd, WM_CHAR, CLng(theKey), ByVal CLng(0))
    Next i
End Sub
'End Code

To use this in your app, you'll need some other api calls to get the hWnd of
the app's main window and for each of the controls in the app.  There are
lots of examples on the net for how to get the hWnd of a control in another
app on-the-fly.  You'll want to use the FindWindow to get the hWnd for the
main window and GetWindow to get the hWnd for each of the child controls.



Sun, 29 Jun 2003 03:24:43 GMT  
 Send mouse clicks to applications
Thank you so much for your help! It worked out beautifully.


Quote:
> Yes, you can accomplish that using the mouse_event API.  This has the same
> problem though as using SendKeys, in that if any other app comes on top
> while your program is running, it will get the click...so you really can't
> do anything else while your app is running.

> A much better solution is to use SendMessage or PostMessage to send
messages
> to the application you are trying to control.  The great thing about it is
> that the app you are manipulating doesn't need to be on top, or have
> focus...it can even be minimized and it will still receive the messages,
so
> you can be clicking and typing away in another window while your program
is
> sending mouse clicks and text to another app.

> All you need for this to work is the hWnd values of the controls you are
> clicking or sending text to.  You don't need to send a click to a text box
> before sending the text, so the click would really just be for simulating
a
> button click.

> To test:

> Start a new project, add two command buttons, and paste in the code below.
> Then start the app you want to control, get the hWnd for a couple of
buttons
> and text boxes in that app by using Spy++,  and replace the value for
> "thehWnd" in the examples below with the values that Spy++ gives you.  I
> have used "SendMessage" in the examples below, but "PostMessage" would
work
> as well.

> 'Start Code
> Option Explicit
> Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
> (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal
lParam
> As Any) As Long
> Private Declare Function PostMessage Lib "user32" Alias "PostMessageA"
> (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal
lParam
> As Long) As Long
> Const WM_CHAR = &H102
> Const WM_LBUTTONDOWN = &H201
> Const WM_LBUTTONUP = &H202
> Const MK_LBUTTON = &H1

> Private Sub Command1_Click()
>     Dim retval As Long
>     Dim thehWnd As Long

>     thehWnd = 'Enter a value here...For testing, use Spy++ to get the hWnd
> of a button in the program you want to control

>     retval = SendMessage(thehWnd, WM_LBUTTONDOWN, ByVal CLng(MK_LBUTTON),
> ByVal CLng(0))
>     retval = SendMessage(thehWnd, WM_LBUTTONUP, ByVal CLng(MK_LBUTTON),
> ByVal CLng(0))
> End Sub

> Private Sub Command2_Click()
>     Dim retval As Long, thehWnd As Long, theKey As Long
>     Dim myString As String, myString1 As String
>     Dim i As Integer

>     thehWnd = 'Enter a value here...For testing, use Spy++ to get the hWnd
> of the textbox in the program you want to control

>     myString = "This is some text that will be entered into the textbox of
> the other program"
>     For i = 1 To Len(myString)
>         myString1 = Mid(myString, i, 1)
>         theKey = Asc(myString1)
>         retval = SendMessage(thehWnd, WM_CHAR, CLng(theKey), ByVal
CLng(0))
>     Next i
> End Sub
> 'End Code

> To use this in your app, you'll need some other api calls to get the hWnd
of
> the app's main window and for each of the controls in the app.  There are
> lots of examples on the net for how to get the hWnd of a control in
another
> app on-the-fly.  You'll want to use the FindWindow to get the hWnd for the
> main window and GetWindow to get the hWnd for each of the child controls.



Sun, 29 Jun 2003 06:18:38 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Sending mouse clicks to another application

2. sending mouse clicks to another application...

3. MOUSE CLICK - SENDING CLICK

4. Sending mouse clicks???

5. Firing or Sending Mouse Clicks

6. How to send mouse-clicks to another app?

7. Send mouse click to desktop, how?

8. Send Right mouse click

9. Sending Mouse Clicks??

10. Send a mouse click in VB3

11. Sending a mouse click from VB4?

12. Sending Mouse Clicks to Windows

 

 
Powered by phpBB® Forum Software