简体   繁体   English

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

[英]How to disable mouse clicks in C#

Trying To achieve努力实现

I'm trying to block input from mouse for certain time我试图在一段时间内阻止来自鼠标的输入

I want to use the code like this: -我想使用这样的代码:-

BlockMouse(true);

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

BlockMouse(false);

I Tried我试过

  • BlockInput(true) but it requires elevated permissions BlockInput(true)但它需要提升的权限

Use this code and implement IMessageFilter aswell使用此代码并实现 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);
}  

I think it is more elegant to set all your controls (like button) to disabled for the time your code is running and then enable them again我认为在代码运行时将所有控件(如按钮)设置为禁用然后再次启用它们会更优雅

To give the user an optical feedback set the cursor to busy with Application.UseWaitCursor = true;为了给用户一个视觉反馈,将光标设置为忙Application.UseWaitCursor = true; and then Application.UseWaitCursor = false;然后Application.UseWaitCursor = false;

of course this solution only blocks the mouse clicks on your application and does not disable the mouse clicks completely当然,此解决方案只会阻止鼠标点击您的应用程序,并不会完全禁用鼠标点击

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM