網(wǎng)站首頁 編程語言 正文
在C#中怎樣禁用鼠標(biāo)按鍵,我們可以通過ImessageFilter接口下的PreFilterMessage方法、Application類的AddMessageFilter方法,RemoveMessageFilter方法和Message結(jié)構(gòu)的Msg屬性來禁用鼠標(biāo)左鍵。Message結(jié)構(gòu)包裝Windows發(fā)送的消息,可使用該結(jié)構(gòu)包裝消息,并將其分配給窗口過程以進(jìn)行調(diào)度,還可以使用該結(jié)構(gòu)獲取系統(tǒng)向應(yīng)用程序或控件發(fā)送的關(guān)于某個(gè)消息的信息。
使用PreFilterMessage方法在調(diào)度消息之前將其篩選出來。語法格式如下:?
Bool PreFilterMessage(ref Message m)
參數(shù)說明:
- m:要調(diào)度的消息,無法修改此消息。
- 返回值:如果篩選消息并禁止消息被調(diào)度,則為True;如果允許消息繼續(xù)到達(dá)下一個(gè)篩選器或控件,則為False。使用AddMessageFilter方法添加消息篩選器以便在向目標(biāo)傳送Windows消息時(shí)監(jiān)視這些消息。使RemoveMessageFilter?從應(yīng)用程序的消息泵移除一個(gè)消息篩選器。
示例一:在ComboBox選擇值的時(shí)候,選擇的值會隨鼠標(biāo)滾輪的滑動而改變,有時(shí)候不小心滑動了滑輪,導(dǎo)致選擇的值改變,在下面的示例中,通過禁用鼠標(biāo)滾輪,防止鼠標(biāo)滾輪的滑動改變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; } } ////// 窗體加載 /// /// /// private void FrmMain_Load(object sender, EventArgs e) { InitComboBox(); } ////// 初始化ComboBox /// private void InitComboBox() { DictionarydictGrade = 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); } } }
示例二:窗體設(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(); } ////// 實(shí)現(xiàn)方法 /// /// ///public bool PreFilterMessage(ref Message m) { //不響應(yīng)鼠標(biāo)右鍵 if (m.Msg >= 516 && m.Msg <= 517) { return true; } else { return false; } } /// /// 禁用鼠標(biāo)右鍵 /// /// /// private void button1_Click(object sender, EventArgs e) { //添加消息 Application.AddMessageFilter(this); MessageBox.Show("鼠標(biāo)右鍵已被禁止使用", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } ////// 解決禁用鼠標(biāo)右鍵 /// /// /// private void button2_Click(object sender, EventArgs e) { //移除消息 Application.RemoveMessageFilter(this); MessageBox.Show("鼠標(biāo)右鍵已被解除禁止使用,可以使用鼠標(biāo)右鍵", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }
鼠標(biāo)動作常見參數(shù):
鼠標(biāo)移動:512
鼠標(biāo)左鍵:
down:513 up:514
double click:515
鼠標(biāo)右鍵:
down:516 up:517
鼠標(biāo)滾輪:522
原文鏈接:https://www.cnblogs.com/dotnet261010/p/6691332.html
相關(guān)推薦
- 2022-11-16 PyTorch中torch.utils.data.DataLoader實(shí)例詳解_python
- 2023-03-13 Python實(shí)現(xiàn)柵欄密碼的加密解密方法詳解_python
- 2023-10-12 利用touch-action解決驗(yàn)證碼滑塊滑動時(shí),背景跟隨一起滑動的問題,以及詳解touch-act
- 2023-01-26 如何在.Net?7中將Query綁定到數(shù)組詳解_實(shí)用技巧
- 2022-10-09 C#使用Enum.TryParse()實(shí)現(xiàn)枚舉安全轉(zhuǎn)換_C#教程
- 2023-03-11 Rust如何進(jìn)行模塊化開發(fā)技巧分享_Rust語言
- 2022-11-03 C#如何給新建的winform程序添加資源文件夾Resources_C#教程
- 2022-10-16 Python?結(jié)構(gòu)化字符串中提取數(shù)據(jù)詳情_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支