Hello, does anybody know how to click/press the first button on the toolbar of an external application? The code below finds the button and presses it, but the event behind the button is not triggered. How can I trigger the event of that button?


Button is pressed, but event not triggered.



vb Code:
  1. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
  2. Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
  3. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
  4. 'Private Const BM_CLICK As Long = &HF5
  5.  
  6. Private Const WM_USER = &H400
  7. Private Const TB_PRESSBUTTON = (WM_USER + 3)
  8.  
  9. Private Sub Command1_Click()
  10.     Dim hParent As Long
  11.     Dim hChild As Long
  12.    
  13.     hParent = FindWindow("TfrmMain", vbNullString)
  14.     Debug.Print hParent
  15.    
  16.     If hParent <> 0 Then
  17.        
  18.         hChild = FindWindowEx(hParent, 0&, "TPanel", vbNullString)
  19.         hParent = FindWindowEx(hParent, hChild, "TPanel", vbNullString)
  20.         hChild = FindWindowEx(hParent, 0&, "TPanel", vbNullString)
  21.         hParent = FindWindowEx(hParent, hChild, "TPanel", vbNullString)
  22.         hChild = FindWindowEx(hParent, 0&, "TToolbar", vbNullString)
  23.         Debug.Print hChild
  24.        
  25.         If hChild <> 0 Then
  26.             Call SendMessage(hChild, TB_PRESSBUTTON, 0, 0)
  27.         End If
  28.     End If
  29. End Sub