Asked By
Tobias
260 points
N/A
Posted on - 05/19/2011
I have a .Net application that I am developing using VB.Net. I am attempting to control a remote application. I found a similar post in this forum upon reading the values of a list box of a remote program. I am using that as reference and have made progress. Presently, I am stuck in attempting to trigger the click event of the list box. I am also new to this Windows Messaging API system. How can I trigger the click event on an item for a remote List box?
Please help.
Answered By
Ingrid
0 points
N/A
#101382
Cannot trigger the click event of a list box remotely
Using the Windows Application Programming Interface you can achieve greater sophistication in interaction with other programs. It allows you to send messages to the remote program from your program, simulating a human user input, either the keyboard, mouse or other human interface devices.
In order to fire the click event of the List box, you need to select them. You can use the Send Message function to notify the List Box that these items need to be selected. Assuming you have multiple selection enabled, you can use the SETSEL message to select the required list items.
Â
LB_SETSEL = &H185
Â
SendMessage(targetControl, LB_SETSEL, 1, Integer.Parse(i))
Â
The targetControl specifies the handle to the window of the List box. The LB_SETSEL is defined as:
Â
The next parameter, 1 , informs the List Box to "select". The last parameter is the List Index of the required item. Using this method you can select the desired items on the List Box.
Answered By
Tobias
260 points
N/A
#101383
Cannot trigger the click event of a list box remotely
Using the Send Message with LB_SETSEL message I was able to select multiple items on the List box; thank you Ingrid.
But the problem still remains – I cannot seem to trigger the click event on the List box on selecting a List box Item. The remote program populates a second List box, whenever you select an item from the first List box. The selection works only if I use the keyboard or the mouse. When I use the Send Message API to select the items, I can see that the selection happens, but the second List box does not get populated.
Â
Please help!
Answered By
Mathias
0 points
N/A
#101384
Cannot trigger the click event of a list box remotely
You can use the VB.Net "SendKeys" function instead of using the Windows API natively. The SendKeys function is actually a wrapper for the Send Message function. You can pass a predefined set of strings as commands to this function and it will do the needful. For example, if you want to simulate the enter key, you pass "{ENTER}" as the command; it is much easier to deal with than the Windows Messaging API.
SendKeys.SendWait("{ENTER}")
Â
SendKeys.SendWait(" ") 'to send a space
Â
In order for the SendKeys function to work, you need to have the required application in the foreground. This is because there is no provision to send the target application or title as a parameter to the SendKeys class.
Answered By
Edvard
0 points
N/A
#101385
Cannot trigger the click event of a list box remotely
You can try sending a mouse click event to the destination list box using the Windows Application Programming Interface. The mouse click event consists of a mouse down event and a mouse up event. You need to execute these together in order to complete the mouse click event. Try sending the click event to the list box. Sample code for it is attached:
Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4
Public Const MOUSEEVENTF_RIGHTDOWN = &H8
Public Const MOUSEEVENTF_RIGHTUP = &H10
Â
Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer).
Â
The above code uses the mouse_event API call. You will need to know the X,Y coordinate location of the destination control in order to use this message. To complete a left click the following need to be executed.
Â
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
Â
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
Â
Try sending the click events by getting the coordinates of the destination list box and passing it to the dx and dy parameters.
Answered By
Tobias
260 points
N/A
#101386
Cannot trigger the click event of a list box remotely
I tried the SendKeys function. It sometimes works and sometimes does not. I think there is a timing issue and the commands get lost. I tried the Windows API for mouse click as outline; it works only if the target application is stationary and in full screen. I am able to get the exact co-ordinates and send a mouse click provided the window is stationary and opens in the same location. The problem is the destination window opens up at a random location dependent on the screen size of the user desktop.
Is it possible to send the mouse events via targeting the window handle?
Answered By
Edvard
0 points
N/A
#101387
Cannot trigger the click event of a list box remotely
Well, you can send a mouse click via the Windows Application Programming Interface. You must be careful and you need to ensure that the mouse down and the mouse up events must be sent to the target application. If you miss to send the mouse up event, the target application will perceive that the mouse button is forever pressed!
Const WM_RBUTTONDOWN = &H204
Const WM_RBUTTONUP = &H205
Â
Private Declare Function SendMessage Lib "USER32.DLL" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer 'sending a right button click updates the selected indexes.
Â
SendMessage(targetControl, WM_RBUTTONDOWN, 0, 0)
SendMessage(targetControl, WM_RBUTTONUP, 0, 0)
Â
The above code allows you to send a mouse event direct to the windows handle. A right click is recommended as the left click will de-select the options in the list box.
Answered By
Tobias
260 points
N/A
#101388
Cannot trigger the click event of a list box remotely
Sending the mouse event via the Windows Application Programming Interface worked! Thank you Edvard for the advise! Now I am able to continue with my application! Thank you experts for the valuable advice!