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

代碼實現(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, "一年級");
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);
}
}
}示例二:窗體設(shè)置右鍵控件,演示禁用和解除禁用右鍵功能,右鍵菜單只有復(fù)制、剪切、粘貼三項
界面:

代碼:
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>
/// 實現(xiàn)方法
/// </summary>
/// <param name="m"></param>
/// <returns></returns>
public bool PreFilterMessage(ref Message m)
{
//不響應(yīng)鼠標右鍵
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);
}
}
}鼠標動作常見參數(shù):
鼠標移動:512
鼠標左鍵:
down:513 up:514
double click:515
鼠標右鍵:
down:516 up:517
鼠標滾輪:522
到此這篇關(guān)于C#實現(xiàn)鼠標消息捕獲的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#使用post發(fā)送和接收數(shù)據(jù)的方法
這篇文章主要介紹了C#使用post發(fā)送和接收數(shù)據(jù)的方法,涉及C#使用post收發(fā)數(shù)據(jù)的相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04
C#從windows剪貼板獲取并顯示文本內(nèi)容的方法
這篇文章主要介紹了C#從windows剪貼板獲取并顯示文本內(nèi)容的方法,涉及C#操作剪貼板的相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04

