Question Overriding WndProc & Message Constants

StoneCodeMonkey

Well-known member
Joined
Apr 17, 2009
Messages
56
Programming Experience
5-10
Any idea where Windows Message constants can be found in the framework?

Example: msg=0x2 (WM_DESTROY) hwnd=0x24038c wparam=0x0 lparam=0x0 result=0x0

I captured that with ...

VB.NET:
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        MyBase.WndProc(m)

        If Not intList.Contains(m.Msg) Then
            intList.Add(m.Msg)
            Debug.Print(m.ToString)
        End If

    End Sub

intList is just a List(Of Integer), keeps me from logging the same message type over and over.

Not all of the messages return a (xxxx) constant from the ToString method, but most do. Just curious. Also, I'm using VB2010 Express, Framework 4.0

Thanks,
 
Last edited:
They are not part of .Net Framework, I use this application to find such declarations: ActiveVB - ApiViewer (en)
The alternative is to search for them on web, or find the relevant C++ header file as documented in MSDN libary for relevant messages and define them yourself in VB, but I think you need Windows SDK installed for this.
 
Every .NET developer needs to have .NET Reflector in their toolkit. I'd never considered your question before but, as you say, Message.ToString displays the constant name so they must be in there somewhere. I fired up Reflector, navigated to the System.Windows.Forms.Message class and selected the ToString method. I then just followed the links until I found what you're looking for. You should do the same. The bad news is that its not available outside the System.Windows.Forms.dll assembly, but at least you can use Reflector to get the appropriate values to declare your own constants as needed.
 
Oh, you were looking for message names for unknown message numbers? That will prove difficult. Usually it is the other way around, sending a specific message or listening for a specific message.
There are for example many occations of constants that are defined by adding to a base message like these:
Const WM_DDE_ACK As Int32 = (WM_DDE_FIRST + 4)
Const WM_CAP_SINGLE_FRAME_OPEN As Int32 = (WM_CAP_START+ 70)
Const WM_ADSPROP_NOTIFY_SETFOCUS As Int32 = (WM_USER + 1105)
So if you get unidentified message '2129' you will have a hard time figure out what that is. Even if you translate to a &H or 0x hex value and search web that will not match anything since it is not defined that way.
 
Very true. My new laptop will be ready next week and I can get .Net Reflector installed again. Have been working from the company laptop for several weeks and they are picky about what software gets installed.
 
Oh, you were looking for message names for unknown message numbers? That will prove difficult. Usually it is the other way around, sending a specific message or listening for a specific message.
There are for example many occations of constants that are defined by adding to a base message like these:

So if you get unidentified message '2129' you will have a hard time figure out what that is. Even if you translate to a &H or 0x hex value and search web that will not match anything since it is not defined that way.

Kind of. This all really started with me trying to disable smooth scrolling in a RTB. Which got me to WndProc where I could catch WM_MOUSEWHEEL. That's where I realized that the ToString shows the constant name, which means they are defined somewhere within the framework.
 
That's where I realized that the ToString shows the constant name, which means they are defined somewhere within the framework.
Not really. As you will see in Reflector, it's just a long Select Case (strictly speaking, long 'switch' as it's written in C#) statement that returns the appropriate string for each number.
 
Not really. As you will see in Reflector, it's just a long Select Case (strictly speaking, long 'switch' as it's written in C#) statement that returns the appropriate string for each number.

That's horrible. I guess the MS programmers had time on their hands. You would think they would have at least put it in a enum.

Thanks everyone.
 
That's horrible. I guess the MS programmers had time on their hands. You would think they would have at least put it in a enum.
.Net Framework is a managed development platform, and while it provides a lot of managed implementations (wrappers) for common Windows API functionality, it hides in all events the actual interoperability with the unmanaged API. There is no reason for .Net to provide or expose any of those declarations, though I think it would not be a bad idea for a third party library to give a .Net developer easy direct access to those methods, types and constants (as enums). But, as said, it is outside the scope of .Net Framework. Using the various APIs is also often more than making a simple call with correct parameters, there may be a combination of many calls that is necesary both to start, continue, and end an operation, and this is considered when .Net expose the managed interface to such functionality.
 
Back
Top