Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 32,093 times

Contents

Related Categories

Subclassing - Catching those messages!

Catching those messages!

To catch any messages sent to a form by windows, you need to tell Windows what procedure to send the message to. You do this using the SetWindowLong api call. Use the following code to start catching your messages:

Module Code

'// variable that stores the previous message handler
Public ProcOld As Long
Public Const GWL_WNDPROC = (-4)
'// Windows API Call for catching messages
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
'// Windows API call for calling window procedures
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 Function WindowProc(ByVal hWnd As Long, ByVal iMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
    '// ----WARNING----
    '// do not attempt to debug this procedure!!
    '// ----WARNING----

    '// this is our implementation of the message handling routine
    '// determine which message was recieved
    Select Case iMsg
    Case else
        '// Ignore all messages for the moment
    End Select
    '// pass all messages on to VB and then return the value to windows
    WindowProc = CallWindowProc(procOld, hWnd, iMsg, wParam, lParam)
End Function

Form Code

'// form_load event. Catch all those messages!
Private Sub Form_Load()
    On Error Resume Next
    '// saves the previous window message handler. Always restore this value
    '// AddressOf command sends the address of the WindowProc procedure
    '// to windows
    ProcOld = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub

'// form_queryunload event. Return control to windows/vb
Private Sub Form_Unload(Cancel As Integer)
    '// give message processing control back to VB
    '// if you don'//t do this you WILL crash!!!
    Call SetWindowLong(hWnd, GWL_WNDPROC, procOld)
End Sub

Basically, when the Form loads, the SetWindowLong API is called. This tells windows to send any messages to the WindowProc procedure in the module, rather than the procedure VB uses. This API returns a handle to the old procedure... we need to save this so that we can restore it when our program ends. If we don't, everything will crash! Now, all messages for the specified hWnd (which was passed when we called SetWindowLong) will be sent to WindowProc. Please note that there will be a lot of messages (ie thousands)... this is why you cannot debug the WindowProc procedure... the VB debugging tools cannot cope with this quantity of messages being sent, even when the program is paused. Instead, you need to turn of Background Compile (Tools|Options|General). This should catch most errors before your program is run. If it doesn't, VB will crash when an error occurs... you need to be very careful!

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • GetLowWord() and GetHighWord()

    Posted by HyperHacker on 25 Feb 2004

    Pretty hacky functions... Try these:

    [code]Public Function GetLowWord(Word As Long) as Long
    GetLowWord = Word Mod 65536
    End Function

    Public Function GetHighWord(Word As Long)
    GetHighWo...

  • Mr. Crowley copies source code!

    Posted by alpine on 08 Aug 2003

    Hummmm..... This code looks surprisingly like the code in the GETMINMAXINFO example at http://www.mvps.org/vbvision/ Right down to the *exact* same comments! Coincidence? You be the judge!

  • Posted by James Crowley on 07 Mar 2003

    http://www.vbaccelerator.com/home/VB/Code/Libraries/Subclassing/SSubTimer/article.asp

  • link not working

    Posted by gautam on 07 Mar 2003

    the link [b]Download the SSubTmr project code (no DLL) (9kb) [/b] not working.