当前位置:K88软件开发文章中心编程语言.NET.NET01 → 文章内容

C#控制台下测试鼠标按键消息

减小字体 增大字体 作者:佚名  来源:翔宇亭IT乐园  发布时间:2018-12-31 11:55:27

:2010-09-14 17:57:00

本文用C#测试鼠标按键的程序,注意在使用时要引入System.Windows.Forms命名空间

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



C#控制台下测试鼠标按键消息