• 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

  • 相关阅读:
    Spark开发环境搭建(IDEA、Scala、SVN、SBT)
    Spark源码系列:RDD repartition、coalesce 对比
    Scala:类和对象
    申请MS的FastCounter
    code generation part1some OOrelated topic! not completed
    [book]ADO.NET实用指南
    如果FC能把blog的WEB VIEW与AGG VIEW统计起来就方便多了
    Dell 1704
    O'Reilly .NET Framework Essentials, 2nd Edition
    单用户blog系统(一)
  • 原文地址:https://www.cnblogs.com/dotnet261010/p/6691332.html
Copyright © 2020-2023  润新知