With Combo1
.AddItem "Item A"
.AddItem "Item B"
.AddItem "Item C"
End With
'enable button to hook
Command1(1).Enabled = True
Command1(2).Enabled = False
Combo1.Text = "edit is Unhooked"
End Sub
Private Sub Form_Unload(Cancel As Integer)
'just in case, call the unhook method
Call UnHook(hwndEdit)
End Sub
Private Sub Command1_Click(Index As Integer)
Select Case Index
Case 1:
'get the handle to the edit portion
'of the combo control
hwndEdit = FindWindowEx(Combo1.hwnd, 0&, vbNullString, vbNullString)
If hwndEdit <> 0 Then
'show that the handle was obtained and hook
Combo1.Text = "Hooked: edit handle is " & CStr(hwndEdit)
Call Hook(hwndEdit)
'disable button so can't hook again
Command1(1).Enabled = False
Command1(2).Enabled = True
End If
Case 2:
If hwndEdit <> 0 Then
Call UnHook(hwndEdit)
Combo1.Text = "edit is Unhooked"
'enable button to hook
Command1(1).Enabled = True
Command1(2).Enabled = False
End If
Case 3: Unload Me
End Select
End Sub
=============================================
Option Explicit
Public Const GWL_WNDPROC As Long = -4
Public Const WM_CONTEXTMENU As Long = &H7B
Public defWndProc As Long
'used to hold the handle to the combo's edit window
Public hwndEdit As Long
Public Declare Function CallWindowProc Lib "user32" _
Alias "CallWindowProcA" _
(ByVal lpPrevWndFunc As Long, _
ByVal hwnd As Long, _
ByVal Msg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
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 FindWindowEx Lib "user32" _
Alias "FindWindowExA" _
(ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, _
ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long
Public Sub Hook(hwnd As Long)
If defWndProc = 0 Then
defWndProc = SetWindowLong(hwnd, _
GWL_WNDPROC, _
AddressOf WindowProc)
End If
Public Function WindowProc(ByVal hwnd As Long, _
ByVal uMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
'only if the handle is that of the
'edit control in the Combo...
If hwnd = hwndEdit Then
'watch for the context menu message
Select Case uMsg
Case WM_CONTEXTMENU
'eat the message -- no popup menu appears
WindowProc = 0
'alternatively, comment out the above, and
'uncomment below to show your own menu (make
'sure the correct menu name is specified)
' Form1.PopupMenu Form1.mnuPopup
' WindowProc = 0
Case Else
'not the message of interest,
'so process normally
WindowProc = CallWindowProc(defWndProc, _
hwnd, _
uMsg, _
wParam, _
lParam)
End Select