Csharp/C Sharp/Windows/Message

Материал из .Net Framework эксперт
Версия от 11:43, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

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";
    }
}


Removing an Installed Message Filter

using System;
using System.Windows.Forms;
   
public class BlockLeftMouseButtonMessageFilter : IMessageFilter
{
    const int WM_LBUTTONDOWN = 0x201;
    
    public bool PreFilterMessage(ref Message m) {
        if(m.Msg == WM_LBUTTONDOWN)
        {
            Console.WriteLine("The left mouse button is down.");
            Application.RemoveMessageFilter(this);
            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 Removal Test";
    }
}