【C#】C# 限制鼠标的移动范围

本文探讨了一个使用C#实现的限制鼠标活动区域的代码片段,该功能在单显示器环境下正常工作,但在双屏显示设置中,当指定的范围超出主屏幕时,功能失效。问题主要出现在多显示器配置下,代码无法正确约束鼠标在指定屏幕内。针对这个问题,文章可能涉及显示器坐标系统、Windows API调用以及多显示器环境的适配策略。
public struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
            public RECT(Int32 left, Int32 top, Int32 right, Int32 bottom)
            {
                Left = left;
                Top = top;
                Right = right;
                Bottom = bottom;
            }
        }
[DllImport("user32.dll")]
        static extern bool ClipCursor(IntPtr lpRect);
        [System.Runtime.InteropServices.DllImport("User32.dll")]
        static extern bool ClipCursor(ref RECT lpRect);
        public static void LimitMouse(int left, int top, int right, int bottom)
        {
            var rect = new RECT(left, top, right, bottom);
            bool flag = ClipCursor(ref rect);
        }
        public static void LimitMouse()
        {
            bool flag = ClipCursor(IntPtr.Zero);
        }

以上方法测试,可用,但是有一个问题,当使用双屏显示器的时候,设置的鼠标范围,超出一块屏幕的位置时,也就是在副屏的范围,该功能变失效了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值