In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
小编给大家分享一下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; } } /// /// 窗体加载 /// /// /// private void FrmMain_Load(object sender, EventArgs e) { InitComboBox(); } /// /// 初始化ComboBox /// private void InitComboBox() { Dictionary dictGrade = new Dictionary(); 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"; } /// /// 索引改变事件 /// /// /// private void cmb_Grade_SelectedIndexChanged(object sender, EventArgs e) { //添加消息过滤 Application.AddMessageFilter(this); } }}示例二:窗体设置右键控件,演示禁用和解除禁用右键功能,右键菜单只有复制、剪切、粘贴三项
界面:
代码:
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 MouseRightDemo{ public partial class FrmMouseRight : Form ,IMessageFilter { public FrmMouseRight() { InitializeComponent(); } /// /// 实现方法 /// /// /// public bool PreFilterMessage(ref Message m) { //不响应鼠标右键 if (m.Msg >= 516 && m.Msg
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 277
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.