简体   繁体   中英

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

Use this code and implement IMessageFilter aswell

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; and then 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)更好

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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