c# - 如何在c#中禁用鼠标移动

标签 c# winforms winapi

<分区>

在我的 Windows 应用程序中,我想隐藏和禁用任何鼠标移动,因为我是在我的应用程序中处理它的。我可以将光标的位置设置为一个点,但如果用户移动鼠标,它就会改变位置。

如何阻止用户输入移动鼠标?

最佳答案

让您的表单实现 IMessageFilter。然后在您的表单中使用以下代码隐藏光标,但确保鼠标右键/左键单击也被禁用

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

参见:Disabling mouse movement and clicks altogether in c#

关于c# - 如何在c#中禁用鼠标移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8970845/

相关文章:

c# - 使用 C# 如何确定服务器上的 excel 文件是否已更新。此检查需要每 24 小时执行一次

c# - ListBox 项目在 OwnerDrawFixed 模式下不显示

c# - 确定用户是否属于本地管理员组

winapi - 在汇编程序中链接到 Kernel32.lib

c - 如何在我的计算机中用 C 查找已安装驱动程序的名称?

c# - 使用巨大的二维数组递归获取最大路径和

c# - mongodb获取集合名称c#

c# - 使用用户控件 C# 时的应用程序性能

c# - UWP App Start 抛出异常 : 'System.Exception' in mscorlib. ni.dll

c# - 序列化 View 或 View 模型