|
-
August 30th, 2005, 10:40 AM
#7
Re: How do I send a "command" to a run application? Or check if a program is running?
Well, to achieve this you don't need to "focus" the window exactly... you can actually just get the windows handle (see below) and then use send keys with it.
To get the handle you can use the FindWindow API call. There is also a FindWindowLike function which I have posted here a couple of times, have a search for it. (I didn't write it... it came from M$, but I improved it abit).
To send the message you use the SendMessage API.
e.g. This code I wrote a while back... It was made to start a self extracted EXE made with winrar, and then automatically fill in the install location and click the install button.
Code:
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal Msg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
...
...
...
'Start the install process
Shell ("WinampToolbarCompressed.exe")
'Get the window handle
r = FindWindowLike(hWnds(), 0, "WinRAR self-extracting archive", "*", Null)
hWndOfMainWindow = hWnds(1)
'Get the text area of the combo box window
r = FindWindowLike(hWnds(), hWndOfMainWindow, "", "ComboBox", Null)
r = FindWindowLike(hWnds(), hWnds(1), "", "Edit", Null)
If r = 1 Then
'Sendkeys to the window handle
'Delete the current text
For i = 1 To 1000
r = SendMessage(hWnds(1), WM_KEYDOWN, 46, 0)
Next
'Enter the new text
For i = 1 To Len(lblInstallTo.Caption)
r = SendMessage(hWnds(1), WM_CHAR, Asc(Mid(lblInstallTo.Caption, i, 1)), 0)
Next i
End If
'Find and press the Install button
r = FindWindowLike(hWnds(), hWndOfMainWindow, "Install", "Button", Null)
r = SendMessage(hWnds(1), WM_LBUTTONDOWN, 1, 655370)
r = SendMessage(hWnds(1), WM_LBUTTONUP, 0, 655370)
...
...
...
Mike
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|