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

前提是你 提交于 2019-11-28 23:42:22

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.

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.

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.

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.

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