[C#]C#实现鼠标消息捕获

在C#中怎样禁用鼠标按键,我们可以通过ImessageFilter接口下的PreFilterMessage方法、Application类的AddMessageFilter方法,RemoveMessageFilter方法和Message结构的Msg属性来禁用鼠标左键。Message结构包装Windows发送的消息,可使用该结构包装消息,并将其分配给窗口过程以进行调度,还可以使用该结构获取系统向应用程序或控件发送的关于某个消息的信息。

使用PreFilterMessage方法在调度消息之前将其筛选出来。语法格式如下: 

Bool PreFilterMessage(ref Message m)

参数说明:

  • m:要调度的消息,无法修改此消息。
  • 返回值:如果筛选消息并禁止消息被调度,则为True;如果允许消息继续到达下一个筛选器或控件,则为False。使用AddMessageFilter方法添加消息筛选器以便在向目标传送Windows消息时监视这些消息。使RemoveMessageFilter 从应用程序的消息泵移除一个消息筛选器。

示例一:在ComboBox选择值的时候,选择的值会随鼠标滚轮的滑动而改变,有时候不小心滑动了滑轮,导致选择的值改变,在下面的示例中,通过禁用鼠标滚轮,防止鼠标滚轮的滑动改变ComboBox选择的值。

界面:

代码实现:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MouseDemo
{
    public partial class FrmMain : Form,IMessageFilter
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        public bool PreFilterMessage(ref Message m)
        {
            if (m.Msg == 522)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmMain_Load(object sender, EventArgs e)
        {
            InitComboBox();
        }

        /// <summary>
        /// 初始化ComboBox
        /// </summary>
        private void InitComboBox()
        {
            Dictionary<int, string> dictGrade = new Dictionary<int, string>();
            dictGrade.Add(1, "一年级");
            dictGrade.Add(2, "二年级");
            dictGrade.Add(3, "三年级");
            dictGrade.Add(4, "四年级");
            dictGrade.Add(5, "五年级");
            dictGrade.Add(6, "六年级");

            BindingSource dataSource = new BindingSource();
            dataSource.DataSource = dictGrade;
            cmb_Grade.DataSource = dataSource;
            cmb_Grade.DisplayMember = "Value";
            cmb_Grade.ValueMember = "Key";
        }

        /// <summary>
        /// 索引改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmb_Grade_SelectedIndexChanged(object sender, EventArgs e)
        {
              //添加消息过滤
            Application.AddMessageFilter(this);
        }


    }
}

Frm_Main.Designer.cs

namespace _193
{
    partial class Frm_Main
    {
        /// <summary>
        ///  Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        ///  Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #region Windows Form Designer generated code
 
        /// <summary>
        ///  Required method for Designer support - do not modify
        ///  the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            SuspendLayout();
            // 
            // Frm_Main
            // 
            AutoScaleDimensions = new SizeF(7F, 17F);
            AutoScaleMode = AutoScaleMode.Font;
            BackgroundImage = Properties.Resources.C_编程词典;
            BackgroundImageLayout = ImageLayout.Stretch;
            ClientSize = new Size(367, 238);
            Name = "Frm_Main";
            Text = "软件主界面";
            Load += Frm_Main_Load;
            ResumeLayout(false);
        }
 
        #endregion
    }
}

Frm_Main.cs

namespace _193
{
    public partial class Frm_Main : Form
    {
        public Frm_Main()
        {
            InitializeComponent();
        }
 
        private void Frm_Main_Load(object sender, EventArgs e)
        {
            Frm_Start frm = new();
            frm.ShowDialog();
        }
    }
}

Frm_Start.Designer.cs

namespace _193
{
    partial class Frm_Start
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #region Windows Form Designer generated code
 
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
            timer1 = new System.Windows.Forms.Timer(components);
            SuspendLayout();
            // 
            // timer1
            // 
            timer1.Tick += Timer1_Tick;
            // 
            // Frm_Start
            // 
            AutoScaleDimensions = new SizeF(7F, 17F);
            AutoScaleMode = AutoScaleMode.Font;
            BackgroundImage = Properties.Resources.start;
            BackgroundImageLayout = ImageLayout.Stretch;
            ClientSize = new Size(368, 240);
            Name = "Frm_Start";
            Text = "启动界面";
            FormClosed += Frm_Start_FormClosed;
            Load += Frm_Start_Load;
            ResumeLayout(false);
        }
 
        #endregion
 
        private System.Windows.Forms.Timer timer1;
    }
}

Frm_Start.Designer.cs

namespace _193
{
    partial class Frm_Start
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #region Windows Form Designer generated code
 
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
            timer1 = new System.Windows.Forms.Timer(components);
            SuspendLayout();
            // 
            // timer1
            // 
            timer1.Tick += Timer1_Tick;
            // 
            // Frm_Start
            // 
            AutoScaleDimensions = new SizeF(7F, 17F);
            AutoScaleMode = AutoScaleMode.Font;
            BackgroundImage = Properties.Resources.start;
            BackgroundImageLayout = ImageLayout.Stretch;
            ClientSize = new Size(368, 240);
            Name = "Frm_Start";
            Text = "启动界面";
            FormClosed += Frm_Start_FormClosed;
            Load += Frm_Start_Load;
            ResumeLayout(false);
        }
 
        #endregion
 
        private System.Windows.Forms.Timer timer1;
    }
}

Frm_Start.cs

namespace _193
{
    public partial class Frm_Start : Form
    {
        public Frm_Start()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 启动计时器,10s计时
        /// </summary>
        private void Frm_Start_Load(object sender, EventArgs e)
        {
            timer1.Start();
            timer1.Interval = 10000;
        }
        /// <summary>
        /// 关闭启动窗体
        /// </summary>
        private void Timer1_Tick(object sender, EventArgs e)
        {
            Close();
        }
        /// <summary>
        /// 关闭计时器
        /// </summary>
        private void Frm_Start_FormClosed(object sender, FormClosedEventArgs e)
        {
            timer1.Stop();
        }
    }
}

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FL1768317420

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值