This code, which was not written by me, will prevent a command button from receiving the focus (The box with dashed lines on command button).
VB Code:
'In a Module
Option Explicit
Public Declare Function SetWindowLong Lib "user32" _
Alias "SetWindowLongA" _
(ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Public Declare Function CallWindowProc Lib "user32" _
Alias "CallWindowProcA" _
(ByVal lpPrevWndFunc As Long, _
ByVal hwnd As Long, _
ByVal uMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Public Const GWL_WNDPROC As Long = (-4)
Private WndProcOrig As Long
Public Function BtnWndProc(ByVal hwndBtn As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
If wMsg = 7 Then
BtnWndProc = 0
Exit Function
Else
BtnWndProc = CallWindowProc(WndProcOrig, hwndBtn, wMsg, wParam, lParam)
End If
End Function
Public Sub SubClassBtn(ByVal hwndBtn As Long)
WndProcOrig = SetWindowLong(ByVal hwndBtn, GWL_WNDPROC, AddressOf BtnWndProc)
End Sub
Public Sub UnSubclassBtn(ByVal hwndBtn As Long)
Call SetWindowLong(hwndBtn, GWL_WNDPROC, WndProcOrig)
WndProcOrig = 0
End Sub
and then you call it like this:
VB Code:
'On the form
Option Explicit
Private Sub Form_Load()
Dim a As Long
With Command1
For a = .LBound To .UBound
SubClassBtn .Item(a).hwnd
Next a
End With
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim a As Long
With Command1
For a = .LBound To .UBound
UnSubclassBtn .Item(a).hwnd
Next a
End With
End Sub