Preprocess Win32 Messages through Windows Forms

Published 20 October 04 04:59 PM

In the unmanaged world, it was quite common to intercept Win32 messages as they were plucked off the message queue. In that rare case in which you wish to do so from a managed Windows Forms application, your first step is to build a helper class which implements the IMessageFilter interface. The sole method, PreFilterMessage(), allows you to get at the underlying message ID, as well as the raw WPARAM and LPARAM data. By way of a simple example:

public class MyMessageFilter : IMessageFilter 
{
  public bool PreFilterMessage(ref Message m) 
  {
    // Intercept the left mouse button down message.
    if (m.Msg == 513) 
    {
      MessageBox.Show("WM_LBUTTONDOWN is: " + m.Msg);
      return true;
    }
    return false;
  }
}

At this point you must register your helper class with the Application type:

public class mainForm : System.Windows.Forms.Form
{
  private MyMessageFilter msgFliter = new MyMessageFilter();

  public mainForm()
  {
    // Register message filter.
    Application.AddMessageFilter(msgFliter);		
  }
…
}

At this point, your custom filter will be automatically consulted before the message makes its way to the registered event hander. Removing the filter can be accomplished using the (aptly named) static Application.RemoveMessageFilter() method.


Tip from Andrew Troelsen
Posted by: Duncan Mackenzie, MSDN
This post applies to Visual C# .NET 2002/2003

Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Geert Baeyaert said on October 22, 2004 4:01 AM:
Extremely usefull indeed.
However, I've noticed that this doesn't work for WM_NOTIFY messages.

Can anyone explain why this is the case?
# 厚重之刀 said on November 3, 2004 4:02 AM:
Very Helpful!
# RebelGeekz said on December 28, 2004 4:54 AM:
[http://itpeixun.51.net/][http://aissl.51.net/][http://kukuxz003.freewebpage.org/][http://kukuxz001.51.net/][http://kukuxz003.51.net/][http://kukuxz005.51.net/][http://kukuxz002.51.net/][http://kukuxz004.freewebpage.org/][http://kukuxz007.51.net/][http://kukuxz001.freewebpage.org/][http://kukuxz006.51.net/][http://kukuxz002.freewebpage.org/][http://kukuxz004.51.net/][http://kukuxz008.51.net/][http://kukuxz009.51.net/][http://kukuxz005.freewebpage.org/][http://kukuxz006.freewebpage.org/][http://kukuxz007.freewebpage.org/][http://kukuxz009.freewebpage.org/]
# FAQ C# said on April 14, 2005 9:21 AM:
# FAQ C# said on April 14, 2005 9:23 AM:
# FAQ C# (par Yannick Lejeune) said on August 23, 2005 5:32 AM:
# Preprocess Win32 Messages through Windows Forms « Alan Kleymeyer’s WordPress Blog said on September 1, 2006 10:24 AM:
PingBack from http://kleymeyer.wordpress.com/2006/09/01/preprocess-win32-messages-through-windows-forms/
# PeekMessage en .NET | hilpers said on January 18, 2009 6:38 AM:

PingBack from http://www.hilpers-esp.com/651589-peekmessage-en-net

# C Frequently Asked Questions Preprocess Win32 Messages through | Paid Surveys said on May 29, 2009 12:49 PM:

PingBack from http://paidsurveyshub.info/story.php?title=c-frequently-asked-questions-preprocess-win32-messages-through

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

This Blog

Syndication

Page view tracker