C#實(shí)現(xiàn)鼠標(biāo)消息捕獲
在C#中怎樣禁用鼠標(biāo)按鍵,我們可以通過(guò)ImessageFilter接口下的PreFilterMessage方法、Application類的AddMessageFilter方法,RemoveMessageFilter方法和Message結(jié)構(gòu)的Msg屬性來(lái)禁用鼠標(biāo)左鍵。Message結(jié)構(gòu)包裝Windows發(fā)送的消息,可使用該結(jié)構(gòu)包裝消息,并將其分配給窗口過(guò)程以進(jìn)行調(diào)度,還可以使用該結(jié)構(gòu)獲取系統(tǒng)向應(yīng)用程序或控件發(fā)送的關(guān)于某個(gè)消息的信息。
使用PreFilterMessage方法在調(diào)度消息之前將其篩選出來(lái)。語(yǔ)法格式如下:
Bool PreFilterMessage(ref Message m)
參數(shù)說(shuō)明:
- m:要調(diào)度的消息,無(wú)法修改此消息。
- 返回值:如果篩選消息并禁止消息被調(diào)度,則為T(mén)rue;如果允許消息繼續(xù)到達(dá)下一個(gè)篩選器或控件,則為False。使用AddMessageFilter方法添加消息篩選器以便在向目標(biāo)傳送Windows消息時(shí)監(jiān)視這些消息。使RemoveMessageFilter 從應(yīng)用程序的消息泵移除一個(gè)消息篩選器。
示例一:在ComboBox選擇值的時(shí)候,選擇的值會(huì)隨鼠標(biāo)滾輪的滑動(dòng)而改變,有時(shí)候不小心滑動(dòng)了滑輪,導(dǎo)致選擇的值改變,在下面的示例中,通過(guò)禁用鼠標(biāo)滾輪,防止鼠標(biāo)滾輪的滑動(dòng)改變ComboBox選擇的值。
界面:

代碼實(shí)現(xiàn):
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, "一年級(jí)");
dictGrade.Add(2, "二年級(jí)");
dictGrade.Add(3, "三年級(jí)");
dictGrade.Add(4, "四年級(jí)");
dictGrade.Add(5, "五年級(jí)");
dictGrade.Add(6, "六年級(jí)");
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)
{
//添加消息過(guò)濾
Application.AddMessageFilter(this);
}
}
}示例二:窗體設(shè)置右鍵控件,演示禁用和解除禁用右鍵功能,右鍵菜單只有復(fù)制、剪切、粘貼三項(xiàng)
界面:

代碼:
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>
/// 實(shí)現(xiàn)方法
/// </summary>
/// <param name="m"></param>
/// <returns></returns>
public bool PreFilterMessage(ref Message m)
{
//不響應(yīng)鼠標(biāo)右鍵
if (m.Msg >= 516 && m.Msg <= 517)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 禁用鼠標(biāo)右鍵
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
//添加消息
Application.AddMessageFilter(this);
MessageBox.Show("鼠標(biāo)右鍵已被禁止使用", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
/// <summary>
/// 解決禁用鼠標(biāo)右鍵
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
//移除消息
Application.RemoveMessageFilter(this);
MessageBox.Show("鼠標(biāo)右鍵已被解除禁止使用,可以使用鼠標(biāo)右鍵", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}鼠標(biāo)動(dòng)作常見(jiàn)參數(shù):
鼠標(biāo)移動(dòng):512
鼠標(biāo)左鍵:
down:513 up:514
double click:515
鼠標(biāo)右鍵:
down:516 up:517
鼠標(biāo)滾輪:522
到此這篇關(guān)于C#實(shí)現(xiàn)鼠標(biāo)消息捕獲的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C#實(shí)現(xiàn)圖表中鼠標(biāo)移動(dòng)并顯示數(shù)據(jù)
- C#簡(jiǎn)單獲取全屏中鼠標(biāo)焦點(diǎn)位置坐標(biāo)的方法示例
- C#實(shí)現(xiàn)的鼠標(biāo)鉤子
- C#鍵盤(pán)鼠標(biāo)鉤子實(shí)例
- C#實(shí)現(xiàn)鼠標(biāo)移動(dòng)到曲線圖上顯示值的方法
- C#實(shí)現(xiàn)隨鼠標(biāo)移動(dòng)窗體實(shí)例
- C#實(shí)現(xiàn)獲取鼠標(biāo)句柄的方法
- C#中winform實(shí)現(xiàn)自動(dòng)觸發(fā)鼠標(biāo)、鍵盤(pán)事件的方法
- C# 鼠標(biāo)穿透窗體功能的實(shí)現(xiàn)方法
- 解決C#獲取鼠標(biāo)相對(duì)當(dāng)前窗口坐標(biāo)的實(shí)現(xiàn)方法
- 用C# 實(shí)現(xiàn)鼠標(biāo)框選效果的實(shí)現(xiàn)代碼
- C# 禁用鼠標(biāo)中間鍵的方法
相關(guān)文章
C#創(chuàng)建壓縮文件的實(shí)現(xiàn)代碼
本篇文章主要介紹了C# 創(chuàng)建壓縮文件的實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
C#使用post發(fā)送和接收數(shù)據(jù)的方法
這篇文章主要介紹了C#使用post發(fā)送和接收數(shù)據(jù)的方法,涉及C#使用post收發(fā)數(shù)據(jù)的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
C#從windows剪貼板獲取并顯示文本內(nèi)容的方法
這篇文章主要介紹了C#從windows剪貼板獲取并顯示文本內(nèi)容的方法,涉及C#操作剪貼板的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
C#中==(雙等于號(hào))與equals()區(qū)別詳解
這兩種方式也是大家在日常編碼工作當(dāng)中用的比較多的判斷方式,本文就詳細(xì)的介紹一下C# 雙等于號(hào)與equals()區(qū)別,方便大家理解2021-05-05
C#二叉搜索樹(shù)算法實(shí)現(xiàn)步驟和實(shí)例代碼
二叉搜索樹(shù)(Binary?Search?Tree,簡(jiǎn)稱BST)是一種節(jié)點(diǎn)有序排列的二叉樹(shù)數(shù)據(jù)結(jié)構(gòu),這篇文章主要介紹了C#二叉搜索樹(shù)算法實(shí)現(xiàn)步驟和實(shí)例代碼,需要的朋友可以參考下2024-08-08

