Hello,

I am trying to get my head around subclassing at the moment using SetWindowLong and CallWindowProc. So far it is a disaster. I am just trying to make a simple example.

VB Code:
  1. Option Explicit
  2.  
  3. Private Declare Function CallWindowProc _
  4.     Lib "user32" _
  5.     Alias "CallWindowProcA" ( _
  6.         ByVal lpPrevWndFunc As Long, _
  7.         ByVal hWnd As Long, _
  8.         ByVal Msg As Long, _
  9.         ByVal wParam As Long, _
  10.         ByVal lParam As Long) _
  11.         As Long
  12.        
  13. Private Declare Function SetWindowLong _
  14.     Lib "user32" _
  15.     Alias "SetWindowLongA" ( _
  16.         ByVal hWnd As Long, _
  17.         ByVal nIndex As Long, _
  18.         ByVal dwNewLong As Long) _
  19.         As Long
  20.  
  21. Private Const GWL_WNDPROC = (-4)
  22.  
  23. Private Const WM_MOUSEMOVE = &H200
  24.  
  25. Private OldWindowProc       As Long
  26.  
  27. Public Sub SubClass(frm As Form)
  28.  
  29.     OldWindowProc = SetWindowLong(frm.hWnd, GWL_WNDPROC, AddressOf NewWindowProc)
  30.  
  31. End Sub
  32.  
  33. Public Sub UnSubClass(frm As Form)
  34.  
  35.     Call SetWindowLong(frm.hWnd, GWL_WNDPROC, OldWindowProc)
  36.    
  37. End Sub
  38.  
  39. Public Function NewWindowProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, lParam As Long) As Long
  40.    
  41.     If Msg = WM_MOUSEMOVE Then
  42.        
  43.         Form1.Caption = "Mouse is moving..."
  44.    
  45.     End If
  46.    
  47.     NewWindowProc = CallWindowProc(OldWindowProc, hWnd, Msg, wParam, lParam)
  48.    
  49. End Function

The idea behind it is that i am trying to process the WM_MOUSEMOVE message once the mouse moves on the form, then it will display a message in the form caption to say that the mouse is being moved.

The best that it does at the moment is force VB to close and i get a GPF.

Any ideas on how i can make this work?