c# - 如何在C#中禁用鼠标点击

标签 c# winforms

努力实现

我试图在一段时间内阻止鼠标输入

我想使用这样的代码:-

BlockMouse(true);

// My code starts here
...
// My code ends here

BlockMouse(false);

我尝试过

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

最佳答案

使用此代码并实现 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);
}  

关于c# - 如何在C#中禁用鼠标点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35757695/

相关文章:

c# - 有没有办法返回创建的记录而无需再次查询数据库?

c# - 如何测试类型是否为原始类型

c# - 适用于 Android Xamarin 的 Google 帐户登录集成

winforms - 在 Beta 测试期间检测异常

c# - 删除硬编码值到 app.config 文件

c# - .ActiveControl 和 .Focus 之间的区别以及各自的作用

c# - 如何从 sqlite 恢复已删除的数据?

c# - 如何针对术语优化此 SharePoint 查询?

c# - 如何使用视觉样式绘制 3D 边框?

.net - 您是否必须在 VB.NET 中显式创建表单实例?