SIGN UP MEMBER LOGIN:    
ARTICLE

How To Pre-Filter Windows Messages

Posted by Chris Blake Articles | Windows Forms C# November 20, 2002
Sometimes it can be useful to pre-filter messages sent to your program.
Reader Level:

Sometimes it can be useful to pre-filter messages sent to your program, there are essentially two steps to do this.

  1. Create your message filter object.
  2. Add the filter to your application.

Step 1: Create your message filter

To make an actual message filter you will need to inherit from the IMessageFilter interface, this is a pretty simple interface. So simple you only have one method to implement, below is the prototype:

public bool PreFilterMessage(ref Message m)

So below is
the class you will need to create:

public class MyMessageFilter : IMessageFilter
{
public bool PreFilterMessage(ref Message m)
{
// the filtering goes here
}
}

Basically all the message sent to your application will come through here, and if you detect your "special message" you can prefilter it (ie by returning true from this method) and do the processing here. E.G:

public class MyMessageFilter : IMessageFilter
{
public bool PreFilterMessage(ref Message m)
{
// 0x100 is the id for the message that is sent when a key is pressed down.
if (m.Msg == 0x100)
{
return true;
}
return false;
}
}

This class will now filter out all keydown messages. Now we need to associate an instance of this class with your application.

Step 2: Add the filter to your application

This is the real simple bit (can it get simpler you ask!).. well it takes only 1 line of code.. and no, it doesnt spread across three pages! To have the message filter working from start to finish you should place this line in the entry point of your application the Main function. And also place it before the line "Application.Run( new Form1() );" Below is the line that you need to add:

Application.AddMessageFilter( new MyMessageFilter() );

And there you have it! Try placing a MessageBox.Show("Key Pressed Down"); in the if statement block.

NOTE: I have on occasion found the odd message slipping through! If you wish to create your own message yous the static method Message.Create() - I am still working on how to send messages.

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor