'In a Module..
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long 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 Private Const GWL_WNDPROC = (-4) Private Const WM_CTLCOLORLISTBOX = &H134 Private Const LB_GETCURSEL = &H188 Private lPrevWndFunc As Long Public oLabel As Label Private Function WindowProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Dim iIndex As Long If Msg = WM_CTLCOLORLISTBOX Then iIndex = SendMessage(lParam, LB_GETCURSEL, 0&, ByVal 0&) If iIndex >= 0 Then oLabel = "Item Index: " & iIndex End If WindowProc = CallWindowProc(lPrevWndFunc, hwnd, Msg, wParam, lParam) End Function Public Sub SubClassIt(ByVal hwnd As Long, ByVal Add As Boolean) If Add Then lPrevWndFunc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindowProc) Else Call SetWindowLong(hwnd, GWL_WNDPROC, lPrevWndFunc) End If End Sub 'In a Form, with a Label and a ComboBox.. Private Sub Form_Load() Dim iIndex As Long Set oLabel = Label1 SubClassIt Combo1.hwnd, True For iIndex = 1 To 10 Combo1.AddItem "Item " & iIndex Next End Sub Private Sub Form_Unload(Cancel As Integer) SubClassIt Combo1.hwnd, False End Sub |