Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling mouse movement and clicks altogether in c# [duplicate]

Tags:

c#

mouse

At work, i'm a trainer. I'm setting up lessons to teach people how to "do stuff" without a mouse... Ever seen people click "login" textbox, type, take the mouse, click "password", type their password, then take the mouse again to click the "connect" button beneath ?

So i'll teach them how to do all that without a mouse (among many other things, of course)

At the end of the course, i'll make them pass a sort of exam.

So i'm building a little wizard based app in which i present some simili-real-life examples of forms to fill in, but i want to disable their mouse programatically while they do this test.

However, further in the wizard, i'll have to let them use their mouse again.

Is there a -- possibly easy -- way to just disable the mouse for a while, and re-enable it later on?

I'm on C# 2.0, programming under VC# 2k5, if that matters

like image 748
ThaNerd Avatar asked Apr 23 '10 13:04

ThaNerd


2 Answers

Make your form implement IMessageFilter.

Then add the following code to the form:

    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);
    }  

This will hide the cursor, make it so that they can't move it and disable the right and left mousebuttons.

like image 90
Hans Olsson Avatar answered Oct 15 '22 06:10

Hans Olsson


You're looking for the Cursor.Hide() method.

Note that the cursor will still be movable, it just won't be visible.
If you're running with Visual Styles enabled, it would be still possible to use the mouse by tracking hover effects.
However, anyone capable of doing that probably doesn't need your course.

A more "fun" way of doing this would be to hanle the MouseMove event and set Cursor.Position to prevent the mouse from moving into your panel.

like image 28
SLaks Avatar answered Oct 15 '22 08:10

SLaks