Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding a window and clicking a button? 1

Status
Not open for further replies.

davem99

Programmer
May 19, 2003
74
US
I have a VB app that hosts an IE browser control. This app interacts with a web site. It enters text, clicks buttons etc..etc..

Sometimes, the website throws a Java Alert() - something like "10 records found. Click OK to view".....so my app stops because of the modal dialog box.

How can I find this window, figure out how many records it found and programatically click OK or Cancel?

thanks!!
 

FindWindow or EnumWindows, EnumChildWindows, SendMessage and a few others, but the problem is with the single threaded nature of VB. From what you are saying is that the execution of your program stops if the dialog appears. How then will you execute the find code if your program has stopped running while waiting for this dialog to go away?

So if you then use a "watcher" program that looks for this dialog you will then need to be able to communicate between the programs. (Look up at MS "176058 HOWTO" for one way to do it.)

Good Luck

 
Thanks VB5Prgrmr!

Unfortunately I think that a "watcher" program is the way to go......

Could I ask for a little more assistance on this? Assuming I simply want an app to sit and wait, look for a specific window and see if there's a dialog box. If there is I sent a "space" or "cr" key.

Alternatively, can I hook into the OS to capture the dialog post and deal with it proactively?

Thanks!!!

 

Actually if code it into a loop it will click the button so fast the user would just see a flash on the screen (undesirable effect I know but...).

Actually you would/could use SendMessage with BM_CLICK as the arguement or BM_SETSTATE or both the WM_LBUTTONDOWN and WM_LBUTTONUP events with MK_LBUTTON as the third arguement (or if your paranoid you can use them all one after the other).

Now about the hooking ... Don't know never tried so the best you will get from me is ... maybe???

Oh yeah GetWindowText is another api

and have you read FAQ222-2244 item 15 yet?

Good Luck

 
Thanks again, VB5!

One last question (I promise!)....

Assume I have the window handle. How can I determine the state of the window to see IF I need to send a message?

Thanks again - I really appreciate it!!

-dave
 

I am not sure what you mean by "State". Do you mean checking the state of an option button or check box?

 
I mean figuring out when to send the 'OK' message to the other app.

In other words, the watcher needs to monitor the state of the window to see if a dialog has been posted, otherwise it will simply send messages repeatedly potentially messing something up.
 

Ahhh... I see what you mean. No it wouldn't. If you are using EnumChildWindows you can test for what type of child window it is. Best bet is to use SPY++ to find the class and other information about the window for your program, and along with GetWindowText you test and retrieve the contents of the msgbox/alert. Then once you know that you have this msgbox/alert box and you know what class it is and you retrieve its title and ect. ect. you can make sure that it is what you are looking for and has the required button(s) that you are looking for and ect. ect.

Then once you have confirmed that you need to send a click then you use the SendMessage API to "Click" on the button.

Good Luck

 
Hmmmmm........so I tried the MS HOWTO and it works perfectly.

Then I mingled the code into my app. The browser throws the Alert but I'm struggling to figure out how to find the handle for the browser or the Alert window itself.
 

Ok, do you have SPY++ on you computer? If so you can use it to look at the properties of the alert window (the handle will change every time it is invoked so you can depend upon that value). Copy down its class and other relavent information.

Now use spy++ on your vb application (I think the class will return as a ThunderForm).

I do not know if you know how to use enumwindows or findwindow but you can use either to find your application and retrieve a handle to the window. Once you have this handle you will use it to EnumChildWindows. This will be your loop in the watcher program (psudo)...

Start
FindProgramToWatch'enumwindows or findwindow
Do Until ProgramToWatchEnds = True
If CallEnumChildWindowsToLookForAlertWindow = True Then
SendClickMessagesToAlertWindow
End If
If ProgramToWatchNotThere Then ProgramToWatchEnds = True
Loop
End

Although you may want to slow down the above loop via a sleep of some similar method.

With the EnumChildWIndows you will be looking for the class of the alert window and possibly its title (if it stays the same which I think it does).

Now, inversely you could make it so that you launch the watcher program which in turn launches your main program, and in your watcher program you can use the shell and wait algorithm that you can find on this site with a search for Shell And Wait. While you are in a loop waiting on the main program you would have an inner loop that looks for the alert window via the enumchildwindows API. Just another way to achieve what you want. If I have confused you please disregard this paragraph.

With a search of the web or this site you may find examples on EnumWindows and or EnumChildWindows.

Good Luck

 
Okey Dokey.....

I have a sample form with a browser control in it which loads an html doc with an alert().

I snagged some code that iterates through the window handle and spews this out:

Class: ThunderFormDC
hWnd: 131866
Text: [Target]
Class: ThunderCommandButton
hWnd: 131872
Text: [Go]
Class: Shell Embedding
hWnd: 197326
Text: []
Class: Shell DocObject View
hWnd: 197502
Text: []
Class: Internet Explorer_Server
hWnd: 131974
Text: []

None of these handles seems to respond to the sendmessage
 

[tt]
Class: ThunderFormDC 'should be vb form
hWnd: 131866
Text: [Target]
Class: ThunderCommandButton 'should be your alert box command button
hWnd: 131872
Text: [Go]
[/tt]

Do you have spy++?

Good Luck

 
I found Spy++ and found this:

The dialog I'm trying to kill lives under the root (Desktop) and includes the OK, Cancel and the Text I want to find.

I can actually find the dialog by calling

FindWindow(vbNullString, "Microsoft Internet Explorer")

and I get the same hWnd that Spy++ gives me.

So....I can find the window handle and iterate through it to get:

Class: #32770
hWnd: 393406
Text: [Microsoft Internet Explorer]
Class: Button
hWnd: 1245798
Text: [OK]
Class: Button
hWnd: 131564
Text: [Cancel]
Class: Static
hWnd: 131622
Text: []
Class: Static
hWnd: 131594
Text: [Your criteria returned 1 record. Do you want to execute this search?]

Almost there!!!!

Last thing....How do I send a message to one of these buttons to make this happen?

Thanks again VB5Prgmr - I really appreciate it!!!

 

Remember the paranoid remark...
[tt]
'send a click message to the button
Result = SendMessage(hWnd, BM_CLICK, 0, 0)
'Change its active state
Result = SendMessage(hWnd, BM_SETSTATE, 1, 0)
'Send the mouse down event
Result = SendMessage(hWnd, WM_LBUTTONDOWN, MK_LBUTTON, 0)
'Send the mouse up event
Result = SendMessage(hWnd, WM_LBUTTONUP, MK_LBUTTON, 0)
[/tt]

Good Luck

 
I have never used Spy++ before. Is there a good tutorial anywhere or is the best way to jump in and use it? I'm a bit under the gun so I can't take too long to try and figure out if I can use it to help me solve my current problem.

Also, where do you go to get the type of in depth knowledge demonstrated by your last post with the 4 SendMessage items. They are very logical and intuitive when I see your example but it would have taken me some time to put the whole puzzle together on my own? Thanks!

Have a great day!

j2consulting@yahoo.com
 

SBendBuckeye,

Please, in the future do not "highjack" another persons thread, please start your own because now davem99 has recieved 2 emails about this thread (see above on the right where it says "Who's Marked This Thread?").

Also if you need to reference another thread all you need to do is type in what you see at the top just under the subjects caption and right above the tabs like this thread713-744609. This site has "macros" (if you will) that look for keywords (thread, forum, faq etc.) and correct formatting and will automatically turn them into links.

Now for you question...

As for tutorials there are probably some somewhere but I just jumped in. So for a short tutorial on how to capture the information of a window go ahead and start spy. Start another program and switch back to spy. Make sure you can see both programs. Look at the Spy menu and either click on Find Window or use its shortcut of Ctrl+F. A small dialog will popup with a bullseye icon in the center. Grab this with your mouse and drag it over to the other programs window and release. Click OK.

For you second question it takes time, a thirst for knowledge, sites like this one, and in my case I learned that from someone who has been in the business longer than I have. :)

Good Luck

 
Hello vb5prgrmr,

Thanks for the example and also for pointing out the impact of adding additional stuff onto an existing thread. Duh, it seems pretty obvious after you mention it.

Hello davem99,

I apologize for cluttering up your thread. I meant no harm and will more careful in the future.

Have a great day!

j2consulting@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top