Image of Navigational Panel mapped to Contents / Home / Search Sending a Mouse Click to "OnTime"
Tech Support Question

Image of Line Break

Q. Visual Basic question - help please! I can not find an answer anywhere to this problem:

I need to send a mouse click to a program called "OnTime" from a Visual Basic program, to select an item from a list box, since OnTime will not allow using a SendKeys statement with shortcut keys to select an item from the listbox. OnTime will only accept a mouse click. I have done the following but it does not work properly. I need some advice.....

  Sub Form_Load ()

  OnTime = "OnTime"                ' Application title

  DoOnTimeCommands:
    On Error Resume Next
    AppActivate OnTime             ' Activate application
    DoEvents                       ' Let Windows do its thing

    If Err > 0 Then                ' Check Error Return
      MsgBox "Ontime Activate Error"
      GoTo EndProgram
    End IF 

  SendKeys "%U,s", True            ' alt-u, menu select
    SendKeys "ansell", True        ' Point to correct item
 
    xpos = 103: ypos = 114         ' Cursor coordinates
    Call SetCursorPos(xpos, ypos)  ' Put cursor on top of item 
                                   ' to be selected
 
    MsgReturn = SendMessage(GetFocus(), WM_LButtonDown , 0, WM_null)  
                                   ' Send
  Click

    MsgBox Str$(MsgReturn)         ' Display return
 
  EndProgram:
  End

I have been trying for days to get an answer to this question, through various avenues. Any information would be greatly appreciated.

Thanks,

From: Richard Stuemke.
Email: rstuemk@ccgate.sos.state.il.us
Organization: Illinois Secretary of State, Central Systems Services.

A. Richard,

I'm not sure exactly what is going wrong here, it's hard to tell without viewing the actual application that you are trying to communicate with.

There are two other things you might try:

  1. Get the hWnd of the listbox and send it the LB_SELECTSTRING message, passing the string as the last parameter. This is a pretty solid method. The only problem is to get the hWnd of the listbox. The simplest way to do that is to use some form of spy utility to get the identifier of the listbox, and then use the SendDlgItemMessage API to direct the message to the listbox, using the hWnd of the Window and the identifier of the listbox. You will find all the information you need to do that in your SDK help file.

  2. Use the SendMessage API to send the keystrokes via a more direct method. This will involve one call for every keystroke but it is a very direct method. SendKeys (by comparison) does a few more weird things on the way through.

As far as getting your current code to work, I would make a couple of suggestions:

  1. Also send a WM_LBUTTONUP Message to the listbox so that it gets a complete click. It may be that the listbox responds when the mouse button comes up.

  2. Check the declarations you are using for SendMessage. I always tend to declare it twice to get the correct declaration for both lParam types (longs and strings). Try the following:

      Declare Function SendMessageString Lib "User" 
              (ByVal hWnd As Integer, ByVal wMsg As Integer, 
               ByVal wParam As Integer, 
               ByVal lParam As String) As Long
      Declare Function SendMessageLong Lib "User" 
              (ByVal hWnd As Integer, ByVal wMsg As Integer, 
               ByVal wParam As Integer, 
               ByVal lParam As Long) As Long
    

  3. The lParam parameter is where you specify the position of the mouse click. The LOWORD specifies the x position and the HIWORD specifies the y position. You might want to try passing the hex value &H30003 for x,y of 3,3 instead of 0. This is just to make sure that you are well inside the control's border region.

That's all that comes to mind at the moment. Let me know if you still have problems and I will try to replicate the problem. Hopefully, however, you now have some things to try which will either help directly or will unearth the answer indirectly.

Good Luck. Code long and prosper.


Image of Arrow to Previous Article Image of Arrow to Next Article

[TECH SUPPORT TOC]
Image of Line Break
[HOME] [TABLE OF CONTENTS] [SEARCH]