全局阻止鼠标进入屏幕区域

Globally preventing mouse from entering screen area

我想做的是在我的桌面上创建一个坚不可摧的边缘来保护"zone"。我正在寻找的东西有点像鼠标陷阱,但我不想被严格限制在一个矩形中,因为这将用于多显示器设置,其中桌面区域可能不是一个完美的矩形,我想要隔离的区域可能在中间的某个地方。

我正在使用 Gma.System.MouseKeyHook 来连接鼠标线,我知道这在 pinvoke 中并不难,但我选择了图书馆路线。

到目前为止我拥有的是:

    const uint MOUSEEVENTF_ABSOLUTE = 0x8000;
    const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
    const uint MOUSEEVENTF_LEFTUP = 0x0004;
    const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020;
    const uint MOUSEEVENTF_MIDDLEUP = 0x0040;
    const uint MOUSEEVENTF_MOVE = 0x0001;
    const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
    const uint MOUSEEVENTF_RIGHTUP = 0x0010;
    const uint MOUSEEVENTF_XDOWN = 0x0080;
    const uint MOUSEEVENTF_XUP = 0x0100;
    const uint MOUSEEVENTF_WHEEL = 0x0800;
    const uint MOUSEEVENTF_HWHEEL = 0x01000;

    [DllImport("User32.Dll")]
    public static extern long SetCursorPos(int x, int y);

    [DllImport("user32.dll")]
    static extern void mouse_event(uint dwFlags, int dx, int dy, uint dwData, int dwExtraInfo);

    private IKeyboardMouseEvents m_GlobalHook;

    public MainWindow()
    {
        InitializeComponent();

        m_GlobalHook = Hook.GlobalEvents();
        m_GlobalHook.MouseMove += M_GlobalHook_MouseMove;
    }

    private void M_GlobalHook_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        if (e.X < 0)
        {
            //SetCursorPos(0, e.Y);
            //mouse_event((int)MOUSEEVENTF_MOVE | (int)MOUSEEVENTF_ABSOLUTE, 0, e.Y, 0, 0);
            System.Windows.Forms.Cursor.Position = new System.Drawing.Point(0, e.Y);
        }
    }

我面临的问题是鼠标的实际位置和光标似乎是抽象的或没有绑定在一起,所以我在它在我试图避免的区域的位置和边缘之间来回出现奇怪的闪烁。如您所见,我尝试了几种不同的方法,但它们的行为都相同。

请注意,为了测试和简化,我的测试区域只是 X < 0 的任何东西,这是我的一个侧面显示器。

答案是做一个低级别的鼠标挂钩,而不是在 CallNextHookEx 位于我想让鼠标进入的区域时返回 (IntPtr)1。这顺利地阻止了鼠标进入我的禁区。

代码很乱,因为它只是为了证明概念而拼凑在一起,但有人可能会根据需要对其进行调整。

public partial class MainWindow : Window
{
    public static IntPtr _hookID = IntPtr.Zero;
    private static MouseHook.LowLevelMouseProc _proc;

    public MainWindow()
    {
        InitializeComponent();

        _proc = new MouseHook.LowLevelMouseProc(HookCallback);
        _hookID = MouseHook.SetHook(_proc);
    }

    private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
    {
        if (nCode >= 0 &&  MouseHook.MouseMessages.WM_MOUSEMOVE == (MouseHook.MouseMessages)wParam)
        {
            MouseHook.MSLLHOOKSTRUCT hookStruct = (MouseHook.MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MouseHook.MSLLHOOKSTRUCT));
            if (hookStruct.pt.x < 0)
            {
                System.Windows.Forms.Cursor.Position = new System.Drawing.Point(0, hookStruct.pt.y);
                return (IntPtr)1;
            }
        }
        return MouseHook.CallNextHookEx(_hookID, nCode, wParam, lParam);
    }
}

class MouseHook
{
    private const int WH_MOUSE_LL = 14;

    public enum MouseMessages
    {
        WM_LBUTTONDOWN = 0x0201,
        WM_LBUTTONUP = 0x0202,
        WM_MOUSEMOVE = 0x0200,
        WM_MOUSEWHEEL = 0x020A,
        WM_RBUTTONDOWN = 0x0204,
        WM_RBUTTONUP = 0x0205
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct POINT
    {
        public int x;
        public int y;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct MSLLHOOKSTRUCT
    {
        public POINT pt;
        public uint mouseData;
        public uint flags;
        public uint time;
        public IntPtr dwExtraInfo;
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool UnhookWindowsHookEx(IntPtr hhk);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr GetModuleHandle(string lpModuleName);

    [DllImport("kernel32.dll")]
    static extern IntPtr LoadLibrary(string lpFileName);

    public delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);

    public static IntPtr SetHook(LowLevelMouseProc proc)
    {
        IntPtr hInstance = LoadLibrary("User32");
        return MouseHook.SetWindowsHookEx(WH_MOUSE_LL, proc, hInstance, 0);
    }
}