【问题标题】:How to disable mouse clicks in C#如何在 C# 中禁用鼠标单击
【发布时间】:2016-03-02 20:45:09
【问题描述】:

努力实现

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

我想使用这样的代码:-

BlockMouse(true);

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

BlockMouse(false);

我试过了

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

【问题讨论】:

  • 看一个低级鼠标钩子。
  • 我对低级鼠标以及如何取消鼠标点击事件一无所知
  • 也许你应该解释一下你为什么要这样做。可能有更直接的方法来实现相同的整体效果。
  • @Jquey007 也许这是值得研究的事情......之前也有人问过这个问题。 stackoverflow.com/questions/4524608/…
  • 好的,我的应用程序最终将信息发送到其他应用程序,它发送 Enter 按钮事件,该事件会因鼠标点击而受到干扰

标签: c# winforms


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    为了给用户一个视觉反馈,将光标设置为忙于Application.UseWaitCursor = true; 然后Application.UseWaitCursor = false;

    当然,此解决方案只会阻止鼠标点击您的应用程序,并不会完全禁用鼠标点击

    【讨论】:

    • 这是一个不错的解决方案,但他从来没有说过他只想停止点击他的程序,所以如果他想停止系统范围内的所有鼠标点击,这将不再有效。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-21
      • 1970-01-01
      • 2023-04-06
      • 2020-11-16
      • 1970-01-01
      • 2012-01-25
      相关资源
      最近更新 更多