Results 1 to 9 of 9

Thread: How can you click a button in another program if you know the program/button's title?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2003
    Posts
    1,269

    How can you click a button in another program if you know the program/button's title?

    Anyone know how I can click a button in another program, knowing the text (caption/title) on the button and of the window?

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: How can you click a button in another program if you know the program/button's title?

    By sending BM_CLICK to the control. The following code will click the 8 button in Calculator.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" ( _
    4.                 ByVal hWnd As Long, _
    5.                 ByVal Msg As Long, _
    6.                 wParam As Any, _
    7.                 lParam As Any) As Long
    8. Private Declare Function SetActiveWindow Lib "user32.dll" ( _
    9.                 ByVal hWnd As Long) As Long
    10. Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" ( _
    11.                 ByVal lpClassName As Any, _
    12.                 ByVal lpWindowName As Any) As Long
    13. Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" ( _
    14.                 ByVal hwndParent As Long, _
    15.                 ByVal hwndChildAfter As Long, _
    16.                 ByVal lpszClass As Any, _
    17.                 ByVal lpszWindow As Any) As Long
    18.    
    19. Private Const BM_CLICK As Long = &HF5
    20.  
    21. Private Sub Command1_Click()
    22.     Dim appHandle       As Long
    23.     Dim buttonHandle    As Long
    24.     Dim retval          As Long
    25.    
    26.     appHandle = FindWindow("SciCalc", vbNullString)
    27.     If appHandle = 0 Then Exit Sub
    28.    
    29.     buttonHandle = FindWindowEx(appHandle, 0, CLng(0), "8")
    30.    
    31.     retval = SetActiveWindow(appHandle)
    32.     retval = SendMessage(buttonHandle, BM_CLICK, ByVal CLng(0), ByVal CLng(0))
    33. End Sub
    Last edited by dee-u; Jan 9th, 2009 at 01:26 AM.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: How can you click a button in another program if you know the program/button's title?

    I wrote a generic automation module to handle this exact type of thing. It has several helpful functions for automation, all of which are based on window and button captions. Copy the code into its own module and toss it into your project and you're all set.

    To use dee-u's example of pressing the 8 button on the calculator, you'd simply call:

    ClickButton "Calculator", "8"
    Code:
    Option Explicit
    
    Public Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    Private Declare Function IsWindowEnabled Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long
    Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
    Private mstrButton As String
    Private mlngButton As Long
    
    Public Function ButtonEnabled(pstrWindow As String, pstrButton As String) As Boolean
        Const WS_DISABLED = &H8000000
        Const GWL_STYLE = (-16)
        Dim lngWindow As Long
        Dim lngButton As Long
        
        mstrButton = pstrButton
        mlngButton = 0
        lngWindow = FindWindow(vbNullString, pstrWindow)
        If lngWindow <> 0 Then
            EnumChildWindows lngWindow, AddressOf EnumChildProc, ByVal 0&
            If mlngButton <> 0 Then ButtonEnabled = (IsWindowEnabled(mlngButton) = 1)
        End If
    End Function
    
    Public Function ButtonExists(pstrWindow As String, pstrButton As String) As Boolean
        Dim lngWindow As Long
        Dim lngButton As Long
        
        mstrButton = pstrButton
        mlngButton = 0
        lngWindow = FindWindow(vbNullString, pstrWindow)
        If lngWindow <> 0 Then
            EnumChildWindows lngWindow, AddressOf EnumChildProc, ByVal 0&
            If mlngButton <> 0 Then ButtonExists = True
        End If
    End Function
    
    Public Function ClickButton(pstrWindow As String, pstrButton As String) As Boolean
        Const BM_CLICK As Long = &HF5&
        Const WS_DISABLED = &H8000000
        Const GWL_STYLE = (-16)
        Dim lngWindow As Long
        Dim lngParam1 As Long
        Dim lngParam2 As Long
        
        mstrButton = pstrButton
        mlngButton = 0
        lngWindow = FindWindow(vbNullString, pstrWindow)
        If lngWindow <> 0 Then
            EnumChildWindows lngWindow, AddressOf EnumChildProc, ByVal 0&
            If mlngButton <> 0 Then
                If IsWindowEnabled(mlngButton) = 1 Then
                    PostMessage mlngButton, BM_CLICK, 0, 0&
                    ClickButton = True
                End If
            End If
        End If
    End Function
    
    Public Function CloseWindow(pstrWindow As String) As Boolean
        Const WM_CLOSE = &H10
        Dim lngWindow As Long
        
        lngWindow = FindWindow(vbNullString, pstrWindow)
        If lngWindow <> 0 Then
            SendMessage lngWindow, WM_CLOSE, 0, 0&
            CloseWindow = True
        End If
    End Function
    
    Public Function CloseWindowIfButtonExists(pstrWindow As String, pstrButton As String, pblnExists As Boolean) As Boolean
        Const WM_CLOSE = &H10
        Dim lngWindow As Long
        Dim lngButton As Long
        Dim blnExists As Boolean
        
        mstrButton = pstrButton
        mlngButton = 0
        lngWindow = FindWindow(vbNullString, pstrWindow)
        If lngWindow <> 0 Then
            EnumChildWindows lngWindow, AddressOf EnumChildProc, ByVal 0&
            blnExists = (mlngButton <> 0)
            If blnExists = pblnExists Then
                SendMessage lngWindow, WM_CLOSE, 0, 0&
                CloseWindowIfButtonExists = True
            End If
        End If
    End Function
    
    Public Function EnumChildProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
        Dim strCaption As String
        
        strCaption = Space$(GetWindowTextLength(hwnd) + 1)
        GetWindowText hwnd, strCaption, Len(strCaption)
        strCaption = Left$(strCaption, Len(strCaption) - 1)
        If strCaption = mstrButton Then
            mlngButton = hwnd
        Else
            EnumChildProc = 1
        End If
    End Function
    
    Public Function WindowIsOpen(pstrWindow As String) As Boolean
        WindowIsOpen = (FindWindow(vbNullString, pstrWindow) <> 0)
    End Function

  4. #4
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: How can you click a button in another program if you know the program/button's title?

    Quote Originally Posted by dee-u
    By sending BM_CLICK to the control. The following code will click the 8 button in Calculator.
    FYI: The code that dee-u posted does not look for the app's Title Bar Name. "SciCalc" is the Class of the Calculator and this is what FindWindow is looking for.

    Included in your VB package is a utility named SpyXX.exe. This utility is invaluable when working with inter-app communication. That's how I determined that "SciCalc" is the Class of the Calculator.

    dee-u...... NICE!
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  5. #5
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: How can you click a button in another program if you know the program/button's title?

    Quote Originally Posted by Ellis Dee
    I wrote a generic automation module to handle this exact type of thing. It has several helpful functions for automation, all of which are based on window and button captions. Copy the code into its own module and toss it into your project and you're all set.

    To use dee-u's example of pressing the 8 button on the calculator, you'd simply call:

    ClickButton "Calculator", "8"
    Ellis, this is nice too. It would be advantageous if you would post calling examples for each function. Just a thought...
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  6. #6
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: How can you click a button in another program if you know the program/button's title?

    Quote Originally Posted by CDRIVE
    Ellis, this is nice too. It would be advantageous if you would post calling examples for each function. Just a thought...
    It's pretty basic stuff:
    vb Code:
    1. If ButtonEnabled("Calculator", "8") Then
    2.     ' There is a program titled "Calculator" open, it has a button titled "8",
    3.     ' and that button is enabled
    4. End If
    5.  
    6. If ButtonExists("Calculator", "8") Then
    7.     ' There is a program titled "Calculator" open, and it has a button titled "8"
    8. End If
    9.  
    10. If ClickButton("Calculator", "8") Then
    11.     ' There is a program titled "Calculator" open, it has a button titled "8",
    12.     ' the button is enabled, and it was just clicked
    13. End If    
    14.  
    15. If CloseWindow("Calculator") Then
    16.     ' The "Calculator" app was just closed
    17. End If
    18.  
    19. If WindowIsOpen("Calculator") Then
    20.     ' The calculator is open
    21. End If
    22.  
    23. If CloseWindowIfButtonExists("AVG Anti-Virus Free", "Close Results") Then
    24.     ' Calling this inside a timer with ann interval of around 4 seconds
    25.     ' will wait until AVG has finished its scan, when it then displays
    26.     ' a Close Results button. Alternately you could just keep trying to
    27.     ' the press the Close Results button using the ClickButton() function
    28.     ' and waiting for success. That's the approach I ended up using,
    29.     ' but since I already wrote this function I left it in the module.
    30. End If

  7. #7
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: How can you click a button in another program if you know the program/button's title?

    Quote Originally Posted by Ellis Dee
    It's pretty basic stuff:
    Perhaps, but the examples you posted will prevent others from finding this post someday and asking this question. It is also presumptuous to assume that members of all levels of expertise will regard your code as basic stuff. Think about it.
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  8. #8
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: How can you click a button in another program if you know the program/button's title?

    The code isn't basic; calling it is.

    C'mon, you have to admit the calls are basic.

  9. #9
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: How can you click a button in another program if you know the program/button's title?

    Quote Originally Posted by Ellis Dee
    The code isn't basic; calling it is.

    C'mon, you have to admit the calls are basic.
    Yes the calls are basic to about 65% of programmers. So you did a favor for the other 35% and they'll thank you for it.
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

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