How To Implement IMessageFilter

G

Guest

Hi,

I recently had a post about how to block Mousewheel events. The answer was
to implement an IMessageFilter. Sadly, I must report that after 24 hours of
researching my library and online resources I am still stumped! I did find a
promising code snippet on MSDN, but have yet to successfully implement it.

Here is the code snippet:

Public Class MessageFilter

Public Class TestMessageFilter
Implements IMessageFilter

Public Function PreFilterMessage(ByRef m As _
System.Windows.Forms.Message) _
As Boolean Implements IMessageFilter.PreFilterMessage
'Blocks all the messages relating to the left mouse button.
If ((m.Msg >= 513) And (m.Msg <= 515)) Then
Console.WriteLine("Process messages: " & m.Msg)
Return True
End If
Return False
End Function
End Class

End Class

I have three basic questions about this code. 1.) How do I specify the
parameter ‘m’ when I call the PreFilterMessage function? 2.) Where in my
code do I insert the PreFilterMessage function? 3.) Where to the constants
513 and 515 in the ‘If’ statement come from and how must I change this to
block messages relating to the Mousewheel?

Thanks for your assistance and patience.
 
M

m.posseth

With a messagefilter you can catch / cancel all events sent to the window
and its controls ( equivalant of subclassing a form in VB6 )

the problem is you need to find the right event code for the message you
want to cancel

here is an example http://www.freevbcode.com/ShowCode.Asp?ID=5635 where it
is jused to dissable the webrowser control`s accelerat
or keys


regards

Michel Posseth [MCP]
 
C

Claes Bergefall

1. You don't call PreFilterMessage, the framework does. You will need to
tell the framework that you have implemented it though. You do this by
calling Application.AddMessageFilter

2. You can implement IMessageFilter in any class you want. I usually do it
in the MainForm and then call AddMessageFilter(Me) from the Load event
handler.

3. The constants can be found in one of the header files (.h) found in the
folder C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\PlatformSDK\Include. You will need to install the C++ part of
Visual Studio to have that directory though. If you can't do that you can
instead install the PlatformSDK which you can download from
<http://www.microsoft.com/downloads/details.aspx?familyid=EBA0128F-A770-45F1
-86F3-7AB010B398A3&displaylang=en>. The constant you're interested in can be
found in winuser.h and is defined like this:

Private Const WM_MOUSEWHEEL As Integer = &H20A
 
G

Guest

This is a group thanks for your responses. The IMessageFilter is now working
in my application. The application creates, edits and deletes records in a
database. The user was complaining that occasionally data in some records was
changing. The only data fields with which they had difficulty had comboBoxes
as their sources. The users swore that they never touched the mousewheel so
it was my problem. I, of course, blamed Bill Gates.

The Enter and Leave events for the comboBoxes now have an AddMessageFilter
and RemoveMessageFilter statements. If this doesn't resolve the problem I'll
know that it really is Bill Gates' fault.

Thanks again,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top