Click buttons in other programs using the API
By: Phoenix293@aol.com Date: 24/09/1998

To try this example, you will need to follow the steps indicated below. This example requires one form consisting of only one command button, and a BAS module to hold the declarations and one SUB.
Start Visual Basic and add a new module to the project. Inside this module, insert the constants and declarations shown below:

Global Const WM_COMMAND = &H111
Global Const GW_Child = 5
Global Const GW_HWNDFIRST = 0
Global Const GW_HWNDLAST = 1
Global Const GW_HWNDNEXT = 2
Global Const GW_HWNDPREV = 3
Global Const GW_OWNER = 4

Declare Function GetDesktopWindow Lib "user32" Alias "GetDesktopWindow" () As Long
Declare Function GetParent Lib "user32" Alias "GetParent" (ByVal hwnd As Long) As Long
Declare Function GetWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Declare Function GetWindowWord Lib "user32" Alias "GetWindowWord" (ByVal hwnd As Long, ByVal nIndex As Long) As Integer
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

It is now necessary to add a SUB to the module as indicated below. This is the SUB that actually clicks another form's buttons.

Sub Click (Btnhwnd As Integer)
    Dim ChildID As Integer
    Dim Nul As Integer
    Const GWW_ID = (-12)
    ChildID = GetWindowWord(Btnhwnd, GWW_ID)
    Nul = SendMessageByNum(GetParent(Btnhwnd), WM_COMMAND, ChildID, ByVal CLng(Btnhwnd))
End Sub

You now have the entire routine necessary to click a button on another form. However, you need to be able to tell the SUB which button to click. Therefore, we have added the routine below to locate a button that you all have and click it. Make sure your Visual Basic "Project" window is visible. This routine is only used to locate a button and should be modified to find other command buttons and windows.

The last step is to place a command button on your form. You may want to place the command button in upper right hand corner of the window and make the window just large enough to hold the button. The procedure will attempt to locate the "View Code" button in the Project Window. If it can find it, it will click it for you, causing the code window for the form to appear. Place the following code in the command button's CLICK event.

Sub Command1_Click ()
    On Error Resume Next
    Dim Btn As Integer, CurHwnd As Integer, T As String
    Dim Length As Integer, x As Integer, y As Integer
    'Trying to find the handle of the View Code Button so that
    'by clicking this program's button, we can see the code
    'window for this form.

    CurHwnd = GetDesktopWindow()            'Get Desktop handle
    CurHwnd = GetWindow(CurHwnd, GW_Child)  'Find Child Windows of Desktop
    Do
        If CurHwnd = 0 Then Exit Do         'No (more) matches found
        'Find out how long the text in this window is
        Length = GetWindowTextLength(CurHwnd)
        T = Space$(Length + 1)              'Allocate buffer space
        Length = GetWindowText(CurHwnd, T, Length + 1)
        If InStr(UCase$(T), "PROJECT") Then
            'The word "Project" was found in this Window's text
            'so this is likely VB's "Project" window

            CurHwnd = GetWindow(CurHwnd, GW_Child)
            'Looking now for the Project Window's child windows
            Do
                If CurHwnd = 0 Then Exit Sub 'No (more) matches found
                'Find out how long the text in this window is
                Length = GetWindowTextLength(CurHwnd)
                T = Space$(Length + 1)       'Allocate buffer space
                Length = GetWindowText(CurHwnd, T, Length + 1)
                If InStr(UCase$(T), "VIEW CODE") Then
                    'This is the handle we want
                    Click CurHwnd            'Click the View Code Button
                    Exit Sub                 'Exit the Sub
                End If
            CurHwnd = GetWindow(CurHwnd, GW_HwndNext) 'Keep looking
            Loop
        End If
        CurHwnd = GetWindow(CurHwnd, GW_HwndNext) 'Keep looking
    Loop
End Sub

Close ALL of your code windows before running this program. If you do not, you might not notice that it was successful. The final step is to run the program. Keep in mind that the sole objective of this event procedure is to locate the "View Code" Button in the Project window and pass its handle to the Click SUB in the module so that it can actually do the clicking for you.

Click Here!


Visual Basic Programming Zone is a website by Lorenzo Dalla Vecchia.
Webmaster's e-mail: webmaster@vbprogzone.cjb.net
Hosted by InWind: www.inwind.it