2

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
Agent_Spock
  • 1,107
  • 2
  • 16
  • 44
Jquey007
  • 65
  • 1
  • 1
  • 6

3 Answers3

2

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);
}  
mmollle
  • 33
  • 6
1

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

RomCoo
  • 1,868
  • 2
  • 23
  • 36
  • 2
    This is a nice solution but he never says he only wants to stop clicks on his program, so if he wants to stop all mouse clicks system wide this no longer works. – Jacobr365 Mar 02 '16 at 21:04
-1

I don't think it is good to block mouse click because most of the inputs are by mouse click, it is better if you rewrite protected override void OnMouseDown(MouseEventArgs e)

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Xiaohuan ZHOU
  • 478
  • 5
  • 6