how to use API (sendmessage?) to click a button

I have written a program that enumerates all of the child windows in a running application.  I have found that by getting the class name of the child windows, it will return "Button" for buttons on the form.  I would like to know how to have my program click on the button based on the name of the button.  I thought maybe with SendMessage or PostMessage.

Thanks.
adjenninAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

adjenninAuthor Commented:
Adjusted points to 190
0
caraf_gCommented:
TheButtonInQuestion.SetFocus
SendKeys " "
0
adjenninAuthor Commented:
since the button is merely a handle to a child window, I cannot use .setfocus
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

caraf_gCommented:
Ah, sorry!
0
Erick37Commented:
Using PostMessage to click a button knowing its handle:

Option Explicit

Const WM_LBUTTONDOWN = &H201
Const WM_LBUTTONUP = &H202

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


Private Sub Command1_Click()
'Button that receives the posted messages
Debug.Print "Clicked"
End Sub

Private Sub Command2_Click()
'Post the messages knowing the hWnd
Call PostMessage(Command1.hwnd, WM_LBUTTONDOWN, 0, &H10001)
Call PostMessage(Command1.hwnd, WM_LBUTTONUP, 0, &H10001)
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Erick37Commented:
Note:
If you want to see the button press, insert a delay between the PostMessage calls:

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

....

Call PostMessage(Command1.hwnd, WM_LBUTTONDOWN, 0, &H10001)
Call Sleep(100)
Call PostMessage(Command1.hwnd, WM_LBUTTONUP, 0, &H10001)
0
adjenninAuthor Commented:
Thanks!  That is just what I had in mind.
0
adjenninAuthor Commented:
That did not work.  nothing happens when command2 is clicked.
0
caraf_gCommented:
Perhaps you could try using SendMessage instead of PostMessage? (blind guess)
0
Erick37Commented:
What did not work?
I tried the code, and it "clicked" a VB button and an external app's button as well.
0
adjenninAuthor Commented:
It wont work without SetFocus.  SetFocus won't work on an external app so you have to use the API version of SetFocus.  That will work.
0
Erick37Commented:
Use AppActivate "AppTitle" just before the PostMessage() calls.
0
adjenninAuthor Commented:
AppActivate will not work in most cases for Child windows without names.  This is the BEST way to accomplish it:

Private Declare Function SetFocusAPI Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long

call SetFocusAPI(hwnd)
...

then you can send the mouse clicks.
0
caraf_gCommented:
Nice detective work adjennin! I'm glad I've kept the notifications going!

Thanks

caraf_g
0
Erick37Commented:
The documentation for SetFocus states:
"The window must be associated with the calling thread’s message queue."
Therefore, SetFocus can not be used to set focus to _another_ application's window through VB.
0
caraf_gCommented:
There's always a snag... :-(
0
adjenninAuthor Commented:
That documentation for SetFocus is for the VB Property SetFocus and not the SetFocus API call.  The API call works, trust me.

0
Erick37Commented:
The documentation I quoted is for the SetFocus API.
http://msdn.microsoft.com/library/psdk/winui/keybinpt_8yb7.htm

I also tried to get it to work, but could not.  Open the FreeCell About dialog and try to click the OK button

AppActivate works:

Private Sub Command2_Click()
    '''This works
    Dim lHwnd As Long
    AppActivate "FreeCell"
    lHwnd = FindWindow(vbNullString, "About FreeCell")
    lHwnd = FindWindowEx(lHwnd, 0, "Button", "OK")
    Call PostMessage(lHwnd, WM_LBUTTONDOWN, 0, &H40004)
    Call Sleep(100)
    Call PostMessage(lHwnd, WM_LBUTTONUP, 0, &H40004)
End Sub


This did not work:

Private Sub Command1_Click()
    '''This does NOT work
    Dim lHwnd As Long
    lHwnd = FindWindow(vbNullString, "About FreeCell")
    Debug.Print SetFocusAPI(lHwnd) 'returns 0
    lHwnd = FindWindowEx(lHwnd, 0, "Button", "OK")
    Call PostMessage(lHwnd, WM_LBUTTONDOWN, 0, &H40004)
    Call Sleep(100)
    Call PostMessage(lHwnd, WM_LBUTTONUP, 0, &H40004)
End Sub
0
adjenninAuthor Commented:
I got it to work in my app.

I wrote a vb prog and put a button on it that said "OK".  then whn i click on it it pups up a msgbox with an "OK" button.  i had trouble with findwindow so i had to use the exact handle of the button.  sure enough, it will pop up the msgbox.
0
adjenninAuthor Commented:
my keyboard sucks.
0
Erick37Commented:
Post your code.
0
adjenninAuthor Commented:
I have the following mess before SetFocusAPI

If IsWindowEnabled(hwnd) = 0 Then retval = EnableWindow(hwnd, 1)
retval = ShowWindow(hwnd, SW_SHOW)
retval = SetForegroundWindow(hwnd)
retval = BringWindowToTop(hwnd)
retval = SetActiveWindow(hwnd)

these functions are all APIs.
0
Erick37Commented:
All you really need is:

retval = ShowWindow(hwnd, SW_RESTORE) 'Restores if minimized
retval = SetForegroundWindow(hwnd) 'Bring to top

After these 2 calls the window will be in the same state as if you called AppActivate.

You do not need to call SetFocusAPI, because SetForegroundWindow sets the window into focus.

Out of curiosity, what was the return value you got from SetFocusAPI?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.