This documentation is archived and is not being maintained.

Application.AddMessageFilter Method

Adds a message filter to monitor Windows messages as they are routed to their destinations.

[Visual Basic]
Public Shared Sub AddMessageFilter( _
   ByVal value As IMessageFilter _
)
[C#]
public static void AddMessageFilter(
 IMessageFilter value
);
[C++]
public: static void AddMessageFilter(
 IMessageFilter* value
);
[JScript]
public static function AddMessageFilter(
   value : IMessageFilter
);

Parameters

value
The implementation of the IMessageFilter interface you want to install.

Remarks

Use a message filter to prevent specific events from being raised or to perform special operations for an event before it is passed to an event handler. Message filters are unique to a specific thread.

To prevent a message from being dispatched, the value parameter instance that you pass to this method must override the PreFilterMessage method with the code to handle the message. The method must return false.

CAUTION   Adding message filters to the message pump for an application can degrade performance.

Example

[Visual Basic, C#, C++] Before you can use a message filter, you must provide an implementation for the IMessageFilter interface. The following class creates a message filter called TestMessageFilter. This filter blocks all messages relating to the left mouse button.

[Visual Basic] 
' Creates a message filter.
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("Processing the messages : " & m.Msg)
         Return True
      End If
      Return False
   End Function
End Class


[C#] 
// Creates a  message filter.
 public class TestMessageFilter : IMessageFilter {
    public bool PreFilterMessage(ref Message m) {
       // Blocks all the messages relating to the left mouse button.
       if (m.Msg >= 513 && m.Msg <= 515) {
          Console.WriteLine("Processing the messages : " + m.Msg);
          return true;
       }
       return false;
    }
  }


[C++] 
// Creates a  message filter.
public:
__gc class TestMessageFilter : public IMessageFilter {
public:
   bool PreFilterMessage(Message* m) {
      // Blocks all the messages relating to the left mouse button.
      if (m->Msg >= 513 && m->Msg <= 515) {
         Console::WriteLine(S"Processing the messages : {0}", __box(m->Msg));
         return true;
      }
      return false;
   }
};

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

Application Class | Application Members | System.Windows.Forms Namespace

Show: