如何在 C# 中禁用鼠标单击

本文关键字:鼠标 单击 | 更新日期: 2023-09-27 18:37:02

试图实现

我正在尝试阻止鼠标输入一段时间

我想像这样使用代码:-

BlockMouse(true);
// My code starts here
...
// My code ends here
BlockMouse(false);

我试过了

  • BlockInput(true),但它需要提升的权限

如何在 C# 中禁用鼠标单击

使用此代码并实现 IMessageFilter

Rectangle BoundRect;
Rectangle OldRect = Rectangle.Empty;
private void EnableMouse()
{
    Cursor.Clip = OldRect;
    Cursor.Show();
    Application.RemoveMessageFilter(this);
}
public bool PreFilterMessage(ref Message m)
{
    if (m.Msg == 0x201 || m.Msg == 0x202 || m.Msg == 0x203) return true;
    if (m.Msg == 0x204 || m.Msg == 0x205 || m.Msg == 0x206) return true;
    return false;
}
private void DisableMouse()
{
    OldRect = Cursor.Clip;
    // Arbitrary location.
    BoundRect = new Rectangle(50, 50, 1, 1); 
    Cursor.Clip = BoundRect;
    Cursor.Hide();
    Application.AddMessageFilter(this);
}  
我认为在

代码运行时将所有控件(如按钮)设置为禁用,然后再次启用它们会更优雅

要为用户提供光学反馈,请将光标设置为忙于Application.UseWaitCursor = true;,然后Application.UseWaitCursor = false;

当然,此解决方案仅阻止鼠标单击应用程序,而不会完全禁用鼠标单击

我认为阻止鼠标单击不好,因为大多数输入都是通过鼠标单击的,最好重写protected override void OnMouseDown(MouseEventArgs e)