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#設(shè)計(jì)模式之建造者模式生成器模式示例詳解
這篇文章主要為大家介紹了C#設(shè)計(jì)模式之建造者模式生成器模式示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08C# 使用SharpZipLib生成壓縮包的實(shí)例代碼
SharpZipLib是一個(gè)C#的類庫(kù),主要用來(lái)解壓縮Zip,GZip,BZip2,Tar等格式,是以托管程序集的方式實(shí)現(xiàn),可以方便的應(yīng)用于其他的項(xiàng)目之中。本文通過(guò)一個(gè)實(shí)例代碼給大家介紹了C# 使用SharpZipLib生成壓縮包的方法,感興趣的朋友跟隨小編一起看看吧2018-09-09C#中SQL參數(shù)傳入空值報(bào)錯(cuò)解決方案
這篇文章主要介紹了C#中SQL參數(shù)傳入空值報(bào)錯(cuò)解決方案,需要的朋友可以參考下2017-06-06c#基于NVelocity實(shí)現(xiàn)代碼生成
這篇文章主要介紹了c#基于NVelocity實(shí)現(xiàn)代碼生成的方法,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-01-01C#更新SQLServer中TimeStamp字段(時(shí)間戳)的方法
這篇文章主要介紹了C#更新SQLServer中TimeStamp字段(時(shí)間戳)的方法,涉及C#操作數(shù)據(jù)庫(kù)字段的相關(guān)技巧,需要的朋友可以參考下2015-05-05C# 參考之訪問(wèn)關(guān)鍵字:base、this
由于靜態(tài)成員函數(shù)存在于類一級(jí),并且不是對(duì)象的一部分,因此沒(méi)有 this 指針。在靜態(tài)方法中引用 this 是錯(cuò)誤的。 索引器允許類或結(jié)構(gòu)的實(shí)例按照與數(shù)組相同的方式進(jìn)行索引。索引器類似于屬性,不同之處在于它們的訪問(wèn)器采用參數(shù)。2008-03-03C#調(diào)用OpenCV開(kāi)發(fā)簡(jiǎn)易版美圖工具【推薦】
本文主要介紹在WPF項(xiàng)目中使用OpenCVSharp3-AnyCPU開(kāi)源類庫(kù)處理圖片,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2019-10-10C#實(shí)現(xiàn)語(yǔ)音播報(bào)功能
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)語(yǔ)音播報(bào)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03