限制鼠标的移动范围

利用api 函数 ClipCursor和GetWindowRect可以实现限定鼠标移动范围的功能。
 
[System.Runtime.InteropServices.DllImport("user32", EntryPoint = "ClipCursor")]
        public extern static int ClipCursor(ref   RECT lpRect);
        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetWindowRect")]
        public extern static int GetWindowRect(int hwnd, ref   RECT lpRect);

        public struct RECT//声明参数的值
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }
        public void Lock(System.Windows.Forms.Form ObjectForm)
        {
            RECT _FormRect = new RECT();
            GetWindowRect(ObjectForm.Handle.ToInt32(), ref _FormRect);
            ClipCursor(ref   _FormRect);
        }
        public void UnLock()
        {
            RECT _ScreenRect = new RECT();

            _ScreenRect.top = 0;
            _ScreenRect.left = 0;
            _ScreenRect.bottom = int.MaxValue; ;
            _ScreenRect.right = int.MaxValue;
            ClipCursor(ref   _ScreenRect);
        }
        private void bntKong_Click(object sender, EventArgs e)
        {
            this.Lock(this);
        }

        private void bntMove_Click(object sender, EventArgs e)
        {
            this.UnLock();
        }

微软BI技术交流群:316744959 武汉NET技术群:961108969 NET技术群:21386099 本人具有丰富的系统开发经验,承接系统开发,小程序,NET系统开发,BI开发,有需求联系微信手机:15010195887
原文地址:https://www.cnblogs.com/Impulse/p/1166873.html