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

心不动则不痛 提交于 2019-11-27 15:01:59

问题


This question is an exact duplicate of:

  • Block/Nullify mouse movement/click in C# 1 answer

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


回答1:


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.




回答2:


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.




回答3:


How about a different approach (thinking out of the "have to program a solution to everything" box): before you start the lessons, disconnect all the mice... have them reconnect it when the mouse is needed again.




回答4:


Imho easiest will be to PInvoke the ShowCursor(FALSE) function (see http://msdn.microsoft.com/en-us/library/ms648396.aspx)

[DllImport("user32.dll")]
static extern int ShowCursor(bool bShow);

Edit: This is equivalent to calling Cursor.Hide () (http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor.hide(v=VS.100).aspx) if you are using Windows Forms.



来源:https://stackoverflow.com/questions/2698673/disabling-mouse-movement-and-clicks-altogether-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!