在C#中怎样禁用鼠标按键,我们可以通过ImessageFilter接口下的PreFilterMessage方法、Application类的AddMessageFilter方法,Rem
在C#中怎样禁用鼠标按键,我们可以通过ImessageFilter接口下的PreFilterMessage方法、Application类的AddMessageFilter方法,RemoveMessageFilter方法和Message结构的Msg属性来禁用鼠标左键。Message结构包装windows发送的消息,可使用该结构包装消息,并将其分配给窗口过程以进行调度,还可以使用该结构获取系统向应用程序或控件发送的关于某个消息的信息。
使用PreFilterMessage方法在调度消息之前将其筛选出来。语法格式如下:
Bool PreFilterMessage(ref Message m)
参数说明:
界面:
代码实现:
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);
}
}
}
界面:
代码:
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();
}
/// <summary>
/// 实现方法
/// </summary>
/// <param name="m"></param>
/// <returns></returns>
public bool PreFilterMessage(ref Message m)
{
//不响应鼠标右键
if (m.Msg >= 516 && m.Msg <= 517)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 禁用鼠标右键
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
//添加消息
Application.AddMessageFilter(this);
MessageBox.Show("鼠标右键已被禁止使用", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
/// <summary>
/// 解决禁用鼠标右键
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
//移除消息
Application.RemoveMessageFilter(this);
MessageBox.Show("鼠标右键已被解除禁止使用,可以使用鼠标右键", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
鼠标移动:512
鼠标左键:
down:513 up:514
double click:515
鼠标右键:
down:516 up:517
鼠标滚轮:522
--结束END--
本文标题: C#实现鼠标消息捕获
本文链接: https://lsjlt.com/news/140616.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0