Visual Basic Programming Code Examples
Visual Basic > Forms Code Examples
Clicking a button on a form through code
Clicking a button on a form through code
Below is a lot of code, which does very little! The code will clicks a button on a form by sending a message directly to that control. This has an advantage over SendKeys as it eliminates potential problems with user interaction.
Option Explicit
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindow32 Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) 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 SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
'Purpose : Clicks a button on a form
'Inputs : sFormCaption The form caption
' sButtonCaption The button caption
' [sFormClassName] The caption of the child window
'Outputs : Returns the handle to the child window or zero if no matching
' window was found
'Notes : Pass in either sRequiredClassName or sCaption or both
' The button must have it's own window handle (VBA command buttons DO NOT)
' Example useage is
' Call FormButtonClick("Form1","Command1")
'Revisions :
Function FormButtonClick(sFormCaption As String, sButtonCaption As String, Optional sFormClassName As String = vbNullString) As Boolean
Const WM_LBUTTONDOWN = &H201, WM_LBUTTONUP = &H202
Dim lHwndButton As Long, lHwndForm As Long
'Find the form
lHwndForm = FindWindow(sFormClassName, sFormCaption)
If lHwndForm Then
'Found form, find button
lHwndButton = DialogGetChildHwnd(lHwndForm, "", sButtonCaption)
If lHwndButton Then
'Bring the window to the foreground
Call SetForegroundWindow(lHwndForm)
FormButtonClick = CBool(SendMessage(lHwndButton, WM_LBUTTONDOWN, ByVal 0&, ByVal 0&) = 0)
FormButtonClick = CBool(SendMessage(lHwndButton, WM_LBUTTONUP, ByVal 0&, ByVal 0&) = 0)
End If
End If
End Function
'Purpose : Returns the handle of a child window within a window
'Inputs : lhwndParent The handle of the parent window
' [sRequiredClassName] The class name of the child window
' [sCaption] The caption of the child window
'Outputs : Returns the handle to the child window or zero if no matching
' window was found
'Notes : Pass in either sRequiredClassName or sCaption or both
'Revisions :
Function DialogGetChildHwnd(lhwndParent As Long, Optional sRequiredClassName As String, Optional sCaption As String) As Long
Dim lLenClassName As Long
Dim sClassName As String
Dim lhWndNext As Long
Const GW_CHILD As Integer = 5, GW_HWNDFIRST As Integer = 0, GW_HWNDNEXT As Integer = 2
On Error Resume Next
'Get a handle to a child window
lhWndNext = GetWindow32(lhwndParent, GW_CHILD)
'Get first child window
lhWndNext = GetWindow32(lhWndNext, GW_HWNDFIRST)
'Find the name of first child
sClassName = DialogClassName(lhWndNext)
If sClassName = sRequiredClassName Or Len(sRequiredClassName) = 0 Then
'Class names match
If Len(sCaption) Then
If DialogGetText(lhWndNext) = sCaption Then
'Found correct child window
DialogGetChildHwnd = lhWndNext
lhWndNext = 0
End If
Else
'Found correct child window
DialogGetChildHwnd = lhWndNext
lhWndNext = 0
End If
End If
Do While lhWndNext <> 0
'Search all children
lhWndNext = GetWindow32(lhWndNext, GW_HWNDNEXT)
sClassName = DialogClassName(lhWndNext)
If sClassName = sRequiredClassName Then
If Len(sCaption) Then
If DialogGetText(lhWndNext) = sCaption Then
'Found correct child window
DialogGetChildHwnd = lhWndNext
Exit Do
End If
Else
'Found correct child window
DialogGetChildHwnd = lhWndNext
Exit Do
End If
End If
Loop
End Function
'Purpose : Returns the class name specified window
'Inputs : lHwnd The handle of the window
'Outputs : Returns the window text of a specified window
'Notes :
'Revisions :
Function DialogClassName(lHwnd As Long) As String
Const clLenName As Long = 100
Dim sResult As String * clLenName, lRetVal As Long
lRetVal = GetClassName(lHwnd, sResult, clLenName)
DialogClassName = Left$(sResult, lRetVal)
End Function
'Purpose : Returns the window text of a specified window
'Inputs : lHwnd The handle of the window
'Outputs : Returns the window text of a specified window
'Notes :
'Revisions :
Function DialogGetText(lHwnd As Long) As String
Const clStrLen As Long = 1000
Static ResString As String * clStrLen, lRet As Long
lRet = GetWindowText(lHwnd, ResString, clStrLen)
DialogGetText = Left$(ResString, lRet)
End Function