Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleSubclass a control to read Windows messages
Description
Keywordssubclass, WindowProc, messages
CategoriesTips and Tricks, API
 
Subclass and display information about messages in the Debug window.

Subroutine LoadMsgNames loads a bunch of message names into a collection. Function MsgName looks up a message's name. The new WindowProc uses these routines to display information about the messages it processes.

 
Public MsgNames As New Collection

Public Sub LoadMsgNames()
Static done_before As Boolean

    If done_before Then Exit Sub
    done_before = True
    
    MsgNames.Add "WM_NULL", "0"
    MsgNames.Add "WM_CREATE", "1"
    MsgNames.Add "WM_DESTROY", "2"
    ' LOTS of code omitted...

End Sub

Public Function MsgName(ByVal num As Long) As String
    On Error Resume Next
    MsgName = MsgNames(Hex$(num))
    If Err.Number = 0 Then Exit Function

    If num >= 0 And num < WM_USER Then
        MsgName = "Range 1 message reserved for Windows"
    ElseIf num >= WM_USER And num <= &H7FFF Then
        MsgName = "Reserved for private window classes"
    ElseIf num >= &H8000 And num <= &HBFFF Then
        MsgName = "Range 3 message reserved for Windows"
    ElseIf num >= &H8000 And num <= &HBFFF Then
        MsgName = "String message for use by applications"
    Else
        MsgName = "Unknown message" & Str$(num)
    End If
End Function

' Display message names.
Public Function NewWindowProc(ByVal hwnd As Long, ByVal msg _
    As Long, ByVal wParam As Long, ByVal lParam As Long) As _
    Long
Const WM_NCDESTROY = &H82

    ' Load the message names.
    LoadMsgNames

    ' Display the message's name.
    Debug.Print Hex$(msg) & ": " & MsgName(msg)

    ' If we're being destroyed,
    ' restore the original WindowProc.
    If msg = WM_NCDESTROY Then
        SetWindowLong _
            hwnd, GWL_WNDPROC, _
            OldWindowProc
    End If

    NewWindowProc = CallWindowProc( _
        OldWindowProc, hwnd, msg, wParam, _
        lParam)
End Function
 
For more information on subclassing including important precautions, see Tutorial: Subclassing.

My book Custom Controls Library shows how to use subclassing to create several advanced ActiveX controls.

 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated