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選擇的值。

界面:

代碼實現:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 
11 namespace MouseDemo
12 {
13     public partial class FrmMain : Form,IMessageFilter
14     {
15         public FrmMain()
16         {
17             InitializeComponent();
18         }
19 
20         public bool PreFilterMessage(ref Message m)
21         {
22             if (m.Msg == 522)
23             {
24                 return true;
25             }
26             else
27             {
28                 return false;
29             }
30         }
31 
32         /// <summary>
33         /// 窗體加載
34         /// </summary>
35         /// <param name="sender"></param>
36         /// <param name="e"></param>
37         private void FrmMain_Load(object sender, EventArgs e)
38         {
39             InitComboBox();
40         }
41 
42         /// <summary>
43         /// 初始化ComboBox
44         /// </summary>
45         private void InitComboBox()
46         {
47             Dictionary<int, string> dictGrade = new Dictionary<int, string>();
48             dictGrade.Add(1, "一年級");
49             dictGrade.Add(2, "二年級");
50             dictGrade.Add(3, "三年級");
51             dictGrade.Add(4, "四年級");
52             dictGrade.Add(5, "五年級");
53             dictGrade.Add(6, "六年級");
54 
55             BindingSource dataSource = new BindingSource();
56             dataSource.DataSource = dictGrade;
57             cmb_Grade.DataSource = dataSource;
58             cmb_Grade.DisplayMember = "Value";
59             cmb_Grade.ValueMember = "Key";
60         }
61 
62         /// <summary>
63         /// 索引改變事件
64         /// </summary>
65         /// <param name="sender"></param>
66         /// <param name="e"></param>
67         private void cmb_Grade_SelectedIndexChanged(object sender, EventArgs e)
68         {
69               //添加消息過濾
70             Application.AddMessageFilter(this);
71         }
72 
73 
74     }
75 }

示例二:窗體設置右鍵控件,演示禁用和解除禁用右鍵功能,右鍵菜單只有復制、剪切、粘貼三項

界面:

代碼:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 
11 namespace MouseRightDemo
12 {
13     public partial class FrmMouseRight : Form   ,IMessageFilter
14     {
15         public FrmMouseRight()
16         {
17             InitializeComponent();
18         }
19 
20         /// <summary>
21         /// 實現方法
22         /// </summary>
23         /// <param name="m"></param>
24         /// <returns></returns>
25         public bool PreFilterMessage(ref Message m)
26         {
27             //不響應鼠標右鍵
28             if (m.Msg >= 516 && m.Msg <= 517)
29             {
30                 return true;
31             }
32             else
33             {
34                 return false;
35             }
36         }
37 
38         /// <summary>
39         /// 禁用鼠標右鍵
40         /// </summary>
41         /// <param name="sender"></param>
42         /// <param name="e"></param>
43         private void button1_Click(object sender, EventArgs e)
44         {
45                //添加消息
46             Application.AddMessageFilter(this);
47             MessageBox.Show("鼠標右鍵已被禁止使用", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
48         }
49 
50         /// <summary>
51         /// 解決禁用鼠標右鍵
52         /// </summary>
53         /// <param name="sender"></param>
54         /// <param name="e"></param>
55         private void button2_Click(object sender, EventArgs e)
56         {
57                 //移除消息
58             Application.RemoveMessageFilter(this);
59             MessageBox.Show("鼠標右鍵已被解除禁止使用,可以使用鼠標右鍵", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
60         }
61     }
62 }

鼠標動作常見參數:

鼠標移動:512

鼠標左鍵:

down:513 up:514

double click:515

鼠標右鍵:

down:516 up:517

鼠標滾輪:522

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM