CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2005
    Posts
    151

    Clicking Toolbar button on another app

    Hi, does anybody know how to click the first Toolbar button on another app?
    I've already asked this on several places, but nobody seems to know how to do it. I tried TB_PRESSBUTTON and it sets the first button to tbrPressed, but it doesn't run the Toolbar click event.

    Code:
    Option Explicit
    
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    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
    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
    'Private Const BM_CLICK As Long = &HF5
    
    Private Const WM_USER = &H400
    Private Const TB_PRESSBUTTON = (WM_USER + 3)
    
    Private Sub Command1_Click()
        Dim hParent As Long
        Dim hChild As Long
        
        hParent = FindWindow("TfrmMain", vbNullString)
        
        If hParent <> 0 Then
            
            hChild = FindWindowEx(hParent, 0&, "TPanel", vbNullString)
            hParent = FindWindowEx(hParent, hChild, "TPanel", vbNullString)
            hChild = FindWindowEx(hParent, 0&, "TPanel", vbNullString)
            hParent = FindWindowEx(hParent, hChild, "TPanel", vbNullString)
            hChild = FindWindowEx(hParent, 0&, "TToolbar", vbNullString)
            
            If hChild <> 0 Then
                Call SendMessage(hChild, TB_PRESSBUTTON, 0, 0)
            End If
        End If
    End Sub

  2. #2
    Join Date
    Mar 2005
    Posts
    226

    Re: Clicking Toolbar button on another app

    You are only doing one action. You need to press it, wait a few miliseconds and then release it.

    I thought 0 was false, so it only looks like you are releasing it.

  3. #3
    Join Date
    Dec 2005
    Posts
    151

    Re: Clicking Toolbar button on another app

    Do you mean like this?

    Code:
        Call SendMessage(hChild, TB_PRESSBUTTON, 0, True)
        Sleep (200)
        Call SendMessage(hChild, TB_PRESSBUTTON, 0, False)
    It makes no difference. The button is pressed and stays pressed. Nothing else happens.

  4. #4
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: Clicking Toolbar button on another app

    if i can still remember, you have to send WM_COMMAND message to the parent window together with the button command id and the toolbar window handle.
    Busy

  5. #5
    Join Date
    Dec 2005
    Posts
    151

    Re: Clicking Toolbar button on another app

    After some searching I found this and it works fine with Internet Explorer.

    http://www.vbforums.com/showpost.php...04&postcount=4

    But if I replace the data in the "TestClickingToolbarButton" sub with my data, then it doesn't work.

    Code:
    Private Sub TestClickingToolbarButton()
        Dim hParent As Long
        Dim hChild As Long
        
        hParent = FindWindow("TfrmMain", vbNullString)
        hChild = FindWindowEx(hParent, 0&, "TPanel", vbNullString)
        hParent = FindWindowEx(hParent, hChild, "TPanel", vbNullString)
        hChild = FindWindowEx(hParent, 0&, "TPanel", vbNullString)
        hParent = FindWindowEx(hParent, hChild, "TPanel", vbNullString)
        hChild = FindWindowEx(hParent, 0&, "TToolbar", vbNullString)
    
        ClickToolbarButtonByIndex hChild, 1
    
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured