Installing a Message Filter : Application Event « Development Class « C# / C Sharp






Installing a Message Filter

 


using System;
using System.Windows.Forms;
   
public class BlockLeftMouseButtonMessageFilter : IMessageFilter
{
    const int WM_LBUTTONDOWN = 0x201;
    const int WM_LBUTTONUP = 0x202;
    
    public bool PreFilterMessage(ref Message m)
    {
        if(m.Msg == WM_LBUTTONDOWN)
        {
            Console.WriteLine("The left mouse button is down.");
            return true;
        }
        if(m.Msg == WM_LBUTTONUP)
        {
            Console.WriteLine("The left mouse button is up.");
            return true;
        }
        return false;
    }
}
   
public class MainForm : Form
{
    public static void Main()
    {
        MainForm MyForm = new MainForm();
        BlockLeftMouseButtonMessageFilter MsgFilter = new BlockLeftMouseButtonMessageFilter();
   
        Application.AddMessageFilter(MsgFilter);
        Application.Run(MyForm);
    }
   
    public MainForm()
    {
        Text = "Message Filter Test";
    }
}

 








Related examples in the same category

1.Application.Run
2.Handling Application Events: OnIdle, OnThreadException, OnThreadExit
3.Handling Application Events: On Application ExitHandling Application Events: On Application Exit
4.Handling Application Events: On IdleHandling Application Events: On Idle
5.Handling Application Events: Thread ExceptionHandling Application Events: Thread Exception
6.Handling Application Events: On Thread ExitHandling Application Events: On Thread Exit
7.Removing an Installed Message Filter