Public Const GWL_WNDPROC As Long = -4
Private Const CBN_DROPDOWN As Long = 7
Private Const WM_LBUTTONDOWN As Long = &H201
Private Const WM_KEYDOWN As Long = &H100
Private Const VK_F4 As Long = &H73
Private 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
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
Public Declare Function SetWindowLong _
Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Public Sub Unhook(hwnd As Long)
If defWinProc <> 0 Then
Call SetWindowLong(hwnd, _
GWL_WNDPROC, _
defWinProc)
defWinProc = 0
End If
End Sub
Public Sub Hook(hwnd As Long)
'Don't hook twice or you will
'be unable to unhook it.
If defWinProc = 0 Then
'set inital state of command buttons
Command1.Caption = "hook combo"
Command2.Caption = "unhook combo"
Command3.Caption = "unhook && end"
Command1.Enabled = True
Command2.Enabled = False
End Sub
Private Sub Command1_Click()
If defWinProc = 0 Then
Hook Combo1.hwnd
Command1.Enabled = False
Command2.Enabled = True
End If
End Sub
Private Sub Command2_Click()
'unhook the combo
If defWinProc <> 0 Then
Unhook Combo1.hwnd
defWinProc = 0
Command1.Enabled = True
Command2.Enabled = False
End If
End Sub
Private Sub Command3_Click()
Unload Me
End Sub
Private Sub Form_Unload(Cancel As Integer)
If defWinProc <> 0 Then Unhook Combo1.hwnd
End Sub
Private Sub List1_KeyDown(KeyCode As Integer, Shift As Integer)
'set flag to allow arrow and enter
'keys to simulate behaviour of normal
'combo
bKeepOpen = True
End Sub
Private Sub List1_KeyPress(KeyAscii As Integer)
'set flag to allow arrow and enter
'keys to simulate behaviour of normal
'combo
If KeyAscii = vbKeyReturn Then
'simulate selecting item with enter
bKeepOpen = False
Call ListView1_Click
Else
'alpha or arrow keys being used,
'so keep open
bKeepOpen = True
End If
End Sub
Private Sub ListView1_Click()
Dim itmx As ListItem
If ListView1.ListItems.Count > 0 Then
Set itmx = ListView1.SelectedItem
'For a style 0 combo, you can not assign
'to the Text property from within the click
'event, so the selected item must be 'added'
'as the only combo item, and selected using
'its listindex property.
'
'For a style 2 combo, the text property
'can't be set unless there is an exact
'match to a list item, so again we fake it
'by adding the selection to the combo and
'selecting it.
'
'Finally, since the tabs can't be used
'in the combo's edit window, as it doesn't
'support tabstops either, on selection we'll
'display the main listview item
With Combo1
.Clear
.AddItem itmx.Text
.ListIndex = 0
End With
End If
If bKeepOpen = False Then
ListView1.Visible = False
Combo1.SetFocus
End If