?
Solved

Simulate Human Clicks

Posted on 2006-04-25
11
Medium Priority
?
340 Views
Last Modified: 2008-03-17
I have this program I use to connect to the Internet via GPRS and I want the PC to go online after a power failure so the first and obvious step is to add this program to the Start menu.

Problem is the program doesn't has a 'connect on startup' option, you have to manually click a Connect button it has. How could I simulate the human click? Are there any API calls that could bring the program window to the XP 'Foreground' and later click on a certain Screen position?

Ty in advance.
0
Comment
Question by:jpp1jpp1
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 1

Expert Comment

by:butterhook
ID: 16533654
You can Subclass the window and access the button's click, you wouldn't even need to bring it to the foreground. It's quite complicated and I don't quite know how to do it, but search for 'Subclassing' and you may find some useful info.

You will need to get the handle (hWnd) for the application window and for the button within that window.

If you have Visual Studio 6 you can use the Spy++ application to find these values.
0
 

Expert Comment

by:aluay
ID: 16534285
My solution is that you do this:
1. Add the program to the registry startup with a command line argument...
for e.g.
AT HKEY_CURRENT_USER and the subkey
Software\Microsoft\Windows\CurrentVersion\Run

add your MyApp.Exe 1

2. Read this command line argument in Visual basic...if its set, then call the connect method
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 total points
ID: 16534387
You can't subclass an external application with VB (at least not without any externall DLLs written in another language).
You don't need subclassing to accomplish this anyways...

butterhook is right in that you need an hWnd for the external app though.  You can use the FindWindow() API to get a handle to the application based on its window title.  This requires an exact match.  Does the application have a static, non-changing title that is predictable?  If it doesn't then you can also use the FindWindow() API to find the window based on its classname (assuming the classname is unique to this app).  Use the Spy++ tool that comes with Visual Studio 6 to view the classname of the window.  If you don't have Spy++, do a Google search for the free tool called "WinSpector" (I've used it before and it seems to be safe and free of spyware).

Here ias a PAQ that demonstrate the FindWindow() API:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20919239.html


There are two basic ways to go about this:

(1) Simulate a mouse click by pressing the mouse button up/down at a specific location onscreen.  The location can be relative to the programs location on screen.  This is dependent upon the button being in the same location everytime the application is run.  Is this the case?  

If you use this method, then you can pass the hWnd for your window (determined by the FindWindow() call) to the GetWindowRect() API.  This API will give you the location onscreen of a window based on the passed in hWnd.  Once you have the location, you can simulate a mouse click at a predetermined offset using the mouse_event() API.

Here is a PAQ that demonstrates the GetWindowRect() API:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/VB_Controls/Q_21567355.html

Here is a PAQ that demonstrates the mouse_event() API:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21515914.html


(2) Send a message directly to the button in the application using SendMessage()/PostMessage().  This requires you to first find the main window handle and then drill down thru the hierarchy of child windows to find the correct button.  This can be tricky depending upon how the application was built.  Spy++/WinSpector are necessary to figure out the correct relationships between windows.  You can use the FindWindowEx() and/or GetNextWindow() APIs in combination to determine the hWnd of your button.  This approach is the more difficult of the two if you don't have experience in this kind of thing (it is also more difficult to describe how to accomplish since we often don't have the application in front of us to walk you thru it!).

Let us know if you want to try this approach instead...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jpp1jpp1
ID: 16534541
Yes, it's an external application and the window's caption doesn't change so I'm gonna try your 1st approach.
0
 

Expert Comment

by:aluay
ID: 16534854
i guess my solution is better,
instead of adding the application to the start menu, add it to the statr list of the registry....with a parameter in the command line....
easy and to the point....

0
 

Author Comment

by:jpp1jpp1
ID: 16534897
Aluay I'm trying to manage an external application, it's not mine
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16534903
@aluay...

I'm pretty sure the application in question was NOT written by jpp1pp1...and therefore this is not an option.

He confirmed in the comment just before yours that it is an "external application".
0
 

Author Comment

by:jpp1jpp1
ID: 16534937
Worked like charm ty Idle mind
0
 

Expert Comment

by:aluay
ID: 16535395
i am sorry guys...i recieved the comments so late.....
best luck
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16536351
You need help ironing out the code jpp1jpp1?  

Or is it all done?...
0
 

Author Comment

by:jpp1jpp1
ID: 16541535
I'd have sweared I accepted yesterday your answer. It's working great.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question